# How to write an Ansible task using with\_dict in a loop (with\_items)?

**URL:** <https://forum.ansible.com/t/how-to-write-an-ansible-task-using-with-dict-in-a-loop-with-items/16878>\
**Category:** Ansible Project\
**Created:** [November 25, 2014, 4:10pm UTC](https://forum.ansible.com/t/how-to-write-an-ansible-task-using-with-dict-in-a-loop-with-items/16878 "2014-11-25T16:10:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Louis\_FEREY](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Jean-Louis\_FEREY](https://forum.ansible.com/u/Jean-Louis_FEREY)\
**Post date:** [November 25, 2014, 4:10pm UTC](https://forum.ansible.com/t/how-to-write-an-ansible-task-using-with-dict-in-a-loop-with-items/16878/1 "2014-11-25T16:10:32Z")

</div>

I want to update INI configuration files.

Today, I store my informations in a var file (in group\_vars) this way :

```auto

# Identity configuration information
identity_servers_conf:
  DEFAULT:
    admin_token: "{{identity_admin_token}}"
    verbose: True
  database:
    connection: "mysql://{{ identity_db_user }:{{ identity_db_password }}@{{ db_lb_name }}/{{ identity_db }}"    
  token:
    provider: keystone.token.providers.uuid.Provider
    driver: keystone.token.persistence.backends.sql.Token  

In my Ansible task, I use these informations this way:

```

```auto
- name: configuration / modify keystone.conf ini file DEFAULT section
  ini_file:
    section: DEFAULT
    dest: /etc/keystone/keystone.conf
    option: "{{item.key}}"
    value: "{{item.value}}"
  with_dict: identity_servers_conf['DEFAULT']

And I duplicate this task for every sections.

Is there a way to iterate through my dict file with each "section" parameters, i.e DEFAULT, database, token.
In fact, I try to find a way to do a with_dict nested in a with_items loop.

```

---

<div class="post-metadata">

**Author:** ![adeaime\_cetout](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@adeaime\_cetout](https://forum.ansible.com/u/adeaime_cetout)\
**Post date:** [January 5, 2015, 12:37pm UTC](https://forum.ansible.com/t/how-to-write-an-ansible-task-using-with-dict-in-a-loop-with-items/16878/2 "2015-01-05T12:37:05Z")

</div>

hello,  
I find very interesting this way of organizing variables for .ini files.

I wanted to use it myself, so I worked on a plugin that allows to generate all the keys of an .ini file in one pass with the inifile module.  
It works fine and I use to manage my OpenStack configuration files.

I am not a specialist in development, but I think this plugin can be useful for everyone, so if someone wants to take over to maintain and integrate it into ansible, he is welcome.

The plugin transforms the hierarchy data in a list (section, key, value) for use directly with the inifile module with\_inidata as below:

vars file :

`  
…  
glanceapi\_conf:  
DEFAULT:  
verbose: “{{ image\_log\_verbose }}”  
rabbit\_host: “{{ amqp\_host }}”  
rabbit\_port: “{{ amqp\_port }}”  
rabbit\_userid: “{{ amqp\_userid }}”  
rabbit\_password: “{{ amqp\_password }}”  
rabbit\_ha\_queues: “{{ amqp\_ha\_queues }}”  
database:  
connection: “mysql://{{ image\_db\_user }}:{{ image\_db\_password }}@{{ db\_host }}/{{ image\_db }}”  
keystone\_authtoken:  
auth\_uri: “http://{{ identity\_admin\_host }}:{{ identity\_api\_port }}/v2.0”  
identity\_uri: “http://{{ identity\_admin\_host }}:{{ identity\_admin\_port }}”  
admin\_tenant\_name: “{{ image\_ks\_tenant }}”  
admin\_user: “{{ image\_ks\_user }}”  
admin\_password: “{{ image\_ks\_password }}”  
paste\_deploy:  
flavor: keystone  
glance\_store:  
default\_store: file  
filesystem\_store\_datadir: /var/lib/glance/images/  
…

`

plugin code :

`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [January 5, 2015, 2:11pm UTC](https://forum.ansible.com/t/how-to-write-an-ansible-task-using-with-dict-in-a-loop-with-items/16878/3 "2015-01-05T14:11:14Z")

</div>

I would be tempted to keep it as a dictionary, but just to render the INI file using the template module.

That way additional lookup plugins can be avoided.

---

<div class="post-metadata">

**Author:** ![adeaime\_cetout](https://avatars.discourse-cdn.com/v4/letter/a/9d8465/32.png) [@adeaime\_cetout](https://forum.ansible.com/u/adeaime_cetout)\
**Post date:** [January 5, 2015, 3:40pm UTC](https://forum.ansible.com/t/how-to-write-an-ansible-task-using-with-dict-in-a-loop-with-items/16878/4 "2015-01-05T15:40:52Z")

</div>

Hello and thank you for your quick response.

In my opinion, to use a template, you must retrieve or generate it and keep it alive (track changes).  
You must also manage the template and variable files.  
With this solution, we only manage the specific keys in a single variables file.  
This is very visual and updating it can be done very easily.

Best Regards
