Debugging Ansible
- Debugging ansible is needed when an unexpected state or errors occur.
Display all variables for a host:
tasks:
- name: Display all variables/facts known for a host
ansible.builtin.debug:
var: hostvars[inventory_hostname]
verbosity: 4
Ansible Blocks
- Blocks are a logical groups of tasks. Blocks allow for handling task errors.
tasks:
- name: Task Grouping Name
block:
- name: Task 1 in block
debug:
msg: "Print message about task 1"
- name: Task 2 in block
debug:
msg: "Print message about task 2"
when: ansible_facts['distribution'] == "Debian"
Ansible Conditionals
- Shut down Debian systems
tasks:
- name: Shut down Debian flavored systems
ansible.builtin.command: /sbin/shutdown -t now
when: ansible_facts['os_family'] == "Debian"