# with\_items/iterations over dictionary for a sysctl task.

**URL:** <https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594>\
**Category:** Ansible Project\
**Created:** [February 18, 2014, 8:55pm UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594 "2014-02-18T20:55:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [February 18, 2014, 8:55pm UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594/1 "2014-02-18T20:55:01Z")

</div>

Hi,

I have defined under group\_vars, something like the following :

---

<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:** [February 19, 2014, 1:29am UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594/2 "2014-02-19T01:29:25Z")

</div>

The pattern for iterating over a hash is the same regardless of module and would look like:

vars:  
my\_pairs:

- key: foo  
value: bar
- key: foo2  
value: bar2

tasks:

- shell: echo “{{ item.key }} and {{ item.value }}”  
with\_items: my\_pairs

So, generally applicable to sysctl or anything else.

You could also define the list right under “with\_items” without the intermediate variable.

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [February 19, 2014, 5:54am UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594/3 "2014-02-19T05:54:56Z")

</div>

Hi Michael,

but in this case the key name is not a static, they key is the sysctl parameter, would it be possible to use a similar construct to the one in the template? my vars for this use case and also some others are 6-10+ items, each item could be defined easily as follow

vars:  
pana\_sys\_ctl:

net.ipv4.ip\_local\_port\_range: 32768 610

net.ipv4.tcp\_fin\_timeout: 30

net.ipv4.tcp\_keepalive\_time: 1800  
…  
pana\_mounts:  
mnt1:  
red: filer001:/disk1/vol1  
blue:filer002:/disk1/vol3  
…

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [February 19, 2014, 7:42am UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594/4 "2014-02-19T07:42:10Z")

</div>

Hi Walid,

Can you try :

- sysctl: name={{ item[0] }} value= {{ item[1] }}

with\_items: pana\_sys\_ctl|dictsort

Regards,

Benno

---

<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:** [February 19, 2014, 1:00pm UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594/5 "2014-02-19T13:00:33Z")

</div>

The dictsort just sorts them and doesn’t really change much.

It sounds like you have a list of sysctl names and each sysctl name has a lot of settings under it.

You could consider flattening everything to one list, and also maybe using the “when” operator to filter which to apply.

If you had a hash of lists and wanted to apply the sysctls in multiple hashes, that might require a filter plugin.

---

<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:** [February 19, 2014, 1:01pm UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594/6 "2014-02-19T13:01:32Z")

</div>

Sorry, I meant “lookup” plugin.

I mean if you had something like:

my\_sysctls:  
one:

- { key: x, value: y }
- { key: x2, value: y2 }  
two:  
…

and wanted to apply all sysctls in one or two.

I would probably just keep seperate lists and iterate over them, and keep those configurations with the roles that need them instead.

---

<div class="post-metadata">

**Author:** ![Walid](https://avatars.discourse-cdn.com/v4/letter/w/ea5d25/32.png) [@Walid](https://forum.ansible.com/u/Walid)\
**Post date:** [February 24, 2014, 7:49pm UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594/7 "2014-02-24T19:49:14Z")

</div>

Hi Michael, and Benno,

Sorry for not replying early, i had a bad flu. at the end i choose to go with dictionary the way Michael described, and not the array/list, as dictionaries are more clear in the playbook tasks. e.g item.key vs item.0  
I am currently using Ansible 1.4.5 and i hit a problem with dictionaries if i format the with\_items differently see below what worked for me and what did not work

### does not work , complains about ""undefined variables: ‘str object’ has no attribute ‘name’ “”

> 

- debug: var= {{ [item.name](http://item.name) }}  
with\_items:

> - my\_pairs

#### works fine

- debug: var= {{ [item.name](http://item.name) }}  
with\_items:

> - " {{ my\_pairs }}"

##### or this also works fine

- debug: var= {{ [item.name](http://item.name) }}  
with\_items: my\_pairs

Is that a YAML convention issue, or an ansible parsing bug ?

kind regards

Walid

---

<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:** [February 24, 2014, 8:00pm UTC](https://forum.ansible.com/t/with-items-iterations-over-dictionary-for-a-sysctl-task/14594/8 "2014-02-24T20:00:49Z")

</div>

with\_items:
