# double with\_items and loops

**URL:** <https://forum.ansible.com/t/double-with-items-and-loops/26569>\
**Category:** Ansible Project\
**Created:** [January 25, 2018, 5:38pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569 "2018-01-25T17:38:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 25, 2018, 5:38pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/1 "2018-01-25T17:38:36Z")

</div>

I have the following, and wish to combine them, but don’t know how because my with\_items is referencing a variable. Wondering if someone can guide me (if possible) on how to combine these two stanzas:

`

- name: Add DNS1 to ifcfg-\* files if missing  
lineinfile:  
path: “{{ net\_path }}{{ item }}”  
state: present  
regexp: “^DNS1”  
line: “DNS1={{ dns1 }}”  
with\_items: “{{ ifcfg\_list.stdout\_lines }}”  
ignore\_errors: yes  
notify:

- Networking

- name: Add DNS2 to ifcfg-\* files if missing  
lineinfile:  
path: “{{ net\_path }}{{ item }}”  
state: present  
regexp: “^DNS2”  
line: “DNS2={{ dns2 }}”  
with\_items: “{{ ifcfg\_list.stdout\_lines }}”  
ignore\_errors: yes  
notify:

- Networking

`

Here is how far I get:

` `How do I incorporate:` `“{{ ifcfg\_list.stdout\_lines }}”`

`That allows me to loop through all ifcfg-* files (except ifcfg-lo)`

- name: Add DNS to ifcfg-\* files if missing  
lineinfile:  
path: “{{ net\_path }}{{ item.nic }}” #\<— This item is no longer reference by ifcfg\_list.stdout\_lines  
state: present  
regexp: “{{ item.regexp }}”  
line: “{{ item.line }}”  
with\_items:
- {regexp: “`^DNS1`”, line: “`DNS1={{ dns1 }}`”, nic: “ifcfg\_list.stdout\_lines”} #\<— The nic section of these looks incorrect to me, and I doubt it would work
- `{regexp: "```^DNS2`", line: "```DNS2={{ dns2 }}`", nic: "ifcfg_list.stdout_lines"}`  
`````ignore\_errors: yes` `notify:` `- Networking`

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 25, 2018, 7:26pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/2 "2018-01-25T19:26:25Z")

</div>

Use the find module to search for the files and exclude the ifcfg-lo and register the result.

Then you can use the with\_nested in the lineinfile.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 25, 2018, 10:31pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/3 "2018-01-25T22:31:07Z")

</div>

I tried the following, but it failed. Likely some syntax issues (doesn’t like my var). I am having a hard time finding comprehensive docs on with\_nested. I will keep trying, but if you have some suggestions, I am all ears.

`

- name: Add DNS entries to ifcfg-\* files if missing  
lineinfile:  
path: “{{ net\_path }}{{ item[0] }}”  
state: present  
regexp: “{{ item[1] }}”  
line: “{{ item[2] }}”  
with\_nested:
- ifcfg\_list.stdout\_lines
- [‘^DNS1’, ‘^DNS2’]
- [‘DNS1={{ dns1 }}’, ‘DNS2={{ dns2 }}’]

`

Failed message:

`  
failed: [ansible-oel6] (item=[u’ifcfg\_list.stdout\_lines’, u’^DNS1’, u’DNS1=10.253.1.23’]) =\> {“changed”: false, “item”: [“ifcfg\_list.stdout\_lines”, “^DNS1”, “DNS1=10.253.1.23”], “msg”: “Destination /etc/sysconfig/network-scripts/ifcfg\_list.stdout\_lines does not exist !”, “rc”: 257}

`

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 25, 2018, 10:33pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/4 "2018-01-25T22:33:46Z")

</div>

``

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 25, 2018, 10:46pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/5 "2018-01-25T22:46:29Z")

</div>

It ultimately does what I want, but changes stuff every time. I can’t figure out the logic that is doing that. Any ideas?

`TASK [dns_update : Add DNS entries to ifcfg-* files if missing] ************************************************************************************************************************************************************************** ok: [ansible-oel7] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS1=10.1.1.10']) ok: [ansible-oel6] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS1=10.1.1.10']) ok: [ansible-rhel6] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS1=10.1.1.10']) ok: [ansible-rhel7] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS1=10.1.1.10']) changed: [ansible-rhel6] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS2=10.1.1.11']) changed: [ansible-oel7] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS2=10.1.1.11']) changed: [ansible-oel6] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS2=10.1.1.11']) changed: [ansible-rhel7] => (item=[u'ifcfg-eth0', u'^DNS1.*', u'DNS2=10.1.1.11']) changed: [ansible-oel7] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS1=10.1.1.10']) changed: [ansible-rhel6] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS1=10.1.1.10']) changed: [ansible-oel6] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS1=10.1.1.10']) changed: [ansible-rhel7] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS1=10.1.1.10']) ok: [ansible-oel7] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS2=10.1.1.11']) ok: [ansible-oel6] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS2=10.1.1.11']) ok: [ansible-rhel6] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS2=10.1.1.11']) ok: [ansible-rhel7] => (item=[u'ifcfg-eth0', u'^DNS2.*', u'DNS2=10.1.1.11'])`

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 25, 2018, 11:53pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/6 "2018-01-25T23:53:57Z")

</div>

The more I read about this, the more I think it is working out of luck. In fact, I don’t understand how it is working now based on the output previously mentioned. According to that both DNS1 and DNS2 should be pointing to dns2 or 10.1.1.11. Thoughts?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 26, 2018, 7:34am UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/7 "2018-01-26T07:34:35Z")

</div>

Your nested has 3 levels and that gets you in trouble since you get this four combinations  
ifcfg-eth0 , ^DNS1.\* , DNS1=10.1.1.10  
ifcfg-eth0 , ^DNS1.\* , DNS2=10.1.1.11  
ifcfg-eth0 , ^DNS2.\* , DNS1=10.1.1.10  
ifcfg-eth0 , ^DNS2.\* , DNS2=10.1.1.11

But you only want these two  
ifcfg-eth0 , ^DNS1.\* , DNS1=10.1.1.10  
ifcfg-eth0 , ^DNS2.\* , DNS2=10.1.1.11

So try this

- name: Add DNS entries to ifcfg-\* files if missing  
&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ net\_path }}{{ item[0] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: "^{{ item[1] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: "{{ item[1] }}={{ vars[item[1] | lower] }}"  
&nbsp;&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- '{{ ifcfg\_list.stdout\_lines }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ['DNS1', 'DNS2']

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 26, 2018, 4:43pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/8 "2018-01-26T16:43:12Z")

</div>

Kai, thank you. That is awesome. I wouldn’t have thought of that. I do have some follow-up questions, just to understand.

`  
line: “{{ item[1] }}={{ vars[item[1] | lower] }}”

`  
What does the = sign do here? Is it overriding the with\_nested item[1]?  
What is vars specifying? If I came close to ever doing this on my own I wouldn’t have added vars… so I am curious.

I know what the rest of it is doing, but am a little fuzzy in regards to the two things above.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 26, 2018, 5:32pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/9 "2018-01-26T17:32:39Z")

</div>

> \>  
> \> So try this  
> \>  
> \> - name: Add DNS entries to ifcfg-\* files if missing  
> \> lineinfile:  
> \> path: "{{ net\_path }}{{ item[0] }}"  
> \> state: present  
> \> regexp: "^{{ item[1] }}"  
> \> line: "{{ item[1] }}={{ vars[item[1] | lower] }}"  
> \> with\_nested:  
> \> - '{{ ifcfg\_list.stdout\_lines }}'  
> \> - ['DNS1', 'DNS2']  
> \>  
> \>  
> \>
> 
> Kai, thank you. That is awesome. I wouldn't have thought of that. I do  
> have some follow-up questions, just to understand.

I'll try to explain.

> line: "{{ item[1] }}={{ vars[item[1] | lower] }}"  
> What does the = sign do here? Is it overriding the with\_nested item[1]?

The = sign is just the = sign.  
In the first iteration the "item[1] = DNS1".  
You need you the line in the file to be DNS1=10.1.1.10

So {{ item[0] }} would be replaced by the content, so it becomes DNS1, we need the character = to get DNS=

> What is vars specifying? If I came close to ever doing this on my own I  
> wouldn't have added vars.... so I am curious.

Your DNS IP is in the variable named dns, to use the content of a variable as a variable name you need to use vars or hostvars.  
vars if the same as hostvars[inventory\_hostname].

item[1] is DNS (upper case) but the variable name is dns (lower case) so "vars[item[1] | lower]" becomes vars['dns'] that is the same {{ dns }} and you get 10.1.1.10.

> I know what the rest of it is doing, but am a little fuzzy in regards to  
> the two things above.

Hopefully my explanation is understandable.

---

<div class="post-metadata">

**Author:** ![Malcolm\_Hussain-Gamb](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Malcolm\_Hussain-Gamb](https://forum.ansible.com/u/Malcolm_Hussain-Gamb)\
**Post date:** [January 26, 2018, 8:18pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/10 "2018-01-26T20:18:35Z")

</div>

If you are editing these files the inifile module is much better btw. Take a look

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 26, 2018, 8:22pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/11 "2018-01-26T20:22:48Z")

</div>

Kai, perfect, thank you. It can get confusing looking at the various variables and {{items}}. Your explanation makes sense.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 26, 2018, 8:23pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/12 "2018-01-26T20:23:03Z")

</div>

Thanks, I will.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 26, 2018, 8:42pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/13 "2018-01-26T20:42:51Z")

</div>

Malcom! Thanks for info on the ini\_file module! That will be much better to use…now I can cut back on the horribly complicated regexp stuff!

---

<div class="post-metadata">

**Author:** ![Malcolm\_Hussain-Gamb](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Malcolm\_Hussain-Gamb](https://forum.ansible.com/u/Malcolm_Hussain-Gamb)\
**Post date:** [January 27, 2018, 7:34pm UTC](https://forum.ansible.com/t/double-with-items-and-loops/26569/14 "2018-01-27T19:34:03Z")

</div>

Another one to look at is the newly added xml module, if you have to suffer the pain of xml config files.
