Ansible allows deeply nested dicts, but then doesn't actually allow you to get much use out of them! Here is my story.
I'm trying to configure a number of ini files with Ansible. To this end, I am using the ini_file
module. This module requires at a minimum, four pieces of information:
- File path
- Section
- Option
- Value of that option
So, I've defined a dict:
vars:
conf:
"/etc/conf1.ini":
- section: main
option: foo
val: bar
- section: main
option: baz
val: qux
"/etc/otherconf.ini":
- section: options
option: flower
val: bird
And so on. So, I can use:
- name: Write ini files
ini_file:
path: "{{ item.0.key }}"
section: "{{ item.1.section }}"
option: "{{ item.1.option }}"
value: "{{ item.1.val }}"
loop: "{{ conf | dict2items | subelements('value') }}"
This... works. I can continue to use this. Example output of /etc/conf1.ini
:
[main]
foo = bar
baz = qux
Question: Is there a better way? How can I organize my data to work better with (the new style of) Ansible loops? (i.e. nothing that starts with with_
)
Note: I also really don't like having to use a fully qualified path as a dict key; I'd rather it be e.g. path: "/etc/conf1.ini"
somehow.
Note 2: I don't have to have this all in one single variable, conf
, but if I have one variable per ini file, how do I loop that?