Need help for json query syntax

29 views
Skip to first unread message

Ramesh AR

unread,
Dec 30, 2020, 9:46:37 AM12/30/20
to Ansible Project
Folks, trying to get json query for the below data's.

Tried the below queries but didn't get output as expected. can someone help me . 
Try 1:
++++++++++++++++++++++++++
   register: result
  - name: Display PSU status
    debug: msg="{{ result.json | json_query(jmesquery) }}"
    vars:
      jmesquery: "[*].{dn: dn, id: id}"
++++++++++++++++++++++++++++
Try:2
    # msg: "{{ result|json_query('configResolveClass.children[0].outConfigs.children[0].equipmentPsu.attributes.equipmentPsu.attributes[0].{dn: dn, id: id, operability: operability, presence:presence}') }}"
+++++++++++++++++++++++++++++

Ansible Facts:
+++++++++++++++++++++++++++
   "aaaLogin": {
        "attributes": {
            "cookie": "",
            "outCookie": "1609339398/bec3ca01-b7af-17af-a0f6-bc16ee6a0f70",
            "outPriv": "admin",
            "outRefreshPeriod": "600",
            "outSessionId": "8446",
            "outVersion": "3.1(3a)",
            "response": "yes"
        }
    },
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "configResolveClass": {
        "attributes": {
            "classId": "equipmentPsu",
            "cookie": "1609339398/bec3ca01-b7af-17af-a0f6-bc16ee6a0f70",
            "response": "yes"
        },
        "children": [
            {
                "outConfigs": {
                    "attributes": {},
                    "children": [
                        {
                            "equipmentPsu": {
                                "attributes": {
                                    "dn": "sys/rack-unit-1/psu-1",
                                    "fwVersion": "12002216",
                                    "id": "1",
                                    "input": "135",
                                    "maxOutput": "NA",
                                    "model": "700-014160-0000",
                                    "operability": "operable",
                                    "output": "118",
                                    "pid": "UCSC-PSU1-770W",
                                    "power": "on",
                                    "presence": "equipped",
                                    "serial": "ART2221F58Q",
                                    "thermal": "ok",
                                    "vendor": "Cisco Systems Inc",
                                    "voltage": "ok"
                                }
                            }
                        },
                        {
                            "equipmentPsu": {
                                "attributes": {
                                    "dn": "sys/rack-unit-1/psu-2",
                                    "fwVersion": "12002216",
                                    "id": "2",
                                    "input": "140",
                                    "maxOutput": "NA",
                                    "model": "700-014160-0000",
                                    "operability": "operable",
                                    "output": "127",
                                    "pid": "UCSC-PSU1-770W",
                                    "power": "on",
                                    "presence": "equipped",
                                    "serial": "ART2221F4XS",
                                    "thermal": "ok",
                                    "vendor": "Cisco Systems Inc",
                                    "voltage": "ok"
                                }
                            }
                        }
                    ]
                }
            }
        ]
    },
    "elapsed": 3,
    "invocation": {
        "module_args": {
            "content": "<configResolveClass inHierarchical=\"false\" classId=\"equipmentPsu\"/>\n",
            "hostname": "host1",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "path": null,
            "protocol": "https",
            "timeout": 60,
            "username": "VIPER_VIO",
            "validate_certs": false
        }
    },
    "response": "OK (unknown bytes)",
    "status": 200
}
++++++++++++++++++++++++++++++++++++++++++++++++++++
Thanks,
Ramesh

Vladimir Botka

unread,
Dec 30, 2020, 10:29:19 AM12/30/20
to Ramesh AR, ansible...@googlegroups.com
On Wed, 30 Dec 2020 06:46:37 -0800 (PST)
Ramesh AR <rames...@gmail.com> wrote:

> register: result
> - name: Display PSU status
> debug: msg="{{ result.json | json_query(jmesquery) }}"
> vars:
> jmesquery: "[*].{dn: dn, id: id}"

Post the (link to) "result". For example

- debug:
var: result.json

You might want to paste it
https://alternativeto.net/software/pastebin/

--
Vladimir Botka

Ramesh AR

unread,
Dec 30, 2020, 11:21:12 AM12/30/20
to Ansible Project
TASK [debug] *************************************************************************************************************************************
ok: [Host1] => {
    "result": {
        "aaaLogin": {
            "attributes": {
                "cookie": "",
                "outCookie": "1609345308/14c4aad7-b7b1-17b1-a10e-bc16ee6a0f70",
                "outPriv": "admin",
                "outRefreshPeriod": "600",
                "outSessionId": "8470",
                "outVersion": "3.1(3a)",
                "response": "yes"
            }
        },
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "changed": false,
        "configResolveClass": {
            "attributes": {
                "classId": "equipmentPsu",
                "cookie": "1609345308/14c4aad7-b7b1-17b1-a10e-bc16ee6a0f70",
                "response": "yes"
            },
            "children": [
                {
                    "outConfigs": {
                        "attributes": {},
                        "children": [
                            {
                                "equipmentPsu": {
                                    "attributes": {
                                        "dn": "sys/rack-unit-1/psu-1",
                                        "fwVersion": "12002216",
                                        "id": "1",
                                        "input": "138",
                                        "maxOutput": "NA",
                                        "model": "700-014160-0000",
                                        "operability": "operable",
                                        "output": "112",
                                        "pid": "UCSC-PSU1-770W",
                                        "power": "on",
                                        "presence": "equipped",
                                        "serial": "ART2221F58Q",
                                        "thermal": "ok",
                                        "vendor": "Cisco Systems Inc",
                                        "voltage": "ok"
                                    }
                                }
                            },
                            {
                                "equipmentPsu": {
                                    "attributes": {
                                        "dn": "sys/rack-unit-1/psu-2",
                                        "fwVersion": "12002216",
                                        "id": "2",
                                        "input": "145",
                                        "maxOutput": "NA",
                                        "model": "700-014160-0000",
                                        "operability": "operable",
                                        "output": "118",
                                        "pid": "UCSC-PSU1-770W",
                                        "power": "on",
                                        "presence": "equipped",
                                        "serial": "ART2221F4XS",
                                        "thermal": "ok",
                                        "vendor": "Cisco Systems Inc",
                                        "voltage": "ok"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        },
        "elapsed": 3,
        "failed": false,
        "response": "OK (unknown bytes)",
        "status": 200
    }
}

PLAY RECAP ***************************************************************************************************************************************
host1               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Dick Visser

unread,
Dec 30, 2020, 5:56:25 PM12/30/20
to ansible...@googlegroups.com
On Wed, 30 Dec 2020 at 15:46, Ramesh AR <rames...@gmail.com> wrote:
Folks, trying to get json query for the below data's.

Tried the below queries but didn't get output as expected. can someone help me . 

Apologies for the obvious question, but what output do you expect?





--
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/2da6748f-ae6e-4f05-8def-8d71520a43c3n%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

Ramesh AR

unread,
Dec 30, 2020, 6:11:00 PM12/30/20
to Ansible Project
Hello Dick,

I'm trying to get like the below format  and also its listing I wanted to join (' , ')  "dn": "sys/rack-unit-1/psu-1"  , "id": "1" 

 "msg": The PSU output is, {
        "dn": "sys/rack-unit-1/psu-1",
        "id": "1",
        "operability": "operable",
        "presence": "equipped"

      "dn": "sys/rack-unit-1/psu-2",
        "id": "2",
        "operability": "operable",
        "presence": "equipped"
    }
}

Thanks,
Ramesh 

Vladimir Botka

unread,
Dec 30, 2020, 9:21:15 PM12/30/20
to Ramesh AR, ansible...@googlegroups.com
On Wed, 30 Dec 2020 15:11:00 -0800 (PST)
Ramesh AR <rames...@gmail.com> wrote:

> "msg": The PSU output is, {
> "dn": "sys/rack-unit-1/psu-1",
> "id": "1",
> "operability": "operable",
> "presence": "equipped"
>
> "dn": "sys/rack-unit-1/psu-2",
> "id": "2",
> "operability": "operable",
> "presence": "equipped"
> }
> }

Try this
https://gist.github.com/vbotka/7ed043e08550e83a929593e3ebd510b9

This is the same

- set_fact:
PSU: "{{ result.configResolveClass.children|
json_query(my_query1)|
json_query(my_query2)|
json_query(my_query3) }}"
vars:
my_query1: "[].outConfigs.children"
my_query2: "[].equipmentPsu.attributes"
my_query3: "[].{dn: dn,
id: id,
operability: operability,
presence: presence}"

--
Vladimir Botka

Ramesh AR

unread,
Dec 31, 2020, 10:23:27 AM12/31/20
to Ansible Project
Hello Vladimir,

Thanks for the help.  however, I set fact on my playbook and doesn't show any output. 

 - set_fact:
       PSU: "{{ result.configResolveClass.children|json_query(my_query1)|json_query(my_query2)|json_query(my_query3) }}"
    vars:
       my_query1: "[].outConfigs.children"
       my_query2: "[].equipmentPsu.attributes"
       my_query3: "[].{dn: dn, d: id,operability: operability,presence: presence}"


PLAY [mdhd] **************************************************************************************************************************************

TASK [Checking PSU status] ***********************************************************************************************************************
ok: [host1]

TASK [set_fact] **********************************************************************************************************************************
ok: [host1]

PLAY RECAP ***************************************************************************************************************************************
host1               : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Thanks,
Ramesh 

Vladimir Botka

unread,
Dec 31, 2020, 10:42:19 AM12/31/20
to Ramesh AR, ansible...@googlegroups.com
On Thu, 31 Dec 2020 07:23:27 -0800 (PST)
Ramesh AR <rames...@gmail.com> wrote:

> Thanks for the help. however, I set fact on my playbook and doesn't show
> any output.

> > https://gist.github.com/vbotka/7ed043e08550e83a929593e3ebd510b9

This is strange. I added the file with the data. Both versions of the
code work for me

PSU:
- dn: sys/rack-unit-1/psu-1
id: '1'
operability: operable
presence: equipped
- dn: sys/rack-unit-1/psu-2
id: '2'
operability: operable
presence: equipped

--
Vladimir Botka

Ramesh AR

unread,
Dec 31, 2020, 10:48:04 AM12/31/20
to Ansible Project
Ok. here's my playbook.

when I use -vvv option, I see those outputs. 
+++++++++++++++++++++++++++++++++
---
- hosts: mdhd
  connection: local
  gather_facts: no
  vars_files:
    - /etc/ansible/var.yml
  tasks:
  - name: Checking PSU status
    imc_rest:
      hostname: "{{ imc_hostname }}"
      username: "{{ imc_username }}"
      password: "{{ imc_password }}"
      protocol: https
      timeout: 60
      validate_certs: no
      content: |
           <configResolveClass inHierarchical="false" classId="equipmentPsu"/>
    register: result
  - set_fact:
      PSU: "{{ result.configResolveClass.children|json_query(my_query1)|json_query(my_query2)|json_query(my_query3) }}"
    vars:
      my_query1: "[].outConfigs.children"
      my_query2: "[].equipmentPsu.attributes"
      my_query3: "[].{dn: dn, id: id, operability: operability, presence: presence}"

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

TASK [set_fact] **********************************************************************************************************************************
task path: /etc/ansible/validation_psu.yml:19
ok: [host1] => {
    "ansible_facts": {
        "PSU": [
            {
                "dn": "sys/rack-unit-1/psu-1",
                "id": "1",
                "operability": "operable",
                "presence": "equipped"
            },
            {
                "dn": "sys/rack-unit-1/psu-2",
                "id": "2",
                "operability": "operable",
                "presence": "equipped"
            }
        ]
    },
    "changed": false
}
META: ran handlers
META: ran handlers

Thanks,
Ramesh

Ramesh AR

unread,
Dec 31, 2020, 11:00:15 AM12/31/20
to Ansible Project
Ok got it . I figured out .. 

just added in mypalybook 
- debug: var=PSU

PLAY [mdhd] **************************************************************************************************************************************

TASK [Checking PSU status] ***********************************************************************************************************************
ok: [host1]

TASK [set_fact] **********************************************************************************************************************************
ok: [host1]

TASK [debug] *************************************************************************************************************************************
ok: [host1] => {
    "PSU": [
        {
            "dn": "sys/rack-unit-1/psu-1",
            "id": "1",
            "operability": "operable",
            "presence": "equipped"
        },
        {
            "dn": "sys/rack-unit-1/psu-2",
            "id": "2",
            "operability": "operable",
            "presence": "equipped"
        }
    ]
}

PLAY RECAP ***************************************************************************************************************************************
host1              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


Reply all
Reply to author
Forward
0 new messages