Skip to content

lemano ansible check

Compares each host with what a playbook would do to it. Ansible evaluates the playbook (variables, templates, conditions, includes, vault) in check mode; Lemano compares what each task would do with what it read on the host just before. The guide walks through a first check.

lemano ansible check PLAYBOOK... [-i INVENTORY]... [--limit PATTERN]
                     [--allow-unsafe-tasks] [--no-refresh] [-o json|yaml]
                     [--detailed-exitcode] [--all-files] [ansible-playbook flags]

The flags and examples of the command are also on its generated page.

The same comparison is printed by a plain ansible-playbook --check run when the lemano.lemano.record callback plugin is enabled (see Running check mode yourself).

What it does

  1. Loads the playbooks with Ansible’s own loader, without running anything, and looks for tasks that would run for real in check mode: tasks with check_mode: false, and include_tasks/include_role whose file name is templated (what they run cannot be known before the run). Includes with a literal name are followed. When there are any, it names each one with its file and line, runs nothing and exits 2, unless --allow-unsafe-tasks is given. --vault-id and --vault-password-file are given to the scan, to the reading of the inventory and to ansible-playbook, so that all of them read the same vaulted files; ANSIBLE_VAULT_PASSWORD_FILE works as it does for ansible-playbook. -J (--ask-vault-pass) asks for the password once, without echo, and gives all of them the same password (see asking for the vault password).
  2. Takes one pass on each host of the plays, after --limit, reached with the inventory’s own settings, as lemano ansible discover does. --no-refresh compares with each host’s latest generation instead.
  3. Runs ansible-playbook --check --diff with the inventories, the limit and every flag it does not know itself (-e, --vault-password-file, -u and the like), with the lemano.lemano.record callback plugin enabled through ANSIBLE_CALLBACKS_ENABLED. That variable replaces the setting of ansible.cfg, so the list it carries starts with the callbacks your environment or ansible.cfg already enable, as ansible-config dump reports them: they still run. Ansible’s output goes to standard error, so standard output carries the report alone. Ansible prints each task’s diff there, except for tasks marked diff: false (or no_log: true): mark the tasks that write secrets that way. Lemano’s own report follows its content rules.
  4. Compares and prints the report.

Several playbooks, and the scope

Give every playbook that manages the hosts, or your site playbook: lemano ansible check site.yml users.yml -i inventory. They run in one ansible-playbook --check --diff, the safety scan covers each of them, and an object is forgotten only when none of them manages it.

The report always names its scope: the text starts with forgotten: managed by none of site.yml, users.yml, and the JSON carries playbooks. An object one playbook leaves alone may well be managed by another one you did not give.

Runs that select tasks

A run with --tags, --skip-tags or --start-at-task never shows the tasks it did not select, so it cannot tell what they manage. In sync, drifted and missing are shown as usual; objects no selected task names are only counted, with the reason “this run selected tasks by tag” (or “started at a task”), and coverage is not computed. The text starts with forgotten: not listed, ... and the JSON carries selection.

The lemano.lemano collection must be installed, as the Install page shows. Results are printed, never stored.

States

Each object is in one of these states:

StateMeaning
in syncA task manages the object and would not change it.
driftedA task would change the object. The fields that changed since the host’s baseline are shown, with who changed them when the host’s logs say.
missingA task wants the object and the host does not have it.
forgottenThe object is on the host and meaningful (see below), and no task manages it.

A file that lineinfile, blockinfile, ini_file or replace manages is managed in part: the managed lines are in sync, drifted or missing, and the rest of the file is neither drifted nor forgotten. Edits elsewhere in it still show in lemano diff.

Tasks the comparison cannot use are listed as not tracked, with the reason: shell, command, raw, script and expect tasks; modules with no mapping; tasks skipped in check mode (a module without check mode support); tasks that failed in check mode; tasks that run for real (check_mode: false); tasks that name no object Lemano can find.

Tasks that change nothing on the host are left out of the comparison and of coverage: debug, set_fact, assert, fail, meta, include_tasks, import_tasks, include_role, import_role, include_vars, setup, gather_facts, stat, ping, pause, wait_for, wait_for_connection, add_host, group_by, set_stats, fetch, slurp, find, getent, package_facts, service_facts, command_info, validate_argument_spec, uri, async_status. So are tasks skipped by their when condition. The list is fixed for now.

Which tasks map to which objects

Modules of ansible.builtin, ansible.legacy, ansible.posix and community.general are mapped by their short name:

ModulesObjects
apt, dnf, dnf5, yum, package, apk, pacman, zypperPackage, by each name (a version suffix, =VERSION or -VERSION, is dropped; package files, URLs and groups name no object)
copy, templateFile dest; also the SudoRule of a file of /etc/sudoers.d
fileFile path
lineinfile, blockinfile, ini_file, replaceFile path, managed in part
service, systemd, systemd_serviceService name
user, groupUser, Group name
authorized_keySshAuthorizedKey of user, by the key’s SHA-256 fingerprint
cronthe Cron entries of /etc/cron.d/<cron_file>, else of the user’s crontab, managed in part
ufw, firewalld, iptablesthe FirewallRule whose comment is the task’s comment; without one, for ufw the rules with the task’s rule, direction, protocol, port, source and destination (both address families), for firewalld the entry of the task’s zone (default public) for its service, port or rich rule; an iptables task without a comment names no object
sysctlSysctl name, and its sysctl_file, managed in part
mountMount path; with fstype: swap, the Swap its src names

state: absent means the task wants the object gone: in sync when it is, drifted when it is still there.

What counts as forgotten

Only meaningful objects are listed as forgotten, so that the list stays short enough to read. Everything else is counted, by kind and reason, on the not listed line.

KindListed when
Packageinstalled by hand, not as a dependency, not with the system, and not a library: a package of priority required, important or standard, one the Debian installer’s list names, or one that dnf’s first transaction installed came with the system; a package of the libs or oldlibs section (Debian family) is a library
Filein /etc, /usr/local/bin or /usr/local/sbin, not a folder, and changed from its package’s copy or owned by no package; files the system writes for itself (account databases, hostname, resolv.conf, links of alternatives, units enabled under /etc/systemd, certificates, host keys) and files other kinds hold (sudoers, cron and sysctl files, ufw’s user.rules, user6.rules and ufw.conf) are left out
Serviceits unit file, or a drop-in that overrides it, is under /etc/systemd/system (written on the host); enabling a unit does not count
Usera person’s account, or a system account with a login shell; never root
Groupit has members
SshAuthorizedKeyalways
SudoRulea file of /etc/sudoers.d with rules that its package does not ship as is, or /etc/sudoers changed from its package’s copy
Cronan entry of a user’s crontab, or of a file owned by no package or changed from its package’s copy
FirewallRulea ufw rule, or a rule of a firewalld zone changed on the host (its file in /etc/firewalld/zones); the kernel’s live rules are counted, and the configuration files that load them are compared as files
Sysctlset in /etc/sysctl.conf or /etc/sysctl.d
Mountin /etc/fstab, and not a file system the machine boots from (/, /boot, /boot/efi, /efi); with the host read first, not in an /etc/fstab last changed before the first boot
Swapin /etc/fstab, turned on by a systemd unit, or turned on by hand; not one a generator makes at boot (its configuration is a file); with the host read first, not in an /etc/fstab last changed before the first boot
Checkoutalways; an ansible.builtin.git task with the same dest manages it. The files its index tracks belong to it and are not listed; its untracked and ignored files are files like any other

Part of the system image. When the host’s first boot is known (the firstBoot fact, the earliest of: when systemd wrote the ID into /etc/machine-id, its modification time, since an image ships the file empty; the creation of cloud-init’s instance folder or its boot-finished; the creation of the Debian installer’s log folder), a package installed by hand whose first install is before it, and a file owned by no package that was created and last changed (content, owner or mode) before it, came with the image: they are counted, not listed. The first install comes only from evidence an upgrade keeps (dpkg’s logs, the birth of the package’s folder in /usr/share/doc, dnf’s history). The first boot and a file’s times are read by the check’s own fresh pass (the firstBoot volatile fact of lemano discover -o json); they are not stored, since writing a file again with the same content, or /etc/machine-id again, moves them. Without such evidence (no birth times on the file system, or a check with --no-refresh) the object stays listed, with “could not be explained” and the reason. lemano ansible export reads the hosts the same way and selects the same objects.

Made by a package’s scripts. Some files no package owns are written by a package’s own scripts (the console font cache of console-setup, the keyboard settings of keyboard-configuration). Lemano ships rules that name a package and the globs of such files; a rule applies only when its package is installed, and a file it matches is counted as made by that package. Add your own rules in lemano/explain/*.yaml under your configuration folder ($XDG_CONFIG_HOME, ~/.config by default):

format: 1
rules:
  - package: myagent
    files: [/etc/myagent/state-*]

Added next to a package’s files. A file no package owns, in a folder whose other files one package owns (/etc/nginx/conf.d/site.conf), is still listed, as added next to the files of that package; with several packages owning files there, no package is named.

A folder of at least 10 files in which every file is owned by no package, and that holds nothing else Lemano read (no file a package owns, the system writes or a task manages), is listed as one entry with its file count and total size, its highest such folder below the top level:

  forgotten /etc/myapp/  2007 files, 91.0 MiB, all owned by no package

Its files still count as forgotten. --all-files lists each of them, and the JSON then carries them with folder set; the JSON always lists the folders under forgottenFolders (path, files, size in bytes). A folder of fewer than 10 such files is listed file by file, and a file named like a file of secrets (.env, a key file) is always listed on its own.

Coverage

  • Per host: the share of the host’s meaningful objects that a task manages (a file managed in part counts as managed).
  • Per role: the share of the role’s tasks that map to objects, with the list of those that do not (file, line and why). A task is counted once, whatever the hosts and loop items.

Output

rec: 3 in sync, 1 drifted, 1 missing, 4 forgotten, 1 not tracked (coverage 42%)
  drifted   File//etc/app/app.conf  role base, task "Deploy the app config" (roles/base/tasks/main.yml:9)  changed by alice 2 hours ago (sudo)
      ~ content changed
      ~ /status/size: 4 -> 8
  missing   File//etc/app/extra.conf  role base, task "Deploy the extra config" (roles/base/tasks/main.yml:14)
  forgotten Package/htop  installed by hand (not as a dependency)
  forgotten SudoRule//etc/sudoers.d/ops  a sudo rule outside the distribution's
  not tracked:
    role base, task "Reload the app" (roles/base/tasks/main.yml:25): shell or command
  not listed: 164 File (files as their package ships them), 132 File (folders), ...
role base: 5 of 6 tasks map to objects (83%)
  not tracked: role base, task "Reload the app" (roles/base/tasks/main.yml:25): shell or command

-o json and -o yaml carry every object with its state, task, role, place, changed fields and attribution, the counts by reason, the tasks not tracked and each role’s coverage. Sensitive fields are shown as changed without their values.

For a drifted file a copy or template task writes, the report shows the content diff from what the task would write to what the host holds, as lemano diff shows it: only for a text file every user on the host can read that is not a file of secrets (crontabs, sudoers files, fstab, authorized keys). The content of any other file is not shown, but it is compared in memory with the task’s side, and the report says “content changed (not shown: …)” when it differs, “content not compared (not shown: …)” when one side is not known, and nothing about content when it is the one the task writes (a file whose mode alone changed). The task’s side is the role’s file for a copy with src: (not one encrypted with Vault), a keyed hash of the content of a copy with content: (an HMAC-SHA256 with a random key made for each run, which lemano ansible check and codify hand to the plugin in a file only you can read, in the run’s private folder, removed after the run; the plugin never sends the content or a plain hash of it, and sends nothing when it has no key, so that file’s content is not compared), and for a template the content in Ansible’s check-mode diff, which the callback plugin keeps only for files whose content a diff shows, so a template of a hidden file is not compared (nor, with ansible-core 2.22 or later, a template whose content holds the value of a secret: Ansible replaces it by $REDACTED$ in the diff, so the content is not known and the file is listed as not compared); it is compared in memory and never stored, logged or written. A native ansible-playbook --check run with the plugin prints the same content lines: check-results begin hands the plugin, in memory, the run’s key and the list of files whose content a diff shows, and the plugin hands the key back to check-results end on the same pipe, never in a file, the environment or a command line. In JSON, contentChanged is true or false when the two sides were compared and absent when they were not; notShown is set only when the content differs or was not compared.

Of a task’s other arguments, only those that name what it manages, the state it wants and what a file task sets are kept (path, dest, file, sysctl_file, name, state, user, cron_file, comment, src, owner, group, mode, enabled, shell, home, groups, a sysctl task’s value, and a key’s fingerprint, never the key): other arguments never leave Ansible. With ansible-core 2.19 or later, the plugin also keeps where Ansible says each of those values came from (file and line), which lemano ansible codify uses as a hint.

Exit codes

CodeMeaning
0The check ran.
8With --detailed-exitcode: something is drifted, missing or forgotten.
2Unsafe tasks were refused (add --allow-unsafe-tasks), or a usage error.
7A host could not be read or compared (named on the report); no task ran on it (a play with no task, or whose tasks all skip it); or its play failed, at the task the report names, so the tasks not reached claim nothing. There is nothing to compare it with: it gets no forgotten list and no coverage.
3, 4, 5As for every command, by the reason the host’s pass failed: connection, host identity (an unpinned key that is not in your known_hosts files is refused with its fingerprint; give --accept-new-host-key to pin it), lock held. A host that failed so has this code instead of 7; the code of the run is the highest of the hosts'.
1Any other failure.

Running check mode yourself

With callbacks_enabled = lemano.lemano.record in ansible.cfg, a plain

ansible-playbook site.yml --check --diff

takes one pass of each play’s hosts before their first task and prints the same report after Ansible’s recap, each line starting with lemano:. Set the plugin’s refresh option to false (LEMANO_ANSIBLE_REFRESH=false) to compare with what Lemano read last. The report names the run’s own playbooks as its scope; when it lists forgotten objects, a last line suggests running lemano ansible check with all your playbooks (or your site playbook) to see what none of them manages.

Here you started the run, and Ansible runs check_mode: false tasks for real: the plugin cannot stop them. It warns about each one as it starts, and lists it as not tracked. Use lemano ansible check where such tasks must be refused before anything runs: on a timer, in CI, from a script.