问题
Small task:
- name: Configure hosts
template: src=host.cfg.j2 dest=/etc/shinken/hosts/{{item.host_name}}.cfg
with_items: shinken_hosts
when: shinken_hosts is defined
notify: reload config
I want to remove all other configs (files) in /etc/shinken/hosts/ configured by this task.
How can I do this?
(It is really important if I fix a typo in 'shinken_hosts', and want to automatically remove old config with mistake in the name).
回答1:
you might want to check this, slide 19. This assumes that you know what files needs to exist in the specific dir, and then deletes all others.
# tidy_expected: [‘conf1.cfg’, conf2.cfg’]
- find: paths={{tidy_path}} #/etc/myapp
register: existing
- file: path={{item.path}} state=absent
when: item.path|basename not in tidy_expected
with_items: “{{existing.files|default([ ])}}”
register: removed
- mail: body=“{{removed}}”
来源:https://stackoverflow.com/questions/32908310/how-to-remove-all-other-files-for-copy-template-modules-in-ansible