# Using dictionary keys as a list in with\_items

**URL:** <https://forum.ansible.com/t/using-dictionary-keys-as-a-list-in-with-items/12824>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [May 17, 2013, 7:51pm UTC](https://forum.ansible.com/t/using-dictionary-keys-as-a-list-in-with-items/12824 "2013-05-17T19:51:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [May 17, 2013, 7:51pm UTC](https://forum.ansible.com/t/using-dictionary-keys-as-a-list-in-with-items/12824/1 "2013-05-17T19:51:31Z")

</div>

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:
> 
> &nbsp;&nbsp;&nbsp;- name: build info on guests  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: virt command=info  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: vminfo
> 
> In a followup task I want to add running guests to a group:
> 
> &nbsp;&nbsp;&nbsp;- name: add running guests to new group  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: add\_host hostname=${item} groupname=running\_vms  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: $vminfo  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;&nbsp;&nbsp;'{{ 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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 17, 2013, 8:17pm UTC](https://forum.ansible.com/t/using-dictionary-keys-as-a-list-in-with-items/12824/2 "2013-05-17T20:17:41Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [May 18, 2013, 11:34pm UTC](https://forum.ansible.com/t/using-dictionary-keys-as-a-list-in-with-items/12824/3 "2013-05-18T23:34:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mischa\_ter\_Smitten](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Mischa\_ter\_Smitten](https://forum.ansible.com/u/Mischa_ter_Smitten)\
**Post date:** [July 30, 2015, 10:19am UTC](https://forum.ansible.com/t/using-dictionary-keys-as-a-list-in-with-items/12824/4 "2015-07-30T10:19:36Z")

</div>

Thanks for the great tip!

with\_items: “{{ databases.keys() }}”

also seems to work, but then you would have no access to the values 🙂

---

<div class="post-metadata">

**Author:** ![Mircea\_Vutcovici](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Mircea\_Vutcovici](https://forum.ansible.com/u/Mircea_Vutcovici)\
**Post date:** [February 18, 2016, 8:26pm UTC](https://forum.ansible.com/t/using-dictionary-keys-as-a-list-in-with-items/12824/5 "2016-02-18T20:26:15Z")

</div>

It is possible to have access to the value. For you case you can use: {{ database[item] }}
