Commands / Services and tunnels

Services and tunnels

ansible-playbook

Also playbook, check mode, dry run, idempotence, drift

Runs a playbook: brings every host to the state it describes, or with --check reports what it would change.

A playbook (YAML) lists tasks that describe a state, not steps: template renders a Jinja2 file from the host's variables and changes the target only if it differs. Run twice, a good playbook reports changed=0 the second time (idempotence). --check changes nothing and reports what would change, --diff shows the lines, so together they find drift: a device that no longer matches its intended config. The PLAY RECAP sums up each host: ok, changed, unreachable, failed.

ansible-playbook command usage

ansible-playbook PLAYBOOK [--check] [--diff] [--limit HOST]

Options

--check
dry run: report what would change, change nothing (handlers are skipped)
--diff
show the lines each change would add (+) or remove (-)
--limit HOST
run on these hosts only
-v
more detail on each task

ansible-playbook examples

ansible-playbook /etc/ansible/site.yml --check --diff
Shows, line by line, what the playbook would change on each router, changing nothing.
ansible-playbook /etc/ansible/site.yml
Pushes the intended config and prints a recap with changed= for each router.

Pitfalls

  • changed=0 means the devices match the files, not that the files are right: a typo is pushed as faithfully as the fix.
  • A task built from shell commands is only idempotent if it compares before it changes (changed_when).

Run it on real machines

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Start with a free account: Google or email, no card.

Open this lab