How to iterate over hash of arrays

14 views
Skip to first unread message

Dhaval Joshi

unread,
Feb 13, 2017, 9:07:12 AM2/13/17
to Ansible Project
Hello,

I have below strucure.

vars:
 locations:
  - parent_location: A
    child_location:
     - a1
     - a2
     - a3
 - parent_location: B
   child_location:
    - b1
    - b2
    - b3
 - parent_location: C
   child_location:
    - c1
    - c2
    - c3

now i have task something like below

command:
  some-command '{{ A }}' create

command:
   some-command '{{ a1 }}' --parent '{{ A }}'

command:
   some-command '{{ b1 }}' --parent '{{ B }}'

and so-on, what's best way to achieve it ?

Thanks,
DJ

Johannes Kastl

unread,
Feb 13, 2017, 9:24:24 AM2/13/17
to ansible...@googlegroups.com
Read the docs? ;-)
http://docs.ansible.com/playbooks_loops.html
https://docs.ansible.com/ansible/playbooks_loops.html#looping-over-hashes
https://docs.ansible.com/ansible/playbooks_loops.html#nested-loops

Something along the lines of:

command: some-command "{{ item.parent_location }}" create
with_dict: "{{ locations }}"

I guess for the child_locations you might need to adapt with_nested
somehow.

Johannes

signature.asc
Reply all
Reply to author
Forward
0 new messages