The Ansible Tour, Part 1: One Repository, Three Servers – How the Code Is Organised and What Happens During a Run

Part 1 of a tour through my Ansible code: how one repository and 34 roles run a blog and two Nextclouds, and what happens during a playbook run.

Comic in the Ansible Tour series, route map with station 1: the man with a backpack full of tools holds the route map with three servers as destinations

🌐 Also available in: Deutsch · Français · Español · Italiano · Português · Nederlands · Polski

First part of a series about the Ansible code behind this blog and two Nextcloud servers. The code is public: github.com/aptupgrademe/www_k3s; the series describes release v3.3.0. Part 1 explains the structure and what happens during a run, parts 2 to 6 the roles, the deep dives the technology underneath.

In short: Three servers, 34 roles, four playbooks, about 22,000 lines – part 1 of the Ansible tour explains what you should know before your first look into the repository. How one set of roles serves two very different applications, where every value comes from, why the order in the playbook is a chain of dependencies and what actually happens between ansible-playbook and the final recap.

Three servers, 34 roles, four playbooks, about 22,000 lines of YAML and templates. Opening the repository for the first time, you mostly see files. This part explains three things worth knowing first: how one set of roles serves two very different applications, where every value comes from, and what actually happens between ansible-playbook and the final summary.

Table of Contents

🧩 One repository for two applications

The blog (WordPress) and the two Nextcloud servers have little in common at first sight. Underneath, though, they need the same things: a hardened AlmaLinux, a firewall, key-only SSH, K3s, TLS certificates, monitoring, logs, protection against brute-force attacks. That is why the roles are named after where they are used:

  • common_* runs on every server: firewall, SSH, K3s, Grafana, fail2ban and a good twenty more.
  • blog_* exists only for WordPress, next_* only for Nextcloud.

Two thirds of the code are shared. A fix to the firewall reaches all three servers with the next run, and a gap that one Nextcloud reveals gets closed for the blog as well.

The price is a rule that never takes a break: every change to a shared role has to work on both stacks. What it looks like when that is only true on paper showed a bug found on 26 October 2026: the fail2ban filter for Nextcloud logins only checked POST /login. Nextcloud, however, sends its login form to /index.php/login. The filter had been running on both Nextcloud servers since July and never counted a single attempt; the rate limit in front of it did not see the requests either. It only came out when a test fed the filters a real log line instead of just checking that fail2ban was running.

The takeaway: shared code saves work, but it needs tests that check the result. A service being configured and running does not prove that it does what it should.

📒 Where every value comes from

The roles describe what to do. The inventory decides with what: which servers exist, which values apply to all of them and which to only one.

  • hosts.yml holds nothing but the assignment: which server belongs to the group blog, which to nextcloud.
  • group_vars/all/ holds what is the same everywhere. The most important file there is versions.yml: every version a playbook installs (K3s, Helm, container images, exporters) is in exactly that file, down to the patch level. Never latest, never a tag like 10.11. Such a tag had left the blog on an outdated MariaDB image in August, still cached on the node, although Docker Hub had long had a newer one under the same name.
  • host_vars/<host>/ contains two files per server: vars.yml with addresses, sizes and switches, vault.yml with the passwords, encrypted with Ansible Vault. Neither is in the public repository. What is public is an .example file next to them that shows which keys a host needs.

When a value is defined in several places, Ansible’s precedence decides: role defaults are the starting point, group_vars override them, host_vars override both. One example is the SSH country filter: it is off in the role, and a server switches it on with a single line in its vars.yml.

So that a missing value does not surface halfway through a run as an obscure template error, 23 roles declare their inputs in meta/argument_specs.yml. Ansible checks them before the role runs its first task.

The takeaway: a version that is not pinned exactly is in reality pinned by the cache.

🔀 How a shared role tells the stacks apart

A common_* role sometimes has to know where it is running. Nextcloud has MariaDB and Redis on the host, the blog both in containers; the firewall therefore needs two more rules on Nextcloud. There are three patterns for this, in order of preference:

  1. The playbook says so. Every full playbook sets a variable stack: wordpress or stack: nextcloud. That is the only place where the decision is made; the roles only read this variable.
  2. One profile per stack. Roles that do the same kind of work on both stacks with different paths – the SELinux labels for the mounted directories, for example – keep both lists in their defaults and pick one by stack.
  3. Ask the server. Whether a server gets its address via DHCP is in no variable; the firewall asks NetworkManager. Without the DHCP rules the address would be gone at the next lease renewal.

Up to version 3.2 the roles decided by the group names in the inventory. That broke on the test server, which sits in both groups: during a blog installation the firewall opened Nextcloud’s database ports, and Prometheus tried to scrape Nextcloud exporters that did not exist there.

The takeaway: the decision belongs in one place – where it is known what gets installed, in the playbook, not in the inventory.

📜 The order in the playbook is a chain of dependencies

There are four playbooks: per application one for the full run and one for updates. A full playbook is a list of about 25 roles, and the order follows the dependencies, not the alphabet:

  • SELinux before the deployment. The pods mount directories from the host. If these do not carry the label container_file_t yet, SELinux blocks every write, and MariaDB does not start.
  • Monitoring after the deployment. Prometheus scrapes the exporters in the pods, and fail2ban reads the ingress logs; both only exist after the deployment.
  • AIDE always last. AIDE compares the system files with a baseline every night. A run changes monitored files on purpose, so the last role renews the baseline.

Every role carries a tag with its own name. That turns the full playbook into a precise tool: --tags blog_k3s_deploy brings only the Kubernetes objects up to date. Exactly there lay a trap that turned into a fixed rule: a run with --tags skips the last role, the AIDE baseline stays old, and a mail about changed files arrives at night. Since then every tagged run is followed by a second one with --tags common_rkhunter,common_aide_refresh.

The update playbooks restart the application and are written defensively for that reason: they refuse to run without --limit, do not update an instance that is already stuck in maintenance mode, and do the work in a block with a rescue that shows, if something fails, in which state the instance is left.

The takeaway: moving a role moves a dependency. The order is part of the code.

⚙️ What happens during a run

Connecting. Ansible logs in as root over SSH with a key; SSH accepts no password. Then it gathers only the few facts the roles need: distribution, kernel, hostname.

A report first. The first role changes nothing. For K3s, Helm, the ingress and cert-manager it compares what is installed, what versions.yml pins and what the latest release is. Whoever starts the run sees whether anything is outdated before anything changes.

Task by task. Ansible runs task 1 on every host in the run, then task 2, and so on. Every task reports ok (nothing to do), changed or failed. Here lies the most important property of the roles: idempotence. A second run right after the first must report changed=0. That turns the playbook into a checking tool: if it reports a change on a server that should match the code, that is either drift – someone changed something by hand – or a bug in the task. For this the tasks read the state first and change only what deviates. Services are not restarted at every opportunity but through handlers that run once at the end, and only if their configuration really changed.

The dry run. With --check --diff Ansible goes through every task, changes nothing and shows what it would change. The script scripts/check-drift.sh filters out only the tasks that would change something. Exit code 0 means: the server is exactly what the code describes.

The verdict. A run that ends with ok only says that every task did what it was told. Whether the application works afterwards is checked at the end by the verify roles: 30 to 38 checks per server, a coloured report, a mail, and the run fails if a critical check does not pass. Since 26 October 2026 these include effect tests: every fail2ban filter gets a real log line, and every jail briefly bans a test address that has to arrive in the kernel.

And always one host after the other: two parallel runs on the same server collided on the package database lock in October.

The takeaway: changed=0 on the second run is no cosmetic detail. It is what makes every real deviation stand out.

🧪 How a change reaches a server

The servers are production: other people’s files and a blog with readers. A change therefore always passes the same stages, from fast and local to slow and real:

  1. Change: the role, the template or versions.yml.
  2. Linting: ansible-lint and yamllint over the whole repository; on commit, gitleaks also checks for passwords and keys.
  3. Test without a server where a test exists (Kubernetes roles, update playbooks).
  4. Dry run with check-drift.sh and the question: is this exactly what I wanted? Anything else on the list is a surprise to understand first.
  5. Real run on one server, limited with --tags to what the change touches.
  6. The drift check again: now it has to say “no drift”.
  7. Commit and push; the CI checks the whole repository once more.

None of these stages relies on someone remembering something. The one rule that stays manual: real runs are started by a person, one host after the other, after the dry run has been read.

🎯 Conclusion: The five lessons of part 1

  • Shared roles save work, but only with tests that check the effect instead of the state.
  • Pin every version exactly, in one place.
  • The decision “blog or Nextcloud” belongs in the playbook.
  • The order of the roles is a chain of dependencies.
  • Idempotence turns a playbook into a checking tool.

🧭 Where the tour goes next

Every part follows the same grid: what a role does, why it is built that way, and what went wrong along the way. The last point is usually the most instructive.

Addendum (28 November 2026): The www_k3s repository is now archived and has been split: the Nextcloud part lives on in nextcloud_k3s, the blog server in www_hugo. The links in this post point to the state at the time and still work.

Some text and images on this site are AI-assisted or AI-generated. Details