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

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

问题描述

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

- 模板:>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/ 中删除它.

有没有办法做到这一点?我尝试摆弄 rsync --delete,但我在模板中遇到了问题,这些模板的后缀 .j2 被删除了.

解决方案

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

- shell: ls -1/some/dir注册:内容- 文件:path=/some/dir/{{ item }} state=absentwith_items:contents.stdout_lines时间:项目不在 managed_files 中

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"

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.

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.

解决方案

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天全站免登陆