Last active
June 7, 2018 06:40
-
-
Save grifferz/a505e352baa18e06ba1ba1d02a123ee2 to your computer and use it in GitHub Desktop.
Build a config file with a supplied list of file paths, indicating any that are missing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# If there were any files missing on the remote host then remove them from #} | |
{# the files_of_interest list using the "difference" filter. #} | |
{% if missing_files_of_interest | length > 0 %} | |
{% set files_of_interest = files_of_interest | difference(missing_files_of_interest) %} | |
############################################################################ | |
# The following files were configured but don't exist on this host right now: | |
{% for file in missing_files_of_interest %} | |
# {{ file }} | |
{% endfor %} | |
############################################################################ | |
{% endif %} | |
{% for file in files_of_interest %} | |
{{ file }} | |
{% endfor %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: all | |
vars: | |
files_of_interest: | |
- /var/log/syslog | |
- /var/log/auth.log | |
- /does/not/exist | |
tasks: | |
- name: Check that all of the interesting files actually exist | |
stat: | |
path: "{{ item }}" | |
with_items: "{{ files_of_interest }}" | |
register: check_files_of_interest | |
- name: Initialize missing file list | |
set_fact: | |
missing_files_of_interest: [] | |
- name: Build list of missing files | |
set_fact: | |
missing_files_of_interest: "{{ missing_files_of_interest + [ item.item ] }}" | |
with_items: "{{ check_files_of_interest.results }}" | |
when: item.stat.exists == False | |
- name: Build /etc/foo/files_of_interest | |
template: | |
src: files_of_interest.j2 | |
dest: /etc/foo/files_of_interest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment