norboten · cheat sheet

A playbook describes a state, not a sequence of commands — cheat sheet

ansible, boot-systemd
$ ansible-playbook site.yml --check --diff          # preview; watch changed AND skipped
$ ansible-playbook site.yml                          # apply
$ ansible-playbook site.yml | tail -n 3              # the recap: a second run should be changed=0
$ ansible-playbook site.yml --list-tasks             # what would run, without running it
$ ansible-playbook site.yml --start-at-task "Configuration"
$ ansible app -m ansible.builtin.debug -a var=app_port     # a variable from the inventory alone
$ ansible-inventory --host localhost                 # all inventory variables for a host
$ ansible-doc ansible.builtin.copy                   # a module's parameters and examples
$ /usr/sbin/sysctl net.ipv4.ip_forward               # the running value
$ sudo sysctl -p /etc/sysctl.d/90-app.conf           # apply one file now
$ systemctl show app -p ActiveEnterTimestamp         # did the service restart?
# a command that converges
- ansible.builtin.command: /usr/local/bin/init-db
  args: {creates: /var/lib/db/.initialised}
# a command that reports honestly
- ansible.builtin.command: some-tool status
  register: out
  changed_when: false
# restart only on change
- ansible.builtin.template: {src: app.conf.j2, dest: /etc/app/app.conf}
  notify: Restart app