iterate over vars[dict_name] for multiple dicts

25 views
Skip to first unread message

Rajesh Rajendran

unread,
Nov 9, 2017, 9:14:25 PM11/9/17
to Ansible Project
---
- hosts: localhost
  gather_facts: false
  vars:
      neo4j:
        name: neo4j
      api_service:
        name: api_service
        pid: /var/api/pid
  tasks:  
    - debug:
        msg: "{{item.pid|default('/var/run/{{item.name}}/{{item.name}}.pid')}}"
      with_items:
          - "{{ vars[service] }}"

ansible-playbook play.yml --extra-vars service=api-service

how will I debug both dictionaries?
  I know that I can't give 
     --extra-vars service=api-service,neo4j or as a llist

or is there any way to dynamically iterate using user given dictionaries.

Kai Stian Olstad

unread,
Nov 10, 2017, 4:23:39 AM11/10/17
to ansible...@googlegroups.com
On 10.11.2017 03:14, Rajesh Rajendran wrote:
> ---
> - hosts: localhost
> gather_facts: false
> vars:
> neo4j:
> name: neo4j
> api_service:
> name: api_service
> pid: /var/api/pid
> tasks:
> - debug:
> msg:
> "{{item.pid|default('/var/run/{{item.name}}/{{item.name}}.pid')}}"

You cant't nest {{ }}


> with_items:
> - "{{ vars[service] }}"
>
> ansible-playbook play.yml --extra-vars service=api-service
>
> how will I debug both dictionaries?
> I know that I can't give
> --extra-vars service=api-service,neo4j or as a llist
>
> or is there any way to dynamically iterate using user given
> dictionaries.

You could do something like this

- debug:
msg: "{{ vars[item].pid | default('/var/run/' ~ vars[item].name ~
'/' ~ vars[item].name ~ '.pid')}}"
with_items:
- "{{ service.split(',') }}"

And then you can use a comma separated values in --extra-vars

--
Kai Stian Olstad

Rajesh Rajendran

unread,
Nov 10, 2017, 6:32:00 AM11/10/17
to Ansible Project
Thank you, dude.
It worked perfectly.
Reply all
Reply to author
Forward
0 new messages