--- - name: "list previous host configs" shell: ls -1 /etc/icinga2/conf.d/ansible_routeros/ | sed -e 's/\.[^\.]*$//' register: previous_hosts changed_when: false - name: "create conf file for each host" template: src="host" dest="/etc/icinga2/conf.d/ansible_routeros/{{ item }}.conf" with_items: "{{ hostlist }}" notify: - reload icinga2 - name: "remove stale hosts" file: path="/etc/icinga2/conf.d/ansible_routeros/{{ item }}.conf" state=absent with_items: "{{ previous_hosts.stdout_lines }}" when: hostvars[item] is undefined notify: - reload icinga2