Parsing json output issues

162 views
Skip to first unread message

Delmiro Campelo

unread,
Mar 6, 2022, 10:17:37 PM3/6/22
to Ansible Project
Hello Ansible friends,

I'm hoping that you can provide some assistance here. I'm trying to parsed through json response from a web application, but I'm having some issues. The goal of this playbook is to display the site-id for all  routers in the json response. Any help is appreciated, thank you.

Here is the ansible playbook. 
- hosts: vmanage
  connection: local
  gather_facts: no
  tasks:
   - vmanage_device_facts:
       user: "{{ ansible_user }}"
       host: "{{ ansible_host }}"
       password: "{{ ansible_password }}"
     register: output

   - name: Create device dictionary
     set_fact:
       device_status: '{{ output | json_query("vedges[0].["side-id"]")}}'

   - name: Print out site ids
     debug:
       msg: "{{ device_status }}"
~
Error message after running the playbook is below:
PLAY [vmanage] **********************************************************************************************************************

TASK [vmanage_device_facts] *********************************************************************************************************
[WARNING]: Module did not set no_log for password
ok: [10.10.2.2]

TASK [Create device dictionary] *****************************************************************************************************
fatal: [10.10.2.2]: FAILED! => {"msg": "template error while templating string: expected token ',', got 'side'. String: {{ output | json_query(\"vedges[0].[\"side-id\"]\")}}"}

PLAY RECAP **************************************************************************************************************************
10.10.2.2                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Dick Visser

unread,
Mar 7, 2022, 1:02:47 AM3/7/22
to ansible...@googlegroups.com
On Mon, 7 Mar 2022 at 04:18, Delmiro Campelo <delm...@gmail.com> wrote:
Hello Ansible friends,

I'm hoping that you can provide some assistance here. I'm trying to parsed through json response from a web application, but I'm having some issues. The goal of this playbook is to display the site-id for all  routers in the json response. Any help is appreciated, thank you.

Here is the ansible playbook. 
- hosts: vmanage
  connection: local
  gather_facts: no
  tasks:
   - vmanage_device_facts:
       user: "{{ ansible_user }}"
       host: "{{ ansible_host }}"
       password: "{{ ansible_password }}"
     register: output

   - name: Create device dictionary
     set_fact:
       device_status: '{{ output | json_query("vedges[0].["side-id"]")}}'

Try quoting side-id with backticks 





   - name: Print out site ids
     debug:
       msg: "{{ device_status }}"
~
Error message after running the playbook is below:
PLAY [vmanage] **********************************************************************************************************************

TASK [vmanage_device_facts] *********************************************************************************************************
[WARNING]: Module did not set no_log for password
ok: [10.10.2.2]

TASK [Create device dictionary] *****************************************************************************************************
fatal: [10.10.2.2]: FAILED! => {"msg": "template error while templating string: expected token ',', got 'side'. String: {{ output | json_query(\"vedges[0].[\"side-id\"]\")}}"}

PLAY RECAP **************************************************************************************************************************
10.10.2.2                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d0e826f5-1ad5-49cf-b429-dc87079f896bn%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Dick Visser

unread,
Mar 7, 2022, 4:57:54 AM3/7/22
to ansible...@googlegroups.com
Backticks don't work I see. This should do it:

device_status: "{{ output | json_query('vedges[0].[\'side-id\']') }}"

--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Delmiro Campelo

unread,
Mar 7, 2022, 9:07:48 AM3/7/22
to Ansible Project
Thank you for your reply Dick, the backspace didn't work here, please see error below. I tried all kinds of combinations :( Do you have any other ideas?


PLAY [vmanage] **********************************************************************************************************************

TASK [vmanage_device_facts] *********************************************************************************************************
[WARNING]: Module did not set no_log for password
ok: [10.10.2.2]

TASK [parse through output] *********************************************************************************************************
fatal: [10.10.2.2]: FAILED! => {"msg": "template error while templating string: expected token ',', got 'side'. String: device_status:\"{{ output | json_query('vedges[0].[\\\\'side-id\\\\']') }}\""}

PLAY RECAP **************************************************************************************************************************
10.10.2.2                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
 

Dick Visser

unread,
Mar 7, 2022, 10:00:03 AM3/7/22
to ansible...@googlegroups.com
Did you try this literal string?

device_status: "{{ output | json_query('vedges[0].[\'side-id\']') }}"




> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e23bad41-3472-4986-9c1e-05a36758ac33n%40googlegroups.com.

Delmiro Campelo

unread,
Mar 7, 2022, 12:39:58 PM3/7/22
to Ansible Project
Yes I did, see the playbook below please. 
- hosts: vmanage
  connection: local
  gather_facts: no
  tasks:
   - vmanage_device_facts:
       user: "{{ ansible_user }}"
       host: "{{ ansible_host }}"
       password: "{{ ansible_password }}"
     register: output

   - name: parse through output

     set_fact:
       device_status:"{{ output | json_query('vedges[0].[\'side-id\']') }}"


   - name: Print out neighbors
     debug:
       msg: "{{  device_status   }}"

Hamza Bouabdallah

unread,
Mar 7, 2022, 5:01:33 PM3/7/22
to Ansible Project

Try converting the data to something that ansible can parse using the from_json filter, then just access it using the json path that  in your case ends with vedges[0]['side-id']
Also why do put a dot between [0] and ['side-id'] ? no need

Delmiro Campelo

unread,
Mar 17, 2022, 6:29:26 PM3/17/22
to Ansible Project
for some reason, I couldn't get it to work. I ended up using Ansible URI module to get the information from API and parse it after. It works now. Thank you to all that responded.
Reply all
Reply to author
Forward
0 new messages