# Formatting the dict items with debug

**URL:** <https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520>\
**Category:** Ansible Project\
**Created:** [October 14, 2023, 8:01am UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520 "2023-10-14T08:01:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 14, 2023, 8:01am UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/1 "2023-10-14T08:01:28Z")

</div>

Hi,

I have the below output from my playbook using

- debug:  
msg: “{{ result.stdout }}”

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“access\_token”: “xxxxxxxxxx”,  
“expires\_in”: 43200,  
“token\_type”: “xxxxxx”  
}  
}

and when I tried to filter the access token alone , with map by  
“{{ result.stdout | map(‘extract’, access\_token) }}” ., it errors as below

The task includes an option with an undefined variable. The error was: ‘access\_token’ is undefined\n\n

and when I tried to filter the access token alone , with map by  
msg: “{{ result.stdout | map(attribute=‘access\_token’) }}” ., it errors as below

What I am missing here to get the desired output here?

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [October 14, 2023, 12:31pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/2 "2023-10-14T12:31:23Z")

</div>

What is your desired output then??

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 14, 2023, 12:43pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/3 "2023-10-14T12:43:40Z")

</div>

I need only the output or the value of the below(trying to set\_fact the access\_token)  
access\_token": “xxxxxxxxxx”

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [October 14, 2023, 12:59pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/4 "2023-10-14T12:59:01Z")

</div>

Try piping it through from\_json first

---

<div class="post-metadata">

**Author:** ![Will\_McDonald](https://avatars.discourse-cdn.com/v4/letter/w/f475e1/32.png) [@Will\_McDonald](https://forum.ansible.com/u/Will_McDonald)\
**Post date:** [October 14, 2023, 1:01pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/5 "2023-10-14T13:01:17Z")

</div>

Is there any reason you can’t just use result.stdout.access\_token?

$ cat debug-token.yml

- name: debug vars  
hosts: localhost  
gather\_facts: no

vars:  
result:  
stdout:  
access\_token: xxxxxxxxxx  
expires\_in: 4320  
token\_type: yyyyyyyyyy

tasks:

- name: debug the result  
ansible.builtin.debug:  
var: result

- name: extract the token  
ansible.builtin.debug:  
msg: “{{ result.stdout.access\_token }}”

$ ansible-playbook debug-token.yml

PLAY [debug vars] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [debug the result] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“result”: {  
“stdout”: {  
“access\_token”: “xxxxxxxxxx”,  
“expires\_in”: 4320,  
“token\_type”: “yyyyyyyyyy”  
}  
}  
}

TASK [extract the token] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “xxxxxxxxxx”  
}

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 14, 2023, 2:09pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/6 "2023-10-14T14:09:59Z")

</div>

when I try with

- debug:  
msg: “{{ result }}”

the output is

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“changed”: true,  
“cmd”: “curl -X POST ‘[https://xxxxxx.com/auth/login](https://xxxxxx.com/auth/login)’ -H ‘Content-Type: application/json’ -d @.mycred.json”,  
– trimmed lines ----  
“rc”: 0,  
– trimmed lines ----  
“stdout”: “{"access\_token":"XXXXX","token\_type":"Bearer","expires\_in":86400}”,  
“stdout\_lines”: [  
“{"access\_token":"XXXXX","token\_type":"Bearer","expires\_in":86400}”  
]  
}  
}

The output for the

- debug:  
msg: “{{ result.stdout |from\_json }}”

is

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“access\_token”: “XXXXXX”,  
“expires\_in”: 86400,  
“token\_type”: “Bearer”  
}  
}

when I use the

- debug:  
msg: “{{ result.stdout.access\_token }}”

the error is

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘ansible.utils.unsafe\_proxy.AnsibleUnsafeText object’ has no attribute ‘access\_token’\n\nThe error appears to be in ‘/home/user/check.yml’: line 32, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug:\n ^ here\n”}

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [October 14, 2023, 3:05pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/7 "2023-10-14T15:05:37Z")

</div>

Yes, so you should combine them like:

- debug:  
msg: “{{ (result.stdout|from\_json).access\_token }}”

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 14, 2023, 3:24pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/8 "2023-10-14T15:24:42Z")

</div>

It worked and its my miss again to combine it … 🙂

---

<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:** [October 14, 2023, 3:28pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/9 "2023-10-14T15:28:04Z")

</div>

> - debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ result.stdout }}"
> 
> ok: [localhost] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"access\_token": "xxxxxxxxxx",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"expires\_in": 43200,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"token\_type": "xxxxxx"  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }

For example, given the file

> cat /tmp/test.json

{  
"access\_token": "xxxxxxxxxx",  
"expires\_in": 43200,  
"token\_type": "xxxxxx"  
}

The value of \*result.stdout\* is \*AnsibleUnsafeText\*. What you see  
depends on the callback. The below play

> cat pb.yml

- hosts: all  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- command: cat /tmp/test.json  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.stdout

gives JSON with the default callback

> ANSIBLE\_STDOUT\_CALLBACK=default ansible-playbook pb.yml

&nbsp;&nbsp;...  
ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"result.stdout": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"access\_token": "xxxxxxxxxx",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"expires\_in": 43200,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"token\_type": "xxxxxx"  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

The same task gives YAML with the yaml callback

> ANSIBLE\_STDOUT\_CALLBACK=yaml ansible-playbook pb2.yml

&nbsp;&nbsp;...  
ok: [localhost] =\>  
&nbsp;&nbsp;result.stdout:  
&nbsp;&nbsp;&nbsp;&nbsp;access\_token: xxxxxxxxxx  
&nbsp;&nbsp;&nbsp;&nbsp;expires\_in: 43200  
&nbsp;&nbsp;&nbsp;&nbsp;token\_type: xxxxxx

You can test the type of the attribute \*stdout\*

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.stdout|type\_debug

gives

&nbsp;&nbsp;result.stdout|type\_debug: AnsibleUnsafeText

> filter the access token alone

Convert the text to dictionary and get the \*access\_token\* as well.  
It's up to you where you put the vars

&nbsp;&nbsp;vars:

&nbsp;&nbsp;&nbsp;&nbsp;test\_data: "{{ result.stdout|from\_json }}"  
&nbsp;&nbsp;&nbsp;&nbsp;access\_token: "{{ test\_data.access\_token }}"

Example of a complete playbook for testing

> cat pb.yml

- hosts: all

&nbsp;&nbsp;vars:

&nbsp;&nbsp;&nbsp;&nbsp;test\_data: "{{ result.stdout|from\_json }}"  
&nbsp;&nbsp;&nbsp;&nbsp;access\_token: "{{ test\_data.access\_token }}"

&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;- command: cat /tmp/test.json  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.stdout  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: result.stdout|type\_debug  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: test\_data|type\_debug  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: test\_data  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: access\_token

---

<div class="post-metadata">

**Author:** ![Veera](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/veera/32/4107_2.png) [@Veera](https://forum.ansible.com/u/Veera)\
**Post date:** [October 14, 2023, 4:50pm UTC](https://forum.ansible.com/t/formatting-the-dict-items-with-debug/38520/10 "2023-10-14T16:50:31Z")

</div>

Thanks Vladimir … rich info for me …
