删除除ansible中的某些文件以外的文件

时间:2017-06-21 12:10:06

标签: ansible

我的tmp目录中有以下文件

root@ansible:/tmp/test$ ls /tmp/test/
file1  file2  file3

我还有以下以this

为模型的剧本
vars:
  exclude_files: file1
tasks:
 - name: check files
   shell: ls -l /tmp/test
   register: capture

- name: remove files
  file: path=/tmp/test/{{item}} state=absent
  with_items: capture.stdout_lines
  when: item not in exclude_files

- name: debug variable
  debug: msg={{exclude_files}}

不幸的是,第二项任务不是删除file2,file3。相反,它将注册变量视为文件。

ok: [172.16.2.3] => (item=capture.stdout_lines) => {
"changed": false,
"invocation": {
    "module_args": {
        "attributes": null,
        "backup": null,
        "content": null,
        "delimiter": null,
        "diff_peek": null,
        "directory_mode": null,
        "follow": false,
        "force": false,
        "group": null,
        "mode": null,
        "original_basename": null,
        "owner": null,
        "path": "/tmp/test/capture.stdout_lines",
        "recurse": false,
        "regexp": null,
        "remote_src": null,
        "selevel": null,
        "serole": null,
        "setype": null,
        "seuser": null,
        "src": null,
        "state": "absent",
        "unsafe_writes": null,
        "validate": null
    }
},
"item": "capture.stdout_lines",
"path": "/tmp/test/capture.stdout_lines",
"state": "absent"

知道为什么这段代码不起作用?我使用的是ansible 2.3。

1 个答案:

答案 0 :(得分:1)

with_items: capture.stdout_lines应为with_items: "{{capture.stdout_lines}}"

with_...中的裸变量很久以前就已被弃用。