Ansible playbook -read array variables from external file recursively

588 views
Skip to first unread message

Saravanan Ponnusamy

unread,
Aug 26, 2018, 3:13:43 PM8/26/18
to Ansible Project

Variables declared as an array in an external file. Need to read these variables recursively in the playbook during play for multiple records. Request to suggest right looping to use in Ansible playbook.

Currently able to get the item and not item.[variable].


Playbook:

---
- hosts: 127.0.0.1
  gather_facts: no

  tasks:
    - debug:
        var: "{{ item }}"
      with_lines: cat array.yaml


$ cat array.yaml

    - { name: user1, uid: 1001, env: dev, app: app1 }
    - { name: user2, uid: 1002, env: cit, app: app2 }

Kai Stian Olstad

unread,
Aug 26, 2018, 4:38:04 PM8/26/18
to ansible...@googlegroups.com
You need to put them in a dictionary, then you can do this.

array.yml
---
myvar:
- { name: user1, uid: 1001, env: dev, app: app1 }
- { name: user2, uid: 1002, env: cit, app: app2 }


tasks:
- include_vars: array.yml

- debug: msg="{{ item.name }} {{ item.uid }} {{ item.env }} {{ item.app }}"
with_items: '{{ myvar }}'


--
Kai Stian Olstad


Saravanan Ponnusamy

unread,
Aug 26, 2018, 6:38:22 PM8/26/18
to Ansible Project
It works. Thanks a lot  Kai Stian Olstad.
Reply all
Reply to author
Forward
0 new messages