Ansible loops over lists and array mixin

ansible

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?

Best Answer

According to your comments you want to create directories for obj.services.#.dirs.tmpdir items with details from obj.metadata. The solution with width_items: "{{ object.services }}" is nearly working - but you don't have access to obj.metadata. The thing is - there is only one obj.metadata at all and so you don't need to refer to it in the loop. You can use it directly.

- name: Creating directories for server and client mode.
  file:
    path: "{{ item.dirs['tmpdir'] }}"
    state: directory
    owner: "{{ obj.metadata.uuid }}"
    group: "{{ obj.metadata.uuid }}"
    mode: 0750
  with_items:
    - "{{ obj.services }}"