# display information returned with debug in a more usable layout

**URL:** <https://forum.ansible.com/t/display-information-returned-with-debug-in-a-more-usable-layout/36865>\
**Category:** Ansible Project\
**Created:** [July 18, 2022, 4:15pm UTC](https://forum.ansible.com/t/display-information-returned-with-debug-in-a-more-usable-layout/36865 "2022-07-18T16:15:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 18, 2022, 4:15pm UTC](https://forum.ansible.com/t/display-information-returned-with-debug-in-a-more-usable-layout/36865/1 "2022-07-18T16:15:19Z")

</div>

I am trying to retreive information about installed versions of a package on a couple of hosts.

- name: check pgBackRest version  
command: /usr/bin/pgbackrest version  
register: backrest\_version

- name: Print return information from the previous task  
ansible.builtin.debug:  
var: backrest\_version

the output I am getting is already contains the information wanted but is not sufficiently handy format-wise.especially running on more then a handfull of hosts.

TASK [Print return information from the previous task] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [step-0227] =\> {  
“backrest\_version”: {  
“changed”: true,  
“cmd”: [  
“/usr/bin/pgbackrest”,  
“version”  
],  
“delta”: “0:00:00.008057”,  
“end”: “2022-07-18 18:08:20.394231”,  
“failed”: false,  
“msg”: “”,  
“rc”: 0,  
“start”: “2022-07-18 18:08:20.386174”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “pgBackRest 2.39”,  
“stdout\_lines”: [  
“ **pgBackRest 2.39** ”  
]  
}  
}

To make this more scalable I am wondering to how to tweak this further so I can get a more usable kind of list or csv with only the concrete information needed like domainname & the version of my package in question. So what would be the direction to investigat in order to move towards the happyplace in this endeavour?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 18, 2022, 5:54pm UTC](https://forum.ansible.com/t/display-information-returned-with-debug-in-a-more-usable-layout/36865/2 "2022-07-18T17:54:13Z")

</div>

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ok: [step-0227] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"backrest\_version": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"stdout": "pgBackRest 2.39",

> ... get a more usable kind of list or csv with ... domainname & the  
> version of my package in question.

Get versions and create dictionary \*pkg\*

&nbsp;&nbsp;ver: "{{ ansible\_play\_hosts|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('extract', hostvars, ['backrest\_version', 'stdout'])|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('split', ' ')|map('last')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"  
&nbsp;&nbsp;pkg: "{{ dict(ansible\_play\_hosts|zip(ver)) }}"

For example, given the data

&nbsp;&nbsp;ok: [test\_11] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;backrest\_version.stdout: pgBackRest 2.39  
&nbsp;&nbsp;ok: [test\_12] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;backrest\_version.stdout: pgBackRest 2.40  
&nbsp;&nbsp;ok: [test\_13] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;backrest\_version.stdout: pgBackRest 2.41

you get the dictionary

&nbsp;&nbsp;pkg:  
&nbsp;&nbsp;&nbsp;&nbsp;test\_11: '2.39'  
&nbsp;&nbsp;&nbsp;&nbsp;test\_12: '2.40'  
&nbsp;&nbsp;&nbsp;&nbsp;test\_13: '2.41'

Write the CSV file

&nbsp;&nbsp;&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: pkg.csv  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for k,v in pkg.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ k }},{{ v }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

You get

&nbsp;&nbsp;\> cat pkg.csv  
&nbsp;&nbsp;test\_11,2.39  
&nbsp;&nbsp;test\_12,2.40  
&nbsp;&nbsp;test\_13,2.41

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 20, 2022, 11:49am UTC](https://forum.ansible.com/t/display-information-returned-with-debug-in-a-more-usable-layout/36865/3 "2022-07-20T11:49:50Z")

</div>

thx for the suggestions. I am sure they are grant, but I seem not to be at level to put them to work correctly yet.  
So, would you mind to specify how to utilize those lines successfully?

my interpretation looked like this:

######### playbook #########################################  
- hosts: all  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;ansible\_user: gwagner

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: check pgBackRest version  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.command: /usr/bin/pgbackrest version  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: backrest\_version

&nbsp;&nbsp;&nbsp;&nbsp;- name: Print return information from the previous task  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: backrest\_version

&nbsp;&nbsp;&nbsp;&nbsp;- name: Get versions and create dictionary \*pkg\*  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ver: "{{ ansible\_play\_hosts | map('extract', hostvars, ['backrest\_version', 'stdout']) | map('split', ' ') | map('last') | list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pkg: "{{ dict(ansible\_play\_hosts|zip(ver)) }}"

&nbsp;&nbsp;&nbsp;&nbsp;- name: Write the CSV file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: pkg.csv  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for k,v in pkg.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ k }},{{ v }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 20, 2022, 12:03pm UTC](https://forum.ansible.com/t/display-information-returned-with-debug-in-a-more-usable-layout/36865/4 "2022-07-20T12:03:00Z")

</div>

You're welcome. Put the declarations of \*ver\* and \*pkg\* into the  
\*vars\*. I edited your code in-line

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [July 20, 2022, 12:48pm UTC](https://forum.ansible.com/t/display-information-returned-with-debug-in-a-more-usable-layout/36865/5 "2022-07-20T12:48:51Z")

</div>

thnks, that worked (of course).

I try not to be embarrased too much for having someone spelling this outfor me like this :-).  
Hopefully I'll be able to pay back to this list later down the road.

I see there is a lot to learn and understand yet (in this case "use of variables" for example).

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 20, 2022, 5:29pm UTC](https://forum.ansible.com/t/display-information-returned-with-debug-in-a-more-usable-layout/36865/6 "2022-07-20T17:29:31Z")

</div>

> I see there is a lot to learn and understand yet (in this case "use  
> of variables" for example).

> \> \> - hosts: all  
> \> \> vars:  
> \> \> ansible\_user: gwagner  
> \> \> ver: "{{ ansible\_play\_hosts|  
> \> \> map('extract', hostvars, ['backrest\_version', 'stdout'])|  
> \> \> map('split', ' ')|map('last')|  
> \> \> list }}"  
> \> \> pkg: "{{ dict(ansible\_play\_hosts|zip(ver)) }}"  
> \> \>  
> \> \> tasks:  
> \> \> - name: check pgBackRest version  
> \> \> ansible.builtin.command: /usr/bin/pgbackrest version  
> \> \> register: backrest\_version  
> \> \>  
> \> \> - name: Print return information from the previous task  
> \> \> ansible.builtin.debug:  
> \> \> var: backrest\_version.stdout  
> \> \> when: debug|d(false)|bool  
> \> \>  
> \> \> - name: Write the CSV file  
> \> \> ansible.builtin.copy:  
> \> \> dest: pkg.csv  
> \> \> content: |-  
> \> \> {% for k,v in pkg.items() %}  
> \> \> {{ k }},{{ v }}  
> \> \> {% endfor %}  
> \> \> delegate\_to: localhost  
> \> \> run\_once: true

There are a couple of aspects.

\* It's good to know that in Ansible the variables are evaluated at  
&nbsp;&nbsp;the last moment, aka "lazy evaluation".

\* There are many places where you can declare variables. See  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)

\* Where would you like to use the variables? In your case, this is  
&nbsp;&nbsp;probably the best question to ask when you want to decide where to  
&nbsp;&nbsp;declare the variables. Put them into the playbook vars (above) if  
&nbsp;&nbsp;you want to use them in the whole playbook. If you need the  
&nbsp;&nbsp;variables to write the CSV file only put them into the task vars  
&nbsp;&nbsp;(below)

&nbsp;&nbsp;- name: Write the CSV file  
&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: pkg.csv  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for k,v in pkg.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ k }},{{ v }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ver: "{{ ansible\_play\_hosts|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('extract', hostvars,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;['backrest\_version','stdout'])|map('split', ' ')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('last')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pkg: "{{ dict(ansible\_play\_hosts|zip(ver)) }}"

&nbsp;&nbsp;This limits the scope of the variables. See  
&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#scoping-variables](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#scoping-variables)

\* To make the code cleaner, you can put the variables into some files.  
&nbsp;&nbsp;For example into the group\_vars,

&nbsp;&nbsp;\> cat group\_vars/all  
&nbsp;&nbsp;ansible\_user: gwagner  
&nbsp;&nbsp;ver: "{{ ansible\_play\_hosts|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('extract', hostvars, ['backrest\_version', 'stdout'])|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('split', ' ')|map('last')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"  
&nbsp;&nbsp;pkg: "{{ dict(ansible\_play\_hosts|zip(ver)) }}"  
&nbsp;&nbsp;content: |-  
&nbsp;&nbsp;&nbsp;&nbsp;{% for k,v in pkg.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;{{ k }},{{ v }}  
&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

&nbsp;&nbsp;This will simplify the playbook

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: check pgBackRest version  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.command: /usr/bin/pgbackrest version  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: backrest\_version  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Write the CSV file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ansible.builtin.copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: pkg.csv  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ content }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true
