# New to ansible --  Looping through files with wildcards

**URL:** <https://forum.ansible.com/t/new-to-ansible-looping-through-files-with-wildcards/25237>\
**Category:** Ansible Project\
**Created:** [August 11, 2017, 8:42pm UTC](https://forum.ansible.com/t/new-to-ansible-looping-through-files-with-wildcards/25237 "2017-08-11T20:42:17Z")\
**Posts on this page:** 4\
**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:** [August 11, 2017, 8:42pm UTC](https://forum.ansible.com/t/new-to-ansible-looping-through-files-with-wildcards/25237/1 "2017-08-11T20:42:17Z")

</div>

Hi all,

I am new to ansible and am looking for some help. I don’t mind finding the answer on my own; however, I could use a shove in the right direction.

I need to modify some files (/etc/sysconfig/network-scripts/ifcfg-\* files). I was trying to use with\_fileglob (among other options), but I read somewhere that wont work ( [https://stackoverflow.com/questions/29831690/ansible-with-fileglob-not-matching-files-with-only-wildcard](https://stackoverflow.com/questions/29831690/ansible-with-fileglob-not-matching-files-with-only-wildcard) )

Basically, I am looking to run a loop against all ifcfg-\* files. I want to modify some entries (which I can do individually) and delete some entries (which I can also do individually), I just don’t know how to approach looping through all of the files. I am even unsure if fileglob is the right approach. I would appreciate some education on the matter. How would you approach it?

Thanks,  
john

---

<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:** [August 11, 2017, 8:59pm UTC](https://forum.ansible.com/t/new-to-ansible-looping-through-files-with-wildcards/25237/2 "2017-08-11T20:59:28Z")

</div>

Trying this (which doesn’t delete the lines yet)… but the other problem is that the shell task is not idempotent

---

<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:** [August 11, 2017, 9:42pm UTC](https://forum.ansible.com/t/new-to-ansible-looping-through-files-with-wildcards/25237/3 "2017-08-11T21:42:48Z")

</div>

> Trying this (which doesn't delete the lines yet).... but the other problem  
> is that the shell task is not idempotent

What do you mean by "is not idempotent" it's just listing files and it need to do that every time.

> ---  
> # tasks file for dns\_update  
> - name: Retrieve ifcfg files in /etc/sysconfig/network-scripts  
> &nbsp;&nbsp;&nbsp;shell: ls /etc/sysconfig/network-scripts | grep ^ifcfg-  
> &nbsp;&nbsp;&nbsp;register: path\_files

Alternative is the find module  
- name: Retrieve ifcfg files in /etc/sysconfig/network-scripts  
&nbsp;&nbsp;&nbsp;find:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths: ls /etc/sysconfig/network-scripts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pattern: ifcfg-\*  
&nbsp;&nbsp;&nbsp;register: path\_files

> - name: Removing all DNS entries in ifcfg-\* files  
> &nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /etc/sysconfig/network-scripts/{{ path\_files.stdout\_lines }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^DNS'  
> &nbsp;&nbsp;&nbsp;with\_items: "{{ path\_files.stdout\_lines }}"

The value from with\_items i in a variable called item so the path sould be  
&nbsp;&nbsp;&nbsp;path: /etc/sysconfig/network-scripts/{{ item }}

With the find module above this task should look like this  
- name: Removing all DNS entries in ifcfg-\* files  
&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: '{{ item.path }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^DNS'  
&nbsp;&nbsp;&nbsp;&nbsp;with\_items: '{{ path\_files.files }}'

---

<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:** [August 11, 2017, 9:43pm UTC](https://forum.ansible.com/t/new-to-ansible-looping-through-files-with-wildcards/25237/4 "2017-08-11T21:43:10Z")

</div>

Forgive me for responding to myself… I just hope that someone in the future might find this relevant/useful.

added “changed\_when: false” to the shell task. That took care of the idempotent issue I was seeing by overriding the change result.

Still plugging away on the other issue

- name: Disabling NetworkManager in ifcfg-\* files  
lineinfile:  
path: /etc/sysconfig/network-scripts/{{path\_files.stdout\_lines}}  
regexp: ‘^NM\_CONTROLLED’  
line: “NM\_CONTROLLED=no”

The above is now returning:  
TASK [dns\_update : Disabling NetworkManager in ifcfg-\* files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [ansibletest-rhel7]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “Destination /etc/sysconfig/network-scripts/[u’ifcfg-bond0’, u’ifcfg-eth0’, u’ifcfg-eth1’, u’ifcfg-eth2’, u’ifcfg-eth3’, u’ifcfg-eth4’, u’ifcfg-eth5’, u’ifcfg-lo’] does not exist !”, “rc”: 257}  
fatal: [ansibletest-rhel6]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “Destination /etc/sysconfig/network-scripts/[u’ifcfg-bond0’, u’ifcfg-eth0’, u’ifcfg-eth1’, u’ifcfg-eth2’, u’ifcfg-eth3’, u’ifcfg-eth4’, u’ifcfg-eth5’, u’ifcfg-lo’] does not exist !”, “rc”: 257}  
fatal: [ansibletest-oel6]: FAILED! =\> {“changed”: false, “failed”: true, “msg”: “Destination /etc/sysconfig/network-scripts/[u’ifcfg-bond0’, u’ifcfg-eth0’, u’ifcfg-eth1’, u’ifcfg-eth2’, u’ifcfg-eth3’, u’ifcfg-eth4’, u’ifcfg-eth5’, u’ifcfg-lo’] does not exist !”, “rc”: 257}

Notice the ifcfg\* names are messed up. I am trying to narrow down the reason
