---
title: "Configure long-lived servers from code · Agent Wranglers"
url: https://agentwranglers.se/playbook/infrastructure/servers/configuration-management
updated: 2026-10-20
licence: "All rights reserved"
---

# Configure long-lived servers from code

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Required
- Chapter: [Servers](https://agentwranglers.se/playbook/infrastructure/servers)
- Rule: When a server lives for months, set up its packages, files and services from code that can run again without changing anything.
- How: Use a tool such as Ansible, cloud-init, Salt or NixOS. Write every step so that a second run makes no changes. Run the code in check mode on a schedule, so a hand-made change shows up as drift. Give people no shell rights for lasting changes, and route every change through the code.
- Check: Run the code twice in a row. The second run reports 0 changed. The scheduled check run reports no differences.

Example:

```yaml
# Ansible: the same steps every time, and a second run changes nothing
- hosts: shop
  become: true
  tasks:
    - name: Install the web server
      ansible.builtin.apt: { name: nginx, state: present }
    - name: Install the site settings
      ansible.builtin.template: { src: shop.conf.j2, dest: /etc/nginx/conf.d/shop.conf, mode: '0644' }
      notify: reload nginx
  handlers:
    - name: reload nginx
      ansible.builtin.service: { name: nginx, state: reloaded }
```

```sh
ansible-playbook site.yml --check --diff   # expect: changed=0
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers infrastructure playbook: https://agentwranglers.se/playbook/infrastructure/servers/configuration-management`.

- Previous: [Replace servers instead of patching them](https://agentwranglers.se/playbook/infrastructure/servers/immutable-servers)
- Next: [Rebuild any server from code](https://agentwranglers.se/playbook/infrastructure/servers/rebuild-from-scratch)
- [Servers](https://agentwranglers.se/playbook/infrastructure/servers)
