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:


    - server:
      archive: binary.tar.gz
        bindir: /path/to/bindir/
        confdir: /path/to/confdir/
        tmpdir: /path/to/tmpdir/

    - client:
      archive: binary.tar.gz
        tmpdir: /path/to/tmpdir

And here is my associated task:

#Creating directories
- name: Creating directories for server and client mode.
    path: "{{ item.1.dirs['tmpdir'] }}"
    state: directory
    owner: "{{ item.0.metadata.uuid }}"
    group: "{{ item.0.metadata.uuid }}"
    mode: 0750
    - "{{ 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?

  • Did you check this documentation? – 030 Nov 14 '16 at 12:30
  • Of course I did it :D But I can't found my exemple. – Dr I Nov 14 '16 at 12:36
  • Try with_items: "{{ object.services }}". – Antonis Christofides Nov 14 '16 at 12:41
  • 1
    Also, don't try to be too clever. Readability matters. If all you have is server and client, and not more, just write the same code twice, once for server and one for client. – Antonis Christofides Nov 14 '16 at 12:43
  • 1
    I think that, for a start, you should rename the obj. obj means really nothing. What is that thing? Whenever you have to name something in a meaningless way it is a hint that your code needs to be reorganized. – Antonis Christofides Nov 14 '16 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.