ansible-从目录中删除非托管文件? [英] ansible - delete unmanaged files from directory?

查看:71
本文介绍了ansible-从目录中删除非托管文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以递归方式复制目录将其中的所有 .j2 文件渲染为模板。为此,我目前使用以下行:

I want to recursively copy over a directory and render all .j2 files in there as templates. For this I am currently using the following lines:

- template: >
            src=/src/conf.d/{{ item }}
            dest=/dest/conf.d/{{ item|replace('.j2','') }}
  with_lines: find /src/conf.d/ -type f -printf "%P\n"



<现在,我正在寻找一种从该目录中删除非托管文件的方法。例如,如果我从 /src/conf.d / 中删除​​文件/模板,我希望Ansible从 /dest/conf.d/中删除它。

Now I'm looking for a way to remove unmanaged files from this directory. For example if I remove a file/template from /src/conf.d/ I want Ansible to remove it from /dest/conf.d/ as well.

有没有办法做到这一点?我尝试摆弄 rsync --delete ,但那里的模板后缀为 .j2 >已删除。

Is there some way to do this? I tried fiddling around with rsync --delete, but there I got a problem with the templates which get their suffix .j2 removed.

推荐答案

我会这样做,假设顶部定义为 managed_files的变量是一个列表

I'd do it like this, assuming a variable defined as 'managed_files' up top that is a list.

- shell: ls -1 /some/dir
  register: contents

- file: path=/some/dir/{{ item }} state=absent
  with_items: contents.stdout_lines
  when: item not in managed_files

这篇关于ansible-从目录中删除非托管文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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