The Ansible Playbook I Never Update
How I keep an Ansible playbook in sync with my Ubuntu laptop using etckeeper, replibook, and a weekly systemd drift check that emails me when the machine and the playbook disagree.
I bought a new laptop last week. It came with Windows 11, which lasted about eleven minutes on my desk before I had a USB stick plugged in and Ubuntu 26.04 installing over it.
Fresh OS, clean slate, exactly the scenario my Ansible playbook was built for. I’d been maintaining that playbook for years specifically so this moment would be painless.
It wasn’t painless. It was three hours of manual cleanup.
The playbook installed my packages. It set up my dotfiles. It configured a handful of services I’d remembered to write down. And then I spent the rest of the evening fixing everything it didn’t know about. The SSH config tweak I made last spring. The cron job I added and forgot. The three packages I installed on a whim that turned out to be load-bearing for my workflow.
This is the dirty secret of “infrastructure as code” for a personal laptop: you only update the playbook when you remember to. And you almost never remember to.
The real problem isn’t the playbook
For a while I thought the fix was discipline. Just update the YAML every time you touch the system. Be a good engineer. Treat your laptop like production.
I tried that for about three weeks. Then I added a function in ~/.config/fish/functions at midnight because I was tired of typing the same rsync command over and over. Useful, obviously. But it never made it into the playbook, so on the new laptop I had to use AI to regenerate the command and create the fish function again.
That’s the thing about drift on a personal machine. It’s rarely dramatic. It’s an alias, a .gitconfig tweak, a favorite font installed through a PPA, a hotkey bound in GNOME settings. Small quality-of-life changes you make without thinking, because thinking is what you’re trying to avoid. And every single one of them vanishes the moment you touch a fresh install.
The problem isn’t that I’m lazy. It’s that the feedback loop is broken. I have no idea when the playbook and the machine diverge. There’s no alarm that goes off. The machine just quietly accumulates undocumented changes, like debt.
So I stopped trying to reverse-engineer a perfect playbook and built something more useful: a detect drift, confirm manually, backfill the playbook loop. It’s not fully automated. It’s not supposed to be. But it works, and I actually maintain it.
Step one: stop flying blind on /etc
The first thing I installed was etckeeper. If you’ve never used it, here’s the pitch: it wraps /etc in a git repository and commits automatically whenever apt installs or removes packages. Every config file change becomes a commit. Every apt transaction becomes a commit.
sudo apt install etckeeper
That’s it. From then on, cd /etc && git log shows me exactly what changed and when. When I can’t remember why sshd_config has some line I don’t recognize, I check the blame. When I want to know what I touched last Tuesday, I check the log.
This alone solved half my problem. I could see the drift. I just couldn’t act on it efficiently yet.
Step two: generate a baseline with replibook
My playbook was years old and missing huge chunks of what my machine actually does. Rewriting it from memory was a non-starter. So I used replibook, which scans the current system and generates a playbook skeleton from what’s actually there.
replibook
It picks up apt-mark packages, systemctl services, crontabs, and a few other bits, and emits a playbook I can run directly. A few things to know before you try it:
- Network config and cron jobs are disabled by default. You need to review them and turn them on deliberately. This is good. It forces you to look.
- Treat the output as a draft, not a deliverable. My first generated playbook had a hundred-odd
state: presentpackage declarations, including transitive dependencies I never asked for. I pruned it down to what I actually care about.
But it gave me a baseline. Instead of staring at an empty file, I had something to correct. That’s a much easier job.
Step three: the drift detector that actually gets my attention
Here’s the part that made the whole loop stick. Every Sunday night, a systemd timer runs my playbook in check mode and emails me if anything would change.
The command is just:
ansible-playbook -i localhost, -c local playbook.yml --check --diff
--check means “don’t actually change anything.” --diff shows me what would change. If the output is all ok and skipping, my machine and my playbook agree. If anything says changed, that’s a manual edit I made and never wrote down.
The systemd unit and timer are short enough to paste:
# ~/.config/systemd/user/ansible-drift.service
[Unit]
Description=Ansible drift check
[Service]
Type=oneshot
ExecStart=/usr/bin/ansible-playbook -i localhost, -c local %h/ansible/playbook.yml --check --diff
StandardOutput=journal
# ~/.config/systemd/user/ansible-drift.timer
[Unit]
Description=Weekly Ansible drift check
[Timer]
OnCalendar=Sun 21:00
Persistent=true
[Install]
WantedBy=timers.target
Then systemctl --user enable --now ansible-drift.timer, and you’re done. I pipe the journal output into a little mail script, but even just running journalctl --user -u ansible-drift on Monday morning works.
The first Sunday I ran this, it reported seventeen changes. Seventeen things I’d done to my machine and never written down. Some were trivial. A couple were important. All of them were invisible to me before.
Where AI actually helps (and where it doesn’t)
I tried pointing an AI at the drift report and asking it to update the playbook automatically. Don’t do this. It doesn’t work, and the failure mode is annoying: the model can’t tell whether a package is something you deliberately installed or a dependency that got dragged in, and it can’t tell whether a config change was intentional or a temporary hack you meant to revert.
Where AI is genuinely useful: writing the YAML once I’ve decided what I want. I look at the --diff output, see that I changed PermitRootLogin in sshd_config, and ask it to write me a lineinfile task for that. It’s fast at syntax. It’s bad at intent. Keep it in that lane and it’s a real time saver.
The loop, in full
So here’s the whole thing, end to end:
etckeeperlogs every/etcchange automatically. I never think about it.replibookgave me a starting playbook that actually resembles my machine.- The systemd timer runs
--check --diffweekly and tells me when the two disagree. - I read the report, decide what’s intentional, and backfill the playbook. AI writes the YAML. I write the decisions.
It’s not glamorous. There’s no “fully automated” anywhere in there. But my playbook has stayed within a week of my actual machine for six months now, which is longer than any previous approach survived.
One thing I’d do differently
I wish I’d set up etckeeper before I started caring about the playbook. The git history in /etc has been more useful than the playbook itself for understanding what I did and why. If you’re starting from scratch, install it today, before you touch anything else.
And if you set up the drift timer, expect a slightly unpleasant first Sunday. Mine reported seventeen undocumented changes. Yours will probably report more. That’s not a failure of the system. That’s the system working. You just couldn’t see the debt before.
I’d genuinely like to know what your first drift report turns up. Mine had a cron job I’d completely forgotten about that was silently failing every night for months. If you find something like that, drop me a note. Misery loves company.
Need a Developer Who Gets It Done?
If this post helped solve your problem, imagine what we could build together! I'm a full-stack developer with expertise in Python, Django, Typescript, and modern web technologies. I specialize in turning complex ideas into clean, efficient solutions.