Checking servers against your roles
ansible-playbook --check --diff tells you what a playbook would change.
It cannot tell you what is on the server that no role knows about: the
package installed by hand, the extra sudo rule, the cron file someone
dropped in. lemano ansible check answers both: Ansible evaluates your
playbook in check mode, and Lemano compares each task’s result with what
it read on the host.
Before you start
lemano,lemano-ansibleand thelemano.lemanocollection installed on the controller (see how to install the collection).- A playbook and an inventory that reach the hosts. Nothing else: the first check creates the Lemano store and a target for each host, as recording playbook runs does.
A first check
lemano ansible check site.yml users.yml -i inventory/production --limit web1- The playbook is loaded with Ansible’s own loader, without running
anything. A task with
check_mode: falseruns for real even in check mode, and an include whose file name is templated cannot be inspected before the run: the check names each one and stops. Look at them, then add--allow-unsafe-tasksif they are safe to run. - Lemano reads each host once, so the comparison is made with what the host holds now.
ansible-playbook --check --diffruns; its output goes to standard error.- The report follows. Give every playbook that manages the hosts (or your site playbook): an object is forgotten only when none of them manages it, and the first line names them.
forgotten: managed by none of site.yml, users.yml
web1: 212 in sync, 3 drifted, 1 missing, 7 forgotten, 14 not tracked (coverage 87%)
drifted File//etc/nginx/nginx.conf role nginx, task "Deploy nginx.conf" (roles/nginx/tasks/main.yml:12) changed by alice 2 days ago (sudo)
~ content changed
missing Package/fail2ban role security, task "Install fail2ban" (roles/security/tasks/main.yml:3)
forgotten Package/htop installed by hand (not as a dependency)
forgotten Cron//etc/cron.d/backup-tmp/5d1e0c3a9b21 a cron file owned by no package
...Reading the report
- drifted and missing are what
--checkalready tells you, with Lemano’s view of what changed since the host’s baseline and who changed it. When you record your playbook runs, the baseline is the last successful run, so a drifted file names the hand edit made since. Who made it comes from the host’s logs: sudo’s log, in the journal or the auth log, attributes a change when the command names the file (sudo vi /etc/app.conf); a change made through a shell (sudo -i, orsudo shwith a script) is shown as made by an unknown hand. - forgotten is what your roles do not cover. The list keeps to what
matters (packages installed by hand after the system, libraries aside;
configuration files changed from their package’s copy or owned by no
package, local unit files, people’s accounts, authorized keys, sudo
rules, cron entries, firewall rules, kernel settings and mounts set in
/etc). Everything else is counted on thenot listedline, with the reason it was left out. The reference has the rules per kind. - runs that select tasks (
--tags,--skip-tags,--start-at-task) list nothing as forgotten: the tasks left out are never shown, so what they manage is unknown. The rest of the report is as usual. - managed in part: a file that only
lineinfileorblockinfiletasks touch is compared on its managed lines; the rest of it is never reported as forgotten. - not tracked lists the tasks the check cannot compare (
shellandcommandtasks, modules it does not map, tasks skipped in check mode), with their file and line: they are the places to make your roles more declarative. - coverage is, per host, the share of its meaningful objects your tasks manage, and, per role, the share of its tasks that map to objects.
In scripts and on a timer
lemano ansible check site.yml -i inventory/production -o json --detailed-exitcodeexits 8 when something is drifted, missing or forgotten, 0 when not, 7 when a host could not be read, and 2 when unsafe tasks were refused. The JSON carries every object with its state, task and role.
From your usual check-mode runs
With the callback plugin enabled in ansible.cfg
(callbacks_enabled = lemano.lemano.record), your usual
ansible-playbook site.yml --check --diffprints the same report after Ansible’s recap, with nothing new to type.
There, you started the run: Ansible runs check_mode: false tasks for
real and the plugin cannot stop them. It warns about each one and lists it
as not tracked. Such a run compares with its own playbooks only, so its
forgotten list is only as complete as they are; the report says so.
When the server is right and the role should say so, see Keeping your roles true to your servers.