Ansible tips & tricks
Ansible data types
integer: 3211241
boolean: true/false
float: 3.141592653
string: "qwerty"
list:
- "item 1"
- "item 2"
dictionary:
key1: "value 1"
key2: "value 2"
Dump all variables
---
- name: dump all
hosts: all
tasks:
- name: Print some debug information
vars:
msg: |
Module Variables ("vars"):
--------------------------------
{{ vars | to_nice_json }}
Environment Variables ("environment"):
--------------------------------
{{ environment | to_nice_json }}
GROUP NAMES Variables ("group_names"):
--------------------------------
{{ group_names | to_nice_json }}
GROUPS Variables ("groups"):
--------------------------------
{{ groups | to_nice_json }}
HOST Variables ("hostvars"):
--------------------------------
{{ hostvars | to_nice_json }}
debug:
msg: "{{ msg.split('\n') }}"
manipulating data
Extract subitems from list of dictionaries
Use "{{ variable | map(attribute='keyname') | list }}"
Select items from list of dictionaries
Use "{{ variable | selectattr('keyname', 'match', 'value') | list }}"
Instead of match, you can also use search or regex. See:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings
Sort list of dictionaries by subitem
Use "{{ variable | sort(attribute='keyname') }}"
--
Ivo van Geel - 21 Sep 2021