How to remove 'all other' files for copy/t

2019-09-12 01:46发布

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条回答
该账号已被封号
2楼-- · 2019-09-12 02:46

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}}”
查看更多
登录 后发表回答