Skip to content

lemano ansible codify

Proposes the edits of your roles that keep each server as it is. It runs the same check as lemano ansible check, with the same arguments and the same safety scan, then, for each object that is drifted, missing or forgotten, shows what changed, who changed it and when, the content diff, and where the edit would go. The guide walks through a first run.

lemano ansible codify PLAYBOOK... [-i INVENTORY]... [--limit PATTERN]
                      [--only NAME]... [--skip NAME]...
                      [--out FILE | --write [--auto-approve]] [--with-secrets]
                      [--allow-unsafe-tasks] [--no-refresh] [ansible-playbook flags]

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

Use it when the server is right: a fix made by hand during an incident, a package installed and kept, a tuned setting. When the role is right, put the server back instead: the last line prints your own ansible-playbook command for that, and, for each host whose history has a recorded Ansible run, lemano revert TARGET --to N to the generation that run wrote.

Output

Nothing is written unless you ask:

  • --out FILE writes a patch: a unified diff with paths relative to the project’s folder (the folder of the first playbook), under a/ and b/. Apply it with git apply FILE or patch -p1 < FILE, or read it and edit by hand. The findings come first, as plain text, which both tools skip. An existing file is refused.
  • --write edits the files in place after one approval: type yes. --auto-approve skips the question.

Before anything is written, the edits are applied to a copy of the project and ansible-playbook --syntax-check must pass on it.

Lemano runs no version control command and reads no version control state: what you do with the patch or the edited files is yours.

Narrowing

--only NAME keeps only the findings named, --skip NAME leaves them out. Both repeat. A name is the object’s Kind/identity (Package/htop, File//etc/app.conf) or its identity alone (htop, /etc/app.conf). Findings left out are counted on one line, and the last line says how to put the servers back for them.

Where each edit goes

FindingEdit
A task argument, written as a literal: a file’s owner, group or mode; a service’s enabled or state; an account’s shell, home or groups; a kernel setting’s valueThe value in the task, at its file and line, written as the task writes its own (yes/no or true/false, 0644 or 644)
The same, written as {{ variable }}The variable where it is defined (see below)
A package version the task pins (nginx=1.24.0-1 or nginx-1.24.0-1.el9, as a name or an item of the list of names, or a variable holding it)The pin, set to the version the server runs
The content of a copy task with src:The file in the role’s files/, replaced by the server’s
The content of a template taskThe template’s changed lines, when they are literal text; a line text {{ variable }} text changes the variable
The managed line of a lineinfile task (state present)The task’s line:, set to the line the task’s regexp matches on the server (the line itself when the task has no regexp), where line is defined: in the task, or the variable it names
A lineinfile task with state: absent whose line is on the serverstate: present and the server’s line (line: is added when the task has none); the preview offers removing the task as the alternative
The managed block of a blockinfile taskThe task’s block:, set to the lines between the markers on the server (marker, marker_begin and marker_end, with Ansible’s defaults), in its own block scalar or in the variable it names. When the hosts hold different blocks, or hosts outside the check run the task, each checked host’s block goes to its host_vars/HOST.yml as a block scalar with the server’s text, as for any other value
A missing object, on every checked hostThe task is removed, or its item from the task’s list of names or its loop:
A forgotten packageA task added to the role that already installs packages, else to a new role packages added to the first playbook
A forgotten file in /etcA copy task, and the file in files/, added to the role that manages files in the same folder or the service named after it (an sshd drop-in goes to the role that manages sshd), else to a new role files
A forgotten service, account, group, authorized key, sudo rule, cron entry, kernel setting, mount or firewall ruleThe tasks lemano ansible export writes for it, with their values written into the tasks, added to the role that already manages that kind of object (a service’s to the role that manages the service), else to a new role named as the export names it; handlers and files come with them. ufw is enabled by a task only when every ufw rule and default policy of the host is in your roles

A variable is looked for where Ansible would find it, from the highest precedence down: the role’s vars/main.yml, the project’s host_vars/HOST.yml, the project’s group_vars/*.yml (all last), the role’s defaults/main.yml. With ansible-core 2.19 or later, Ansible tags a task’s resolved arguments with the file and line of the variable they came from; the callback plugin sends that place, and Lemano reads the file at that line and uses it only when the key there holds the value the task used (“where Ansible says it was defined, confirmed”). A value is changed where it is found only when that definition holds the value the task used. A value set per host keeps the type of the value it overrides: a number stays a number, a boolean a boolean.

A value is set per host, in host_vars/HOST.yml, when:

  • it differs between the checked hosts that run the task;
  • hosts outside the check also run the task: a run with --limit, or hosts of the play the run did not reach. Every host each play targets, whatever the limit, is read from the inventory with Ansible’s own loader; the preview names the hosts left alone (“also used by web2: not changed there”);
  • it is defined nowhere Lemano looks (set_fact, extra vars, an inventory plugin), in several group_vars files, or the definition found is not the value the task used: the preview says so with a note. A host_vars value wins over role defaults, group_vars and inventory values, but not over extra vars or set_fact.

Listed, not patched

These findings are shown with their diff and the reason, and nothing is edited for them:

  • the value is a Jinja expression other than one variable;
  • the task belongs to a role installed outside the project’s folder (from Galaxy or a collection): the next install would overwrite the edit;
  • a literal value that differs between the hosts that run the task;
  • an object missing on some of the hosts that run the task only, or a task hosts outside the check also run;
  • a forgotten object, when hosts outside the check also run the plays of the role it would go to; a new role goes to the first play that lists roles and whose hosts were all checked, written in that play’s style (- users or - role: users);
  • a forgotten object on some of the checked hosts only, or whose content differs between them;
  • a template whose lines do not render one for one (control blocks, comments), or a changed line holding an expression;
  • a file whose content Lemano does not show (see below), for a drifted copy or template;
  • a lineinfile task whose regexp matches no line of the server’s file any more, or several lines, or that uses backrefs; a blockinfile task whose markers are not on the server exactly once each, whose block is empty on the server, or whose block is a literal of the task and differs between the hosts (make it a variable, then run codify again); a regexp, marker or line that is an expression other than one variable; a host_vars file that already sets the variable as a value on one line, or holds no YAML mapping to add to;
  • the lines of a file whose content is not shown: “the file may hold secrets: change it by hand”. With --with-secrets the value is written as an Ansible Vault value, in the task or in the variable it names, and for each host in host_vars when the hosts hold different values;
  • a drifted cron entry or mount, a package version given by a version argument, and the arguments not listed above: change them by hand. Cron commands and mount options can hold secrets that Lemano keeps only as keyed hashes.

Editing your files

Codify replaces only the text of the value it changes, removes only the lines of the item it removes, and adds tasks or keys after the last entry of a list or mapping, at its indentation. It never writes a file back from its parsed form: comments, quoting, indentation and key order stay as you wrote them. A comment next to an edited value keeps its old words, so review the comments next to the values codify changed (for example mode: '0600' # read by every user keeps its comment). Each edited YAML file is parsed again and must differ from the original by the intended changes alone. A block scalar (| or >, with any chomping indicator) is replaced under its own header, indentation and comment; one with an indentation indicator, a value spread over several lines in another style, and flow lists ([a, b]) are not edited.

Secrets

Codify runs ansible-playbook --check --diff, whose output goes to standard error. Ansible prints each task’s diff, except for tasks marked diff: false (or no_log: true): mark the tasks that write secrets that way. Lemano’s own output and the patch follow the rules below.

The content rules are those of lemano diff (see security model): the content of a file is shown, and written, only when every user on the host can read it, it is not a file of secrets (crontabs, sudoers files, fstab, authorized keys), it is text and it is stored. The content of a drifted file that is not shown is still compared in memory with the role’s side: the finding says “content changed (not shown: …)” when it differs, “content not compared (not shown: …)” when one side is not known, and nothing when it is the same (contentChanged in JSON is true, absent or false). A forgotten file whose content is not shown is added with its content as a variable left undefined ({{ undef(hint=...) }}) in the role’s defaults/main.yml: set it yourself. With --with-secrets, it is written as an Ansible Vault value instead, and the file a drifted copy task copies is replaced by a Vault-encrypted file (the copy module decrypts it). The Vault password is the one given to ansible-playbook with --vault-password-file, else asked twice without echo. Values Lemano holds only as keyed hashes are never written. Nothing secret is printed, written to the patch or logged.

For a template task, the content Ansible would write comes from its check-mode diff. The callback plugin keeps it only for files whose content the rules above let Lemano show, and Lemano compares it in memory; it is never stored. In the same way, for lineinfile and blockinfile tasks the plugin keeps line, regexp, block and the markers only for those files. Lines of the file that no task manages are neither compared nor reported.

Exit codes

CodeMeaning
0Done (a patch written, the files edited, or the preview alone)
1An error, including a project that fails --syntax-check after the edits
2Unsafe tasks refused, a usage error, an existing --out file, or the edits not approved
7A host could not be read or compared
3, 4, 5As for lemano ansible check: by the reason a host’s fresh reading failed, 4 for a host key that is not pinned (give --accept-new-host-key) or changed