Errativ behavior when looping over a fileglob

17 views
Skip to first unread message

Marc Haber

unread,
Nov 7, 2017, 1:22:19 PM11/7/17
to ansible...@googlegroups.com
Hi,

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

$ cat site.yml
---
- name: apply common configuration
hosts: all
remote_user: root
become: "yes"
roles:
- common
$ cat roles/common/tasks/repos.yml
---
- 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"
- name: include repositories
tags:
repos
include_tasks:
"{{distribution}}/{{distribution_version}}/repos.yml"
$

My target system contains the following files:
-rw-r--r-- 1 root root 0 Nov 7 19:06 zda-foo.list
-rw-r--r-- 1 root root 63 Nov 7 19:03 zda-sid-mc.list
-rw-r--r-- 1 root root 75 Nov 7 19:03 zda-sid-zg-stable-mc.list
-rw-r--r-- 1 root root 77 Nov 7 19:03 zda-sid-zg-unstable-mc.list

Running the playbook tries to remove two files that are not there but
leaves the files in place:

$ ansible-playbook --inventory=hosts.yml --limit=emptysid86 site.yml

PLAY [apply common configuration]

TASK [Gathering Facts]
ok: [emptysid86]

TASK [common : remove extra files that may be on the systems]
ok: [emptysid86] => (item=/etc/apt/sources.list.d/sid-zg-stable-mc.list)
ok: [emptysid86] => (item=/etc/apt/sources.list.d/sid-zg-unstable-mc.list)

When I replace with_fileglob with with_items in the tasks file, it
removes the files that are explicitly given, but of course doesn't glob,
so the /etc/apt/sources.lists.d/zda-*.list doesn't work.

What am I doing wrong?

Greetings
Marc

--
-----------------------------------------------------------------------------
Marc Haber | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany | lose things." Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature | How to make an American Quilt | Fax: *49 6224 1600421

Kai Stian Olstad

unread,
Nov 7, 2017, 1:35:23 PM11/7/17
to ansible...@googlegroups.com
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.


--
Kai Stian Olstad

Marc Haber

unread,
Nov 8, 2017, 2:15:40 PM11/8/17
to ansible...@googlegroups.com
Hi Kai,

thanks for the nudge. I was able to solve the issue.
> 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:

---
- name: search for sources.list files
find:
paths: "/etc/apt/sources.list.d"
patterns: "zda-*.list,exp-mc.list,sid-mc.list,sid-zg-stable-mc.list,sid-zg-unstable-mc.list,stretch-mc.list,stretch-security.list,stretch-zg-stable-mc.list,stretch-zg-unstable-mc.list,buster-mc.list,stretch-zg-stable-mc.list,stretch-security.list"
register: sourceslistfiles
- name: delete sources.list files
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ sourceslistfiles.files }}"

Thanks for helping.

Of course, this triggers immediately the next issue, such is when one
begins with new technology. Will post a new question soon.

Grüße
Reply all
Reply to author
Forward
0 new messages