Skip to content
jalogisch

from 10 minutes to 34 seconds - stop turning the whole server off and on again

· development tools

This week this blog got a new font. Better to read, that was the whole idea. A small change: one CSS file, three font files. The rollout took 38 seconds.

A few days earlier, the same change would have cost me ten minutes. I use Ansible to roll out configuration and data, and earlier that week one small website took ten minutes to go online. Then I saw a mistake. The fix was one more run. Ten minutes again.

That is not an Ansible problem. That is a planning problem.

hello, IT

The first scene of “The IT Crowd”: the phone rings in the basement, Roy picks up the phone, eyes still on the screen. “Hello, IT. Have you tried turning it off and on again?”

It is the oldest joke in support, and it works because it is true so often. But you do not reboot the whole building because one printer has a paper jam.

My playbook did exactly that. Every small change ran everything: base system, hardening, the monitoring agent, the container runtime, the proxy, and then all websites, one after the other. I counted one run: 287 tasks to change 13 things. All 13 in the one project I had touched. The other 274 tasks checked that the rest of the server was still the same as ten minutes ago. It was.

how it got there

Nobody plans a playbook like that. It grows. At the start there was only the server, so there was one playbook for the server. Then came the container runtime, then the proxy, then the first website, then the next one. Every new role landed in the same file, because that was the file that worked. Until it hurt.

the rest of the fire

A run that touches everything is not only slow. It can also break everything.

One wrong parameter in one of those big runs, and every website got a new password for its test access. Not only the one I was working on. All of them. The fix was a second full run, ten more minutes.

In the same show, Moss puts a burning fire extinguisher down on the desk: “I’ll put this over here, with the rest of the fire.” That was my playbook. If the run had only touched the one project, the mistake would have stayed with that one project.

cut by how often things change

So I cut it. Not by technology, but by how often each part changes:

  • server: base system, hardening, agents. Changes rarely. Runs when I change the server itself.
  • hosting: proxy, routes, the config of all websites. Changes sometimes.
  • content: one website, its files and its own config. Changes all the time.

Three playbooks, three small wrappers:

# web.yml: the whole server, then everything else
- name: server
  hosts: web
  roles: [base, hardening, monitoring_agent, container_runtime]

- name: hosting
  ansible.builtin.import_playbook: web-hosting.yml
./web-server.sh  web01             # everything, when the server changes
./web-hosting.sh web01             # proxy and all websites
./web-content.sh web01 blog        # one website, nothing else

The content playbook runs one role with one variable, -e only=blog, and the role loops only over that project. That run takes 34 seconds now. If nothing changed, 30 seconds and zero changes.

two traps on the way

Tags looked easier, and were not. My first idea was --tags content. But my role loads its tasks with include_tasks, and the Ansible docs are clear: “If you add tags to an include, they apply only to the include itself, not to any tasks in the included file or role.” So the tag ran the include, and then skipped everything inside. Separate playbooks were the boring and more stable way.

Copy does not like many files. The static sites went up with ansible.builtin.copy over a folder. Also from the docs: the recursive copy “does not scale to lots (>hundreds) of files”. Every file is its own round trip. Now I pack the folder locally with tar, unpack it on the server into a new folder and swap it with the old one. A checksum over all files decides first if there is anything to copy at all.

And a small bonus trap: ansible_become: true in the inventory beats become: false on a task. Variables win over keywords. With delegate_to: localhost, my laptop suddenly asked for sudo. Setting the variable on the task fixed it.

measure before you cut

You do not need to guess where the time goes. Turn on one callback for one run:

ANSIBLE_CALLBACKS_ENABLED=ansible.posix.profile_tasks ansible-playbook web-content.yml -e only=blog

At the end you get the slowest tasks with seconds next to them. In my case the answer was simple: most of the time went into tasks that had nothing to do with my change.

If you build something new with Ansible: cut it on day one. Ask for every role how often it will change, and put the things that change often into their own playbook. Have you tried turning it off and on again? Sure. Just not the whole building for one printer.

Read more: Ansible tags and includes, copy module, variable precedence, The IT Crowd quotes.