How to remove 'all other' files for copy/template modules in ansible?

旧时模样 提交于 2019-12-12 03:22:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!