Skip to content
Codifying into roles

Keeping your roles true to your servers

lemano ansible check shows where a server and your roles disagree. For each finding, one side is right:

  • The role is right: someone changed the server by mistake, or should not have. Run your playbook, and the server is put back.
  • The server is right: a fix made by hand during an incident, a package installed and kept, a setting tuned. The role must say so, or the next playbook run undoes the fix.

lemano ansible codify is for the second case: it proposes the edits of your roles that keep the servers as they are.

A first run

lemano ansible codify site.yml -i inventory/production --limit web1

It runs the check (the same safety scan, a fresh read of each host, your playbook in check mode), then prints each finding:

lemano ansible: web1 checked against site.yml: 5 drifted, 1 missing, 2 forgotten

1. drifted   File//etc/app/app.conf (role app) on web1
   changed since the baseline, by an unknown hand
   @@ -1,3 +1,3 @@
    # The app's settings.
   -port = 8080
   -workers = 4
   +port = 9090
   +workers = 8
   edit: roles/app/defaults/main.yml:3 app_port (defined there, confirmed)
   edit: roles/app/templates/app.conf.j2:3 (a literal line)
2. drifted   File//etc/app/banner.txt (role app) on web1
   changed since the baseline, by an unknown hand
   @@ -1 +1 @@
   -Welcome.
   +Welcome back.
   edit: roles/app/files/banner.txt (the file the task copies, replaced)
3. drifted   File//etc/app/notes.txt (role app) on web1
   changed since the baseline, by an unknown hand
   ~ mode: 0644 -> 0600
   edit: roles/app/tasks/main.yml:24 mode (a literal)
4. missing   File//etc/app/extra.conf (role app) on web1
   edit: remove the task roles/app/tasks/main.yml:26 (missing on every checked host)
5. drifted   File//etc/app/computed.conf (role app) on web1
   changed since the baseline, by an unknown hand
   ~ mode: 0644 -> 0600
   not patched: mode is a Jinja expression ({{ app_base_mode | default('0644') }}): change it by hand
6. drifted   File//etc/app/vendor.conf (role vendor) on web1
   changed since the baseline, by an unknown hand
   ~ mode: 0644 -> 0600
   not patched: the role is installed outside the project (from Galaxy or a collection): its files are never edited, since the next install would overwrite them
7. forgotten File//etc/app/local.conf on web1
   changed since the baseline, by an unknown hand
   edit: roles/app/files/local.conf (the server's file)
   edit: add to roles/app/tasks/main.yml (the role that manages /etc/app)
8. forgotten Package/cronie-anacron on web1
   changed by alice 2 hours ago (dnf)
   edit: add to roles/packages/tasks/main.yml (a new role)

not patched: 2
to put the servers back instead, for what the roles keep: ansible-playbook -i inventory/production site.yml --limit web1
nothing written: add --out FILE to write a patch, or --write to edit the files

The content diff goes from what the role would write to what the server holds, with the rules of lemano diff: a file only some users can read, or that may hold secrets is not shown, but it is still compared in memory: the line says “content changed (not shown: …)” when it differs, “content not compared (not shown: …)” when one side is not known (a template’s rendered content is given only for files whose content is shown), and nothing when the content is the one the role writes.

Taking only some findings

Codify proposes every finding by default. Leave some out with --skip, or keep only some with --only (both repeat; a name is Kind/identity or the identity alone):

lemano ansible codify site.yml -i inventory/production --limit web1 \
    --skip /etc/app/notes.txt --out web1.patch

The findings you leave out are those where the role is right: the last line prints your playbook command that puts the servers back.

Ansible prints its own check-mode diff of each task while codify runs, except for tasks marked diff: false (or no_log: true): mark the tasks that write secrets that way.

Applying the edits

--out web1.patch writes a patch. The findings come first, as plain text, then the diff of each file, with paths relative to the folder of your playbook:

git apply web1.patch      # or: patch -p1 < web1.patch

--write edits the files in place after you type yes (--auto-approve skips the question). Either way, Lemano first applies the edits to a copy of the project and checks it with ansible-playbook --syntax-check. It never runs a version control command: commit, branch or review the change as you do with any other.

Then check again: what you codified is in sync.

lemano ansible check site.yml -i inventory/production --limit web1

What codify does not touch

  • Your formatting. Only the text of the changed value, or the lines of the removed item, change; comments and quoting stay. A comment next to an edited value keeps its old words, so review it (for example mode: '0600' # read by every user keeps its comment).
  • Roles installed from Galaxy or a collection (outside your project’s folder): the next install would overwrite them. Their findings are listed, with the diff.
  • Expressions. A value written as an expression, or a template line with one, is listed for you to change.
  • Values that differ between hosts and are written as literals: make them a variable, then run codify again; it sets each host’s value in host_vars/.

The reference lists where each kind of edit goes and every case left to you.

Next