如何在ansible中删除复制/模板模块的“所有其他"文件? [英] How to remove 'all other' files for copy/template modules in ansible?

查看:25
本文介绍了如何在ansible中删除复制/模板模块的“所有其他"文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小任务:

- 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

我想删除此任务配置的/etc/shinken/hosts/中的所有其他配置(文件).

I want to remove all other configs (files) in /etc/shinken/hosts/ configured by this task.

我该怎么做?

(如果我修复了 'shinken_hosts' 中的拼写错误,并希望自动删除名称中有错误的旧配置,这真的很重要).

(It is really important if I fix a typo in 'shinken_hosts', and want to automatically remove old config with mistake in the name).

推荐答案

您可能需要查看 这个,幻灯片 19.这假设您知道特定目录中需要存在哪些文件,然后删除所有其他文件.

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}}"

这篇关于如何在ansible中删除复制/模板模块的“所有其他"文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆