Skip to content

Instantly share code, notes, and snippets.

@funollet
Created March 21, 2015 16:05
Show Gist options
  • Save funollet/bcbcaf0f4e752a8adc94 to your computer and use it in GitHub Desktop.
Save funollet/bcbcaf0f4e752a8adc94 to your computer and use it in GitHub Desktop.
An example with Ansible to manage files in a directory; unmanaged files must be removed
---
- hosts: localhost
connection: local
vars:
conf_d: results/conf.d
put_files:
- a.conf
- b.conf
tasks:
- name: Put files in conf.d/
template:
src: "templates/{{ item }}.j2"
dest: "{{ conf_d}}/{{ item }}"
with_items: put_files
- name: Make list of all files
shell: "ls {{ conf_d }}"
register: all_files
- name: Remove extra files
file:
path: "results/conf.d/{{ item }}"
state: absent
with_items: all_files.stdout_lines | difference(put_files)
init:
mkdir -p results/conf.d/
mkdir -p templates
touch templates/{a,b}.conf.j2
touch results/conf.d/unwanted_file.conf
touch results/conf.d/break_my_config.conf
run:
ansible-playbook -i localhost, clumsy.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment