Help printing out key/value pair from a register?

27 views
Skip to first unread message

ZillaYT

unread,
Feb 5, 2018, 11:10:48 AM2/5/18
to Ansible Project
I have a register with the following output of a query from the acl module.

ok: [server.domain.com] => {
   
"acl_query.results": [
       
{
           
"_ansible_item_result": true,
           
"_ansible_no_log": false,
           
"changed": false,
           
"item": [
               
{
                   
"path": "/local/py/virtualenvs/service_mix/logs"
               
},
               
{
                   
"etype": "other",
                   
"permissions": "rwx"
               
}
           
],
           
"skip_reason": "Conditional result was False",
           
"skipped": true
       
},
       
{
           
"_ansible_item_result": true,
           
"_ansible_no_log": false,
           
"changed": false,
           
"item": [
               
{
                   
"path": "/local/py/virtualenvs/service_mix/tmp_files"
               
},
               
{
                   
"etype": "other",
                   
"permissions": "rwx"
               
}
           
],
           
"skip_reason": "Conditional result was False",
           
"skipped": true
       
},
       
{
           
"_ansible_item_result": true,
           
"_ansible_no_log": false,
           
"_ansible_parsed": true,
           
"acl": [
               
"user::rwx",
               
"user:cva:rwx",
               
"user:scmauto:rwx",
               
"group::r-x",
               
"mask::rwx",
               
"other::r-x",
               
"default:user::rwx",
               
"default:user:cva:rwx",
               
"default:user:scmauto:rwx",
               
"default:group::r-x",
               
"default:mask::rwx",
               
"default:other::r-x"
           
],
           
"changed": false,
           
"failed": false,
           
"invocation": {
               
"module_args": {
                   
"default": false,
                   
"entity": "",
                   
"entry": null,
                   
"etype": null,
                   
"follow": true,
                   
"path": "/local/httpd",
                   
"permissions": null,
                   
"recursive": false,
                   
"state": "query",
                   
"use_nfsv4_acls": false
               
}
           
},
           
"item": [
               
{
                   
"path": "/local/httpd"
               
},
               
{
                   
"entity": "my_user",
                   
"etype": "user",
                   
"permissions": "rwx"
               
}
           
],
           
"msg": "current acl"
       
}
   
]
}


How can I write a debug module that will show me, for each item in results array, the path, entity, etype, and permissions values?

- debug: msg="{{ acl_query.results.??? }}"

(Why am I so bad at this? LOL)

Claudia de Luna

unread,
Feb 5, 2018, 8:14:21 PM2/5/18
to Ansible Project
Hi ZillaYT,

Its sometimes difficult to figure out how to get to specific values that are returned.  Some times you have to "deconstruct" it.

If you take the output you get when you run your playbook and paste it into something like https://jsoneditoronline.org/ you can start to understand the structure and that will make it much easier to pick out the information you need.

Normally I register my output

      register: facts_output

and then I start drilling down.  Looking at the data structure in something that helps you visualize it helps me.  So this is a list of dictionaries and 

    
- debug: var=facts_output
    
- debug: var=facts_output.ansible_facts.ansible_net_version


Here is a longer version but if you figure out your data structure you don't have to do this "trial and error" method.

      # save the command output in a variable called "output"
      register: output
      # print the contents of the variable "output" which is a dictionary of lists of dictionaries
    - debug: var=output
      # print the first command that was run
    - debug: var=output.results[0].item
      # print the command results with line feeds
    - debug: var=output.results[0].stdout_lines
      # print the list item #72 
    - debug: var=output.results[0].stdout_lines[0][72]

See this email exchange on a similar topic.

So you are going to want to see (assuming you save (register) your output into avariable called "output":
- debug: var=output.acl_query.results['2'].item[0].path
- debug: var=output.acl_query.results['2'].item[1].entity
- debug: var=output.acl_query.results['2'].item[1].etype
- debug: var=output.acl_query.results['2'].item[1].permissions

..you get the idea

Reply all
Reply to author
Forward
0 new messages