Remove missing files from a list

8 views
Skip to first unread message

Andy Smith

unread,
Apr 4, 2018, 4:43:14 PM4/4/18
to ansible...@googlegroups.com
Hi,

Imagine a service foo that has a config file
/etc/foo/files_of_interest which is just a list of other file paths,
one per line. If you put a path that doesn't exist into the file
then the service foo complains about it regularly, so you'd better
not do that.

I'd like to provide a default list of paths to all remote hosts, but
sometimes some of those paths won't be present. At the time the
/etc/foo/files_of_interest file is built I would like to exclude any
paths that don't exist.

I would prefer not to use per-host variables to specify a different
set of paths as that would be too much to manage and some of these
paths may spring into existence later on, without me knowing.

I found one way to do it:

https://gist.github.com/grifferz/a505e352baa18e06ba1ba1d02a123ee2

This works, but have I missed something which allows this to be done
in a more elegant way?

Cheers,
Andy

flowerysong

unread,
Apr 4, 2018, 5:56:39 PM4/4/18
to Ansible Project
On Wednesday, April 4, 2018 at 4:43:14 PM UTC-4, Andy Smith wrote:

I'd like to provide a default list of paths to all remote hosts, but
sometimes some of those paths won't be present. At the time the
/etc/foo/files_of_interest file is built I would like to exclude any
paths that don't exist.

I would prefer not to use per-host variables to specify a different
set of paths as that would be too much to manage and some of these
paths may spring into existence later on, without me knowing.

I found one way to do it:

    https://gist.github.com/grifferz/a505e352baa18e06ba1ba1d02a123ee2

This works, but have I missed something which allows this to be done
in a more elegant way?

Building the list of missing files doesn't need to be a multi-step process or involve an Ansible loop. I would preserve the ordering in the generated file instead of splitting it into two lists, so that someone manually adding a file and uncommenting its line creates the same state as re-templating the file.

Andy Smith

unread,
Apr 5, 2018, 11:02:38 PM4/5/18
to ansible...@googlegroups.com
Hi,
Excellent, thank you! Some new concepts for me there.

I had to add a "set_fact:" between lines 15 and 16 to get this to
pass syntax check, but I assume this was just a typo (Ansible
2.4.3.0).

Cheers,
Andy
Reply all
Reply to author
Forward
0 new messages