Re: [ansible-project] Using dictionary keys as a list in with_items

19,835 views
Skip to first unread message

Michael DeHaan

unread,
May 17, 2013, 3:00:10 PM5/17/13
to ansible...@googlegroups.com
You should use the wait_for module to wait on the guests until they are running.




On Fri, May 17, 2013 at 9:20 AM, Rik Theys <rik....@gmail.com> wrote:
Hi,

I'm creating a playbook in which I register a variable to hold the virt info output:

   - name: build info on guests
     action: virt command=info
     register: vminfo

In a followup task I want to add running guests to a group:

   - name: add running guests to new group
     action: add_host hostname=${item} groupname=running_vms
     with_items: $vminfo
     only_if: "'${item.status}' == 'running'"

This fails because with_items expects a list and I'm providing a dictionary.

Is there a way to make with_items use the keys of the dictionary as list items?

Regards,

Rik

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

C. Morgan Hamill

unread,
May 17, 2013, 3:51:31 PM5/17/13
to ansible-project
Excerpts from Rik Theys's message of 2013-05-17 09:20:26 -0400:
> Hi,
>
> I'm creating a playbook in which I register a variable to hold the virt
> info output:
>
> - name: build info on guests
> action: virt command=info
> register: vminfo
>
> In a followup task I want to add running guests to a group:
>
> - name: add running guests to new group
> action: add_host hostname=${item} groupname=running_vms
> with_items: $vminfo
> only_if: "'${item.status}' == 'running'"
>
> This fails because with_items expects a list and I'm providing a dictionary.
>
> Is there a way to make with_items use the keys of the dictionary as list
> items?

I know this works in normal jinja2 templates:

'{{ item.keys() }}'

So that might work in Ansible 1.2 (devel from git).

If you need something more complicated, you could always write a filter
plugins, which I'm fairly sure works with the jinja-style substitutions.
--
C. Morgan Hamill
<cha...@wesleyan.edu>

Michael DeHaan

unread,
May 17, 2013, 4:17:41 PM5/17/13
to ansible...@googlegroups.com
Aside:  if using 1.2 you should not be using only_if.

It's a legacy syntax that requires a lot of confusing quoting, and uses the variable system that is unlike templates, where we are consolidating now.

The new way is just this, much simpler:

when: foo.status == 'running'




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Kahlil Hodgson

unread,
May 18, 2013, 7:34:14 PM5/18/13
to ansible...@googlegroups.com
Here's and example using the Jinja2's dictsort filter to turn a dictionary into a list of (key,value) pairs which you can than iterate over with with_items:

vars:
    databases:
      service:
        user:  service_app
        pass:  'secret'

      website:
        user:  website_app
        pass:  'secret'

      brand_demo1:
        user:  demo1_app
        pass:  'secret'

tasks:
    - name: setup database users
      postgresql_user: name={{ item.1.user }} password={{ item.1.pass }}
      sudo_user: postgres
      with_items: databases|dictsort


Hope this helps.

Kal


Kahlil (Kal) Hodgson                       GPG: C9A02289
Head of Technology                         (m) +61 (0) 4 2573 0382
DealMax Pty Ltd                            (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing.  You must remember that
the parts you are reassembling were disassembled by you.  Therefore,
if you can't get them together again, there must be a reason.  By all
means, do not use a hammer."  -- IBM maintenance manual, 1925

Mischa ter Smitten

unread,
Jul 30, 2015, 6:19:36 AM7/30/15
to Ansible Project, kahlil....@dealmax.com.au
Thanks for the great tip!

with_items: "{{ databases.keys() }}"

also seems to work, but then you would have no access to the values :-)

Mircea Vutcovici

unread,
Feb 20, 2016, 11:35:15 AM2/20/16
to Ansible Project, kahlil....@dealmax.com.au

It is possible to have access to the value. For you case you can use: {{ database[item] }}
Reply all
Reply to author
Forward
0 new messages