I'm currently trying to work on a task which will create directories for two differents services but I've trouble solving how am I suppose to do that using ansible loops.
Here is the object:
obj:
metadata:
uuid:
version:
services:
- server:
archive: binary.tar.gz
dirs:
bindir: /path/to/bindir/
confdir: /path/to/confdir/
tmpdir: /path/to/tmpdir/
- client:
archive: binary.tar.gz
dirs:
tmpdir: /path/to/tmpdir
And here is my associated task:
#Creating directories
- name: Creating directories for server and client mode.
file:
path: "{{ item.1.dirs['tmpdir'] }}"
state: directory
owner: "{{ item.0.metadata.uuid }}"
group: "{{ item.0.metadata.uuid }}"
mode: 0750
with_subelements:
- "{{ obj }}"
- services
Now, I'm a little bit disturbed by how loops works on ansible and a little bit stick for now with my current iteration, so, could you help me to find a way to achieve what I'm trying to do?
According to your comments you want to create directories for
obj.services.#.dirs.tmpdir
items with details fromobj.metadata
. The solution withwidth_items: "{{ object.services }}"
is nearly working - but you don't have access toobj.metadata
. The thing is - there is only oneobj.metadata
at all and so you don't need to refer to it in the loop. You can use it directly.