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

43 views
Skip to first unread message

Marcos Georgopoulos

unread,
Mar 5, 2018, 2:47:28 AM3/5/18
to Ansible Project

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

Varun Chopra

unread,
Mar 5, 2018, 3:12:13 AM3/5/18
to Ansible Project
You can access the first element using result.ansible_facts.napalm_environment.cpu[0].%usage or so.

On Monday, March 5, 2018 at 1:17:28 PM UTC+5:30, Marcos Georgopoulos wrote:

Marcos Georgopoulos

unread,
Mar 5, 2018, 3:31:38 AM3/5/18
to Ansible Project


On Monday, March 5, 2018 at 7:12:13 PM UTC+11, Varun Chopra wrote:
You can access the first element using result.ansible_facts.napalm_environment.cpu[0].%usage or so.

On Monday, March 5, 2018 at 1:17:28 PM UTC+5:30, Marcos Georgopoulos wrote:


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   

 

Kai Stian Olstad

unread,
Mar 5, 2018, 5:08:31 AM3/5/18
to ansible...@googlegroups.com
On 05.03.2018 09:31, Marcos Georgopoulos wrote:
> On Monday, March 5, 2018 at 7:12:13 PM UTC+11, Varun Chopra wrote:
>>
>> You can access the first element using
>> result.ansible_facts.napalm_environment.cpu[0].%usage or so.
>>
>> On Monday, March 5, 2018 at 1:17:28 PM UTC+5:30, Marcos Georgopoulos
>> wrote:
>>>
>>>
> Thanks I had tried that but it does not work. See below.

This should work

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

--
Kai Stian Olstad

Marcos Georgopoulos

unread,
Mar 5, 2018, 11:11:24 PM3/5/18
to Ansible Project
Thank you that did it.
Reply all
Reply to author
Forward
0 new messages