在ansible中循环嵌套列表

时间:2018-05-15 13:47:32

标签: ansible

我正在使用find模块来获取所有名为" deployments"的目录。在不同的安装点(/ E,/ F)和 然后使用文件模块在所有找到的dirs中设置组所有权。 现在ansible在嵌套列表中给出了find输出,而with_items无法遍历所有挂载点的文件列表。 如何在我的任务中循环所有嵌套列表?

results=[
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "examined": 139898,
                "files": [
                    {
                        "atime": 1526307047.608814,
                        "ctime": 1523368503.64159,
                        "dev": 64778,
                        "gid": 780200012,
                        "inode": 39583770,
                        "isblk": false,
                        "ischr": false,
                        "isdir": true,
                        "isfifo": false,
                        "isgid": true,
                        "islnk": false,
                        "isreg": false,
                        "issock": false,
                        "isuid": true,
                        "mode": "6775",
                        "mtime": 1523368503.64159,
                        "nlink": 2,
                        "path": "/F/Ford/AutoDeploy/PRD/local_1/deployments",
                        "rgrp": true,
                        "roth": true,
                        "rusr": true,
                        "size": 4096,
                        "uid": 780200029,
                        "wgrp": true,
                        "woth": false,
                        "wusr": true,
                        "xgrp": true,
                        "xoth": true,
                        "xusr": true
                    }
                ],
                "invocation": {
                    "module_args": {
                        "age": null,
                        "age_stamp": "mtime",
                        "contains": null,
                        "file_type": "directory",
                        "follow": false,
                        "get_checksum": false,
                        "hidden": false,
                        "paths": [
                            "/F"
                        ],
                        "patterns": [
                            "deployments"
                        ],
                        "recurse": true,
                        "size": null,
                        "use_regex": false
                    }
                },
                "item": "/F",
                "matched": 1,
                "msg": ""
            },
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": false,
                "examined": 60251,
                "files": [
                    {
                        "atime": 1526365588.0262258,
                        "ctime": 1521525712.8813984,
                        "dev": 64777,
                        "gid": 780200012,
                        "inode": 12058651,
                        "isblk": false,
                        "ischr": false,
                        "isdir": true,
                        "isfifo": false,
                        "isgid": true,
                        "islnk": false,
                        "isreg": false,
                        "issock": false,
                        "isuid": true,
                        "mode": "6775",
                        "mtime": 1521525712.8813984,
                        "nlink": 2,
                        "path": "/H/Hyundai/AutoDeploy/PRD/local_6/deployments",
                        "rgrp": true,
                        "roth": true,
                        "rusr": true,
                        "size": 4096,
                        "uid": 780200029,
                        "wgrp": true,
                        "woth": false,
                        "wusr": true,
                        "xgrp": true,
                        "xoth": true,
                        "xusr": true
                    }
                ],
                "invocation": {
                    "module_args": {
                        "age": null,
                        "age_stamp": "mtime",
                        "contains": null,
                        "file_type": "directory",
                        "follow": false,
                        "get_checksum": false,
                        "hidden": false,
                        "paths": [
                            "/H"
                        ],
                        "patterns": [
                            "deployments"
                        ],
                        "recurse": true,
                        "size": null,
                        "use_regex": false
                    }
                },
                "item": "/H",
                "matched": 1,
                "msg": ""
            }
        ]

Playbook: 

---
 - name: deployment and syntaxCheck dir group verfication
   become: yes
   hosts: P98
   gather_facts: no
   tasks:
   - name: checking for deployments
     find:
      paths: "{{ item }}"
      patterns: "deployments"
      recurse: yes
      file_type: directory
     with_items: "{{ path }}"
     register: find_result

   - name: display the output of find
     debug: var=find_result

   - name: change the group ownership of deployments
     file:
      path: "{{ item.path }}"
      group: sag
     with_items:
     - "{{ find_result.results | map(attribute='files') | list }}"
      #with_items: "{{ find_result.results[0].files }}"

1 个答案:

答案 0 :(得分:1)

这个过滤器序列会让您在列表中paths逐个解析:

- name: print paths
  debug:
    msg: "{{ item }}"
  with_items:
    - "{{ ansible_variable | map(attribute='files') | sum(start=[]) | map(attribute='path') | list }}"

输出:

TASK [print paths] **************************************************************************************************************************************************************************************************
ok: [localhost] => (item=None) => {
    "msg": "/F/Ford/AutoDeploy/PRD/local_1/deployments"
}
ok: [localhost] => (item=None) => {
    "msg": "/H/Hyundai/AutoDeploy/PRD/local_6/deployments"
}