且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ansible - 从目录中删除非托管文件?

更新时间:2023-12-04 19:52:40

我会这样做,假设一个变量定义为 '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