【问题标题】:Looping nested list in ansible在ansible中循环嵌套列表
【发布时间】:2018-05-15 13:47:32
【问题描述】:

我正在使用 find 模块在不同的挂载点(/E,/F)中获取所有名为“deployments”的目录和 然后使用文件模块在所有找到的目录中设置组所有权。 现在 ansible 在嵌套列表中提供查找输出,并且 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 }}"

【问题讨论】:

    标签: ansible


    【解决方案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"
    }
    

    【讨论】:

    • 感谢它现在的工作,你能告诉我什么是 map , sum , |并列出在这里做的事情,或者有什么好的在线资源可以让我学习。
    • map 从你的初始变量的结构中提取files 属性,将其和用于将其转换为列表,然后我们拾取path 属性,最后转换为一个列表。您应该检查的有关过滤的 2 个重要链接是filters from ansible documentation,还有documentation from jinja。他们有很好的例子可以尝试熟悉它们。
    猜你喜欢
    • 2019-08-30
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多