# Errativ behavior when looping over a fileglob

**URL:** <https://forum.ansible.com/t/errativ-behavior-when-looping-over-a-fileglob/25950>\
**Category:** Ansible Project\
**Created:** [November 7, 2017, 6:21pm UTC](https://forum.ansible.com/t/errativ-behavior-when-looping-over-a-fileglob/25950 "2017-11-07T18:21:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [November 7, 2017, 6:21pm UTC](https://forum.ansible.com/t/errativ-behavior-when-looping-over-a-fileglob/25950/1 "2017-11-07T18:21:48Z")

</div>

Hi,

I am trying to remove a bunch of files wiht the following code:

$ cat site.yml

---

<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:** [November 7, 2017, 6:35pm UTC](https://forum.ansible.com/t/errativ-behavior-when-looping-over-a-fileglob/25950/2 "2017-11-07T18:35:06Z")

</div>

with\_fileglob is a lookup plugin, and lookup plugin runs on the Ansible controller and not the remote host.

To do what what you want to do, you must use the find module to find the files and register the output in a variable.  
Then you can delete them with the file module and with\_items.

---

<div class="post-metadata">

**Author:** ![Marc\_Haber](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Marc\_Haber](https://forum.ansible.com/u/Marc_Haber)\
**Post date:** [November 8, 2017, 7:15pm UTC](https://forum.ansible.com/t/errativ-behavior-when-looping-over-a-fileglob/25950/3 "2017-11-08T19:15:24Z")

</div>

Hi Kai,

thanks for the nudge. I was able to solve the issue.

> \> - name: remove extra files that may be on the systems  
> \> file:  
> \> path: "{{ item }}"  
> \> state: absent  
> \> with\_fileglob:  
> \> - "/etc/apt/sources.list.d/zda-\*.list"  
> \> - "/etc/apt/sources.list.d/exp-mc.list"  
> \> - "/etc/apt/sources.list.d/sid-mc.list"  
> \> - "/etc/apt/sources.list.d/sid-zg-stable-mc.list"  
> \> - "/etc/apt/sources.list.d/sid-zg-unstable-mc.list"  
> \> - "/etc/apt/sources.list.d/stretch-mc.list"  
> \> - "/etc/apt/sources.list.d/stretch-security.list"  
> \> - "/etc/apt/sources.list.d/stretch-zg-stable-mc.list"  
> \> - "/etc/apt/sources.list.d/stretch-zg-unstable-mc.list"  
> \> - "/etc/apt/sources.list.d/buster-mc.list"  
> \> - "/etc/apt/sources.list.d/stretch-zg-stable-mc.list"  
> \> - "/etc/apt/sources.list.d/stretch-security.list"  
> \> - "/etc/apt/preferences.d/??-zda-\*.pref"  
> \> - "/etc/apt/preferences.d/10-sid-zg-unstable-mc.pref"  
> \> - "/etc/apt/preferences.d/20-sid-zg-stable-mc.pref"  
> \> - "/etc/apt/preferences.d/50-sid-security.pref"  
> \> - "/etc/apt/preferences.d/70-sid.pref"  
> \> - "/etc/apt/preferences.d/99-default.pref"
> 
> with\_fileglob is a lookup plugin, and lookup plugin runs on the Ansible controller and not the remote host.

Ouch. That was not clear to me at all, but of course it explains  
eveything. So you use with\_fileglob mainly when you transport files from  
the controller to the remote.

> To do what what you want to do, you must use the find module to find the files and register the output in a variable.  
> Then you can delete them with the file module and with\_items.

This is my code:
