Ansible - 如何注册来自" FIND"模块和其他用途

时间:2017-01-10 09:40:48

标签: ansible

我需要找到未知目录位置的文件并将其删除。 试图使用"发现"模块,注册其输出,并将其传递给"文件"。

即使我看到注册路径,我也不能在以后使用它:

< TASK [print find_result] >
ok: [1.2.3.4] => {
    "find_result": {
        "changed": false, 
        "examined": 3119, 
        "files": [
            {
                "atime": 1483973253.7295375, 
           ...
                "mode": "0600", 
                "mtime": 1483973253.7295375, 
                "nlink": 1, 
                "path": "/tmp/delme", 

我的剧本是:

- hosts: "{{ target }}"
  become: no
  vars:
    find_what: "delme*"
    find_where: "/tmp"

  tasks:
  - name: finding files
    find:
      paths:            "{{ find_where }}"
      patterns:         "{{ find_what }}"
      recurse:          "yes"
      file_type:        "file"
    register: find_result

# \/ for debugging
  - name: print find_result
    debug: var=find_result

  - name: remove files
    file:
        path= "{{ item.path }}"
        state=absent
    with_items: "{{ find_result.files }}"

2 个答案:

答案 0 :(得分:10)

file之后的=任务空间存在语法缺陷。

尝试:

- name: remove files
  file:
    path: "{{ item.path }}"
    state: absent
  with_items: "{{ find_result.files }}"

答案 1 :(得分:0)

删除文件任务时出现语法错误。

您可以使用:

- name: remove files
  file: >
    path={{ item.path }}
    state=absent
  with_items: "{{ find_result.files }}"

或者

- name: remove files
  file: path={{ item.path }} state=absent
  with_items: "{{ find_result.files }}"