๐ Ansible Snippets Reference
A curated collection of useful YAML snippets for playbooks, debugging, regex, mapping, and more.
๐ Starters
โถ๏ธ Basic Playbook Start
- name: Some text
hosts: "{{ host | default('all') }}"
become: true
gather_facts: no
ignore_unreachable: true
๐งฉ Localhost + Dynamic Hosts
- name: Login
hosts: localhost
vars:
ovirt_url: https://cent-engine.qrom.nl/ovirt-engine//api
ovirt_user: 'admin@ovirt@internalsso'
ovirt_passwd: 'XxX'
tasks:
- name: Get SSO token
ovirt.ovirt.ovirt_auth:
url: "{{ ovirt_url }}"
username: "{{ ovirt_user }}"
password: "{{ ovirt_passwd }}"
- name: Add dynamic hosts
ansible.builtin.add_host:
name: "{{ item }}"
loop:
- ichi.zilux.nl
- ni.zilux.nl
- name: Do stuff
hosts: all
tasks:
- name: Debug message
ansible.builtin.debug:
msg: From second play in playbook
๐ Regex & Search
๐ Retry Until Match
- name: Retry until 'OnDemand' appears
ansible.builtin.shell: some command
register: cmd_res
retries: 10
until: cmd_res.stdout_lines | regex_search('OnDemand', multiline=True, ignorecase=True)
๐ Find Substring
- name: Check for reboot requirement
debug:
msg: "{{ sut_verify.stdout }}"
when: cmd.stdout.find('Requires reboot') != -1
๐ Debugging
๐ง Dump All Variables
- name: Dump all
hosts: all
tasks:
- name: Print debug info
vars:
msg: |
{{ vars | to_nice_json }}
{{ environment | to_nice_json }}
{{ group_names | to_nice_json }}
{{ groups | to_nice_json }}
{{ hostvars | to_nice_json }}
debug:
msg: "{{ msg.split('\n') }}"
tags: debug_info
- name: Show hostvars
debug:
var: hostvars[inventory_hostname]
tags: debug_info
๐ Show Facts
- name: Show facts
hosts: localhost
gather_facts: true
tasks:
- name: Show ansible_facts
debug: var=ansible_facts
- name: Show FQDN
debug:
msg:
- "ansible_fqdn: {{ ansible_fqdn }}"
- "ansible_facts.fqdn: {{ ansible_facts.fqdn }}"
- "default_ipv4: {{ ansible_default_ipv4.address }}"
๐งฎ Mapping Examples
๐บ๏ธ Map Attributes
- name: Map examples
hosts: all
vars:
names:
- first: Paul
last: Thompson
ctc: 100000
address: { city: LasVegas, country: USA }
- first: Sarav
last: AK
ctc: 200000
address: { city: Chennai, country: India }
tasks:
- debug: msg="{{ names | map(attribute='last') | map('upper') }}"
- debug: msg="{{ names | map(attribute='ctc') | map('float') }}"
- debug: msg="{{ names | map(attribute='ctc') | product(['USD']) | map('join',' ') }}"
๐ Map with Zip
- name: Map with zip
hosts: all
vars:
only: /boot
tasks:
- name: Display mounts
debug:
msg: "mount = {{ item.0 }} with device = {{ item[1] }}"
loop: "{{ ansible_mounts | map(attribute='mount') | zip(ansible_mounts | map(attribute='device')) }}"
when: item.0 == only
๐ Subelement Examples
- name: Loop with subelements
hosts: localhost
vars:
users:
- name: jan
mysqlhosts:
- host: hosta
port: 8080
- name: harry
mysqlhosts:
- host: hostb
port: 8090
- host: hostc
port: 8190
tasks:
- name: With subelements
debug:
msg: "{{ item.0.name }} - {{ item.1.host }} - {{ item.1.port }}"
loop: "{{ users | subelements('mysqlhosts') }}"
๐งช Selectattr Examples
- name: Selectattr examples
hosts: localhost
vars:
bands:
- name: The Beatles
members: [Lennon, McCartney, Harrison, Starr]
formed: 1960
decade: 60s
- name: Run DMC
members: [Simmons, McDaniels, Mizell]
formed: 1983
decade: 80s
tasks:
- name: Bands formed in 1983
debug:
msg: "{{ bands | selectattr('formed', 'match', '1983') | map(attribute='name') }}"
๐งช OS Update & Reboot
๐ Update OS
- hosts: all
become: yes
tasks:
- name: Upgrade packages
yum:
name: '*'
state: latest
update_cache: yes
exclude:
- docker-ce
- cassandra
๐ Reboot Server
- name: Reboot server
ansible.builtin.reboot:
msg: "Server reboot via Ansible"
connect_timeout: 5
reboot_timeout: 600
pre_reboot_delay: 60
post_reboot_delay: 1200
when: reboot_select == "yes"
๐ฆ Variable Definitions
- name: Define vars
hosts: localhost
vars:
mynlijst: ['een', 'twee']
mynlistofhash:
- { naam: harry, adres: hier }
- { naam: piet, adres: daar }
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
tasks:
- debug: var=users.alice.telephone
- name: Loop over dictionary
debug:
msg: "{{ item.key }} is {{ item.value.name }} {{ item.value.telephone }}"
loop: "{{ users | dict2items }}"