Santosh Prasad
Santosh Prasad

Reputation: 59

Looping nested list in ansible

I am using find module to get all the directories named "deployments" in different mount points(/E,/F) and then using the file module to set the group ownership in all those found dirs. Now ansible is giving the find output in nested list, and with_items is not able to loop over files list of all the mount points. How can i loop on all the nested list in my task?

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 }}"

Upvotes: 0

Views: 3870

Answers (1)

ilias-sp
ilias-sp

Reputation: 6705

this sequence of filters will get you the paths in a list to parse one by one:

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

output:

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"
}

Upvotes: 2

Related Questions