# Ansible -  loop through multiple files when with\_items defined

**URL:** <https://forum.ansible.com/t/ansible-loop-through-multiple-files-when-with-items-defined/26590>\
**Category:** Ansible Project\
**Created:** [January 29, 2018, 6:07pm UTC](https://forum.ansible.com/t/ansible-loop-through-multiple-files-when-with-items-defined/26590 "2018-01-29T18:07:24Z")\
**Posts on this page:** 5\
**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 29, 2018, 6:07pm UTC](https://forum.ansible.com/t/ansible-loop-through-multiple-files-when-with-items-defined/26590/1 "2018-01-29T18:07:24Z")

</div>

This is similar to another question I posed a few days ago, but different enough I thought it deserved its own topic. I have a variable of ifcfg-list.stdout\_lines that contains a list of ifcfg-\* file names (exlcuding loopback). I need to iterate the following code over this list (apply it to all ifcfg-\* files), but don’t know how to do it. with\_nested doesn’t seem to be the right solution to me. I know I can do this in multiple tasks (already done), but I am trying to consolidate all of the tasks down to a single task:

`

- name: Gather list of ifcfg-\* files  
shell: ls “{{ net\_path }}” | grep ^ifcfg- | grep 0$ | grep -ve ifcfg-lo -e @ # @ excludes ansible backup files  
register: ifcfg\_list  
changed\_when: false

- name: Update ifcfg-\* files  
ini\_file:  
path: “{{ net\_path }}{{ ifcfg\_list.stdout\_lines }}” #\<-- I know this is wrong, but I need to iterate through this list and apply the changes to all files.  
no\_extra\_spaces: true  
section: null  
state: present  
option: “{{ item.option }}”  
value: “{{ item.value }}”  
with\_items:

- { option: “NM\_CONTROLLED”, value: “no” }

- { option: “PEERDNS”, value: “no” }

- { option: “DNS1”, value: “{{ dns1 }}” }

- { option: “DNS2”, value: “{{ dns2 }}” }  
notify:

- Networking

`

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [January 29, 2018, 6:11pm UTC](https://forum.ansible.com/t/ansible-loop-through-multiple-files-when-with-items-defined/26590/2 "2018-01-29T18:11:19Z")

</div>

What's wrong with "with\_nested"?

---

<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 29, 2018, 6:14pm UTC](https://forum.ansible.com/t/ansible-loop-through-multiple-files-when-with-items-defined/26590/3 "2018-01-29T18:14:26Z")

</div>

Nothing I guess, other than I didn’t know how to use it in this scenario. Thank you for your example, I will go and work with it.

---

<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 29, 2018, 6:32pm UTC](https://forum.ansible.com/t/ansible-loop-through-multiple-files-when-with-items-defined/26590/4 "2018-01-29T18:32:19Z")

</div>

Uwe Sauter,

It worked perfectly! Thank you.

---

<div class="post-metadata">

**Author:** ![Uwe\_Sauter](https://avatars.discourse-cdn.com/v4/letter/u/41988e/32.png) [@Uwe\_Sauter](https://forum.ansible.com/u/Uwe_Sauter)\
**Post date:** [January 29, 2018, 6:33pm UTC](https://forum.ansible.com/t/ansible-loop-through-multiple-files-when-with-items-defined/26590/5 "2018-01-29T18:33:03Z")

</div>

You're welcome.
