register, debug & assert
Three small modules make playbooks observable and safe: register captures a task's result, debug prints things, and assert stops the run when an assumption is wrong.
register
Every task returns a dictionary. register: name stores it as a variable for the current host. What's in it depends on the module; command and shell give you stdout, stdout_lines, stderr, rc, and every module adds changed and failed:
cat > reg.yml <<'YML'
- name: Register results
hosts: localhost
gather_facts: false
tasks:
- name: List some numbers
ansible.builtin.command: seq 3
register: nums
changed_when: false
- name: Look at the whole result
ansible.builtin.debug:
var: nums
- name: Use parts of it
ansible.builtin.debug:
msg: "rc={{ nums.rc }}, lines={{ nums.stdout_lines }}, sum={{ nums.stdout_lines | map('int') | sum }}"
YML
ansible-playbook reg.yml | sed -n '/TASK \[Look/,$p' | grep -vE '"(start|end|delta)"'TASK [Look at the whole result] ************************************************
ok: [localhost] => {
"nums": {
"changed": false,
"changed_when_result": false,
"cmd": [
"seq",
"3"
],
"failed": false,
"msg": "",
"rc": 0,
"stderr": "",
"stderr_lines": [],
"stdout": "1\n2\n3",
"stdout_lines": [
"1",
"2",
"3"
]
}
}
TASK [Use parts of it] *********************************************************
ok: [localhost] => {
"msg": "rc=0, lines=['1', '2', '3'], sum=6"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(The start, end and delta timestamps of the command were filtered out of this transcript, because they differ on every run.)
With a loop, the registered variable has a results list with one entry per item; see Loops.
debug
| Form | Use |
|---|---|
msg: "text {{ var }}" |
a message, a list of lines, or any structure |
var: some.variable |
print one variable with its name (no braces) |
verbosity: 2 |
print only when run with -vv, so normal runs stay quiet |
assert: fail early and clearly
An assertion turns a silent wrong assumption into a readable failure before anything is changed. Put assertions about input variables at the top of a play or role:
cat > check.yml <<'YML'
- name: Validate inputs
hosts: localhost
gather_facts: false
vars:
app_env: prod
app_replicas: 1
tasks:
- name: Inputs are sane
ansible.builtin.assert:
that:
- app_env in ['dev', 'staging', 'prod']
- app_replicas | int >= 1
- app_env != 'prod' or app_replicas | int >= 2
fail_msg: "prod needs at least 2 replicas, got {{ app_replicas }}"
success_msg: inputs ok
quiet: false
- name: Would deploy
ansible.builtin.debug:
msg: deploying
YML
ansible-playbook check.yml | grep -E '^(ok|fatal|failed)|"msg"|PLAY RECAP' ; echo "exit code: ${PIPESTATUS[0]}"
echo "=== with 3 replicas"
ansible-playbook check.yml -e app_replicas=3 | grep -E '"msg"'fatal: [localhost]: FAILED! => {
"msg": "prod needs at least 2 replicas, got 1"
PLAY RECAP *********************************************************************
exit code: 2
=== with 3 replicas
"msg": "inputs ok"
"msg": "deploying"
that: takes a list of Jinja2 expressions without braces; all must be true. The failing host stops, the PLAY RECAP shows failed=1, and the exit code 2 fails your CI job. Use ansible.builtin.fail with when: for the same effect with one condition.
Assertions as tests
The same module is the backbone of testing in Ansible: Molecule's verify.yml is usually a list of stat/slurp/command tasks with register, followed by asserts on the results (Testing Roles with Molecule).
- name: Read the deployed config
ansible.builtin.slurp:
src: /etc/linkly/app.env
register: env_file
- name: Config has the right port
ansible.builtin.assert:
that: "'APP_PORT=8080' in (env_file.content | b64decode)"Test yourself
Which keys does a registered command result include?
- only
stdout stdout,stdout_lines,stderr,rc,changed, …msgonly- nothing until
debugruns
How are expressions written in assert's that: list?
- With
{{ }} - Without braces, as raw Jinja2 expressions
- As shell tests
- As Python code
What does verbosity: 2 on a debug task do?
- Prints twice
- Prints only when run with
-vvor more - Raises the log level of the module
- Fails on warnings