How do you loop over a dictionary?
IntermediateConceptJinja2, Conditionals and Loops
Answer
Convert it into a list of key/value items with dict2items.
vars:
sysctl_settings:
vm.swappiness: 10
net.core.somaxconn: 4096
tasks:
- ansible.posix.sysctl:
name: "{{ item.key }}"
value: "{{ item.value }}"
sysctl_set: true
loop: "{{ sysctl_settings | dict2items }}"
The reverse is items2dict. Prefer a list of dicts in your data model when you need to loop in several places.
More on Jinja2, Conditionals and Loops
What role does Jinja2 play in Ansible?BeginnerWhat are the three Jinja2 delimiters?BeginnerShow a template with a loop and a conditional, and how it is deployed.BeginnerWhich filters do you use every day?BeginnerHow do you transform lists and dicts with filters (`map`, `select`, `selectattr`, `unique`, `flatten`)?IntermediateHow does `when` work and which operators can you use?Beginner