How do I access the cpu %usage in this example? I'm getting an error no matter what I try.

Hi,

I am using the Napalm module with Ansible and using its get_facts module to obtain some basic information from the device. The relevant output is below,

`
“ansible_facts”: {
“napalm_environment”: {
“cpu”: {
“0”: {
“%usage”: 0.0
}
},

`

Using the debug module for troubleshooting, the following

`

  • name: print data
    debug:
    msg: "{{ result.ansible_facts.napalm_environment.cpu }} "

`

outputs the following:

"msg": { "0": { "%usage": 0.0 } }

The question is how do I access the value for the key “%usage”.

`

  • name: print data
    debug:
    msg: "{{ result.ansible_facts.napalm_environment.cpu.0}} "

`

`

  • name: print data
    debug:
    msg: "{{ result.ansible_facts.napalm_environment.cpu.‘0’. }} "

`

The error was: dict object has no element 0

Many thanks

You can access the first element using result.ansible_facts.napalm_environment.cpu[0].%usage or so.

Thanks I had tried that but it does not work. See below.

PLAY [Device Pre testing] ********************************************************************************************************************************************************************************************************************

TASK [get facts from device] *****************************************************************************************************************************************************************************************************************

ok: [172.16.1.127]

TASK [print data] ****************************************************************************************************************************************************************************************************************************

fatal: [172.16.1.127]: FAILED! => {“failed”: true, “msg”: "template error while templating string: expected name or number. String: {{ result.ansible_facts.napalm_environment.cpu[0].%usage }} "}

to retry, use: --limit @/Users/marcosgeorgopoulos/Documents/Ansible/napalm/test.retry

PLAY RECAP ***********************************************************************************************************************************************************************************************************************************

172.16.1.127 : ok=1 changed=0 unreachable=0 failed=1

This should work

result.ansible_facts.napalm_environment.cpu['0']['%usage']

Thank you that did it.