ansible - getting operational structured data from cumulus switch


Hi all,

I noticed that Cumulus Switch has a built-in JSON formatting in the NCLU 'net show' command but how to use it using ansible to get an operational structured data? And also I tried using the 'nclu' Network Module for example below;

- nclu:
commands: show interface eth0 json

But it did not return anything, maybe I just miss something there. but then when I use an ansible 'command' module for example below.

- command: net show interface eth0 json

It did return something but not a structured one.

Thanks in advance.

3 replies

Userlevel 1
Hi Reynold,

It looks like I was able to get structured output using the command module. With the following playbook contents:
---
- hosts: spine02
become: true
tasks:
- command: net show interface eth0 json
register: output
- debug: msg='{{ output.stdout }}'
I get the following output when running the playbook:
nmitchell@prophecy ~/git/topology_converter $ ap test.yml
PLAY [spine02] **************************************************************************************************************************
TASK [Gathering Facts] ******************************************************************************************************************
Monday 26 February 2018 15:56:23 -0500 (0:00:00.026) 0:00:00.026 *******
ok: [spine02]
TASK [command] **************************************************************************************************************************
Monday 26 February 2018 15:56:24 -0500 (0:00:00.759) 0:00:00.785 *******
changed: [spine02]
TASK [debug] ****************************************************************************************************************************
Monday 26 February 2018 15:56:24 -0500 (0:00:00.307) 0:00:01.092 *******
ok: [spine02] => {
"msg": {
"connector_type": "Unknown",
"iface_obj": {
"asic": null,
"counters": null,
"description": "",
"dhcp_enabled": true,
"ip_address": {
"allentries": [
"10.255.1.126/24"
]
},
"ip_neighbors": {
"ipv4": [
"52:54:00:2b:a2:5c"
],
"ipv6": []
},
"lacp": {
"bypass": "",
"partner_mac": "",
"rate": "",
"sys_priority": ""
},
"lldp": null,
"mac": "52:54:00🇧🇩2a:ae",
"members": {},
"min_links": "",
"mtu": 1500,
"native_vlan": null,
"vlan": null,
"vlan_filtering": false
},
"linkstate": "UP",
"mode": "Mgmt",
"speed": "1G",
"summary": "IP: 10.255.1.126/24(DHCP)"
}
}
PLAY RECAP ******************************************************************************************************************************
spine02 : ok=3 changed=1 unreachable=0 failed=0
Monday 26 February 2018 15:56:24 -0500 (0:00:00.035) 0:00:01.128 *******
===============================================================================
Gathering Facts --------------------------------------------------------- 0.76s
command ----------------------------------------------------------------- 0.31s
debug ------------------------------------------------------------------- 0.04s
Is this the type of structured output you were looking for?
Userlevel 4
Nick Mitchell wrote:

Hi Reynold,

It looks like I was able to get structured output using the command module. With the...

I also go one step further to convert the JSON data coming back into a native Ansible variable that can be parsed and used to inform other tasks, the example below applies to the output from the decode_syseeprom command however it could be used for NCLU JSON outputs as well.

- name: Collect System Parameters in JSON
shell: /usr/cumulus/bin/decode-syseeprom -j
register: decode_syseeprom_json
changed_when: False
- name: Convert JSON into Ansible native variable type
set_fact:
decode_syseeprom: "{{decode_syseeprom_json.stdout | from_json }}"
changed_when: False

Nick Mitchell wrote:

Hi Reynold,

It looks like I was able to get structured output using the command module. With the...

Hi Nick,

Thanks for the reply and that is the structured output I was expecting during my testing. It was my bad, I did not receive same output as yours because of my stdout_callback which is set to actionable. Thanks again.

Reply