Parsing var created with loop

17 views
Skip to first unread message

John Harmon

unread,
Feb 22, 2018, 4:26:01 PM2/22/18
to Ansible Project
I have the following output from a variable called directories.  It was created using a loop that would run stat against two different paths.  What is the proper way to reference the "stat: exists: boolean" sections of the output?  I need to check for both directories for both item.paths.  I have tried and tried, but I must be doing something wrong.
    "directories.results": [
       
{
           
"_ansible_ignore_errors": null,
           
"_ansible_item_result": true,
           
"_ansible_no_log": false,
           
"_ansible_parsed": true,
           
"changed": false,
           
"failed": false,
           
"invocation": {
               
"module_args": {
                   
"checksum_algorithm": "sha1",
                   
"follow": false,
                   
"get_attributes": true,
                   
"get_checksum": true,
                   
"get_md5": true,
                   
"get_mime": true,
                   
"path": "/home/applmgr/Scripts"
               
}
           
},
           
"item": {
               
"path": "/home/applmgr/Scripts"

           
},
           
"stat": {
               
"atime": 1519330068.2478893,
               
"attr_flags": "e",
               
"attributes": [
                   
"extents"
               
],
               
......lots of stuff
                 "exists": true,
                ......lots of stuff
           
}
       
},
       
{
           
"_ansible_ignore_errors": null,
           
"_ansible_item_result": true,
           
"_ansible_no_log": false,
           
"_ansible_parsed": true,
           
"changed": false,
           
"failed": false,
           
"invocation": {
               
"module_args": {
                   
"checksum_algorithm": "sha1",
                   
"follow": false,
                   
"get_attributes": true,
                   
"get_checksum": true,
                   
"get_md5": true,
                   
"get_mime": true,
                   
"path": "/home/oracle/Scripts"
               
}
           
},
       
    "item": {
               
"path": "/home/oracle/Scripts"
           
},
           
"stat": {
               
"exists": false

           
}
       
}
   
]
}


Kai Stian Olstad

unread,
Feb 22, 2018, 4:57:45 PM2/22/18
to ansible...@googlegroups.com
On Thursday, 22 February 2018 22.26.01 CET John Harmon wrote:
> I have the following output from a variable called directories. It was
> created using a loop that would run stat against two different paths. What
> is the proper way to reference the "stat: exists: boolean" sections of the
> output? I need to check for both directories for both item.paths. I have
> tried and tried, but I must be doing something wrong.
> "directories.results": [
> {
> "_ansible_ignore_errors": null,
> "_ansible_item_result": true,
> "_ansible_no_log": false,
> "_ansible_parsed": true,
> "changed": false,
> "failed": false,
> "invocation": {
> "module_args": {
> "checksum_algorithm": "sha1",
> "follow": false,
> "get_attributes": true,
> "get_checksum": true,
> "get_md5": true,
> "get_mime": true,
> "path": "/home/applmgr/Scripts"
> }
> },
>
> *"item": { "path": "/home/applmgr/Scripts"*
> },
> "stat": {
> "atime": 1519330068.2478893,
> "attr_flags": "e",
> "attributes": [
> "extents"
> ],
> ......lots of stuff
> * "exists": true,*
> ......lots of stuff
> }
> },
> {
> "_ansible_ignore_errors": null,
> "_ansible_item_result": true,
> "_ansible_no_log": false,
> "_ansible_parsed": true,
> "changed": false,
> "failed": false,
> "invocation": {
> "module_args": {
> "checksum_algorithm": "sha1",
> "follow": false,
> "get_attributes": true,
> "get_checksum": true,
> "get_md5": true,
> "get_mime": true,
> "path": "/home/oracle/Scripts"
> }
> },
>
>
>
>
> * "item": { "path": "/home/oracle/Scripts" },
> "stat": { "exists": false*
> }
> }
> ]
> }


It depends on what you want to do next.
The fist one is
{{ directories.results.0.stats.exists }}
and the second one is
{{ directories.results.1.stats.exists }}

If you need both to be true before running a task that would be

when: directories.results.0.stats.exists and directories.results.1.stats.exists

since they are bools.


--
Kai Stian Olstad

John Harmon

unread,
Feb 22, 2018, 5:03:44 PM2/22/18
to Ansible Project
Thanks Kai.  I still see a problem:
    - name: debug
      debug
:
       
var: directories.results.0.stats.exists



Result:
TASK
[debug] *****************************************************************************************************************************************************************************************************************************
ok
: [labebs-apps1] => {
   
"directories.results.0.stats.exists": "VARIABLE IS NOT DEFINED!"
}


Is there any way to reference it by something more descriptive, like a name?


John Harmon

unread,
Feb 22, 2018, 5:07:59 PM2/22/18
to Ansible Project
Sorry, syntax error.  Had stats not stat.
Reply all
Reply to author
Forward
0 new messages