Hello
Today I came across the following problem: I needed to export some Cisco device data from Prime to a CSV file using Ansible.
I had some success initially: I used the show inventory parser with a 2960x and was able to export the data. However, I had some 9xxx switches in my mix, too.
When I tried using the ”show inventory” Genie parser on a Cisco 9300, I got the ”rp_dict variable not initialised” error. So the parser works great on a 2960x but on any C9xxx switch it fails miserably. Where’s the problem?
Let’s have a look at the source code of the show inventory Genie parser.
if 'STACK' in pid:
main_dict = ret_dict.setdefault('main', {})
main_dict['swstack'] = True
if ('ASR-9') in pid and ('PWR' not in pid) and ('FAN' not in pid):
rp_dict = ret_dict.setdefault('slot', {}).\
setdefault('0', {}).\
setdefault('rp', {}).\
setdefault(pid, {})
rp_dict['name'] = name
rp_dict['descr'] = descr
rp_dict['pid'] = pid
rp_dict['vid'] = vid
rp_dict['sn'] = sn
asr900_rp = True
# Ensure name, slot have been previously parsed
if not name or not slot:
continue
# PID: ASR1000-RP2 , VID: V02 , SN: JAE153408NJ
# PID: ASR1000-RP2 , VID: V03 , SN: JAE1703094H
# PID: WS-C3850-24P-E , VID: V01 , SN: FCW1932D0LB
if ('RP' in pid) or ('WS-C' in pid) or ('R' in name):
rp_dict = slot_dict.setdefault('rp', {}).\
setdefault(pid, {})
rp_dict['name'] = name
rp_dict['descr'] = descr
rp_dict['pid'] = pid
rp_dict['vid'] = vid
rp_dict['sn'] = sn
# PID: ASR1000-SIP40 , VID: V02 , SN: JAE200609WP
# PID: ISR4331/K9 , VID: , SN: FDO21520TGH
# PID: ASR1002-X , VID: V07, SN: FOX1111P1M1
# PID: ASR1002-HX , VID: , SN:
elif (('SIP' in pid) or ('-X' in pid) or \
('-HX' in pid) or ('-LC' in pid) or ('module' in name and not ('module F' in name))) and \
('subslot' not in name):
lc_dict = slot_dict.setdefault('lc', {}).\
setdefault(pid, {})
lc_dict['name'] = name
lc_dict['descr'] = descr
lc_dict['pid'] = pid
lc_dict['vid'] = vid
lc_dict['sn'] = sn
# PID: SP7041-E , VID: E , SN: MTC164204VE
# PID: SFP-GE-T , VID: V02 , SN: MTC2139029X
# PID: EM7455/EM7430 , VID: 1.0 , SN: 355813070074072
elif subslot:
if ('STACK' in pid) or asr900_rp:
subslot_dict = rp_dict.setdefault('subslot', {}).\
setdefault(subslot, {}).\
setdefault(pid, {})
The problem is that the rp_dict variable won’t get initialised unless the module name meets some conditions ( e.g. : if (‚RP’ in pid) or (‚WS-C’ in pid) or (‚R’ in name): ) but the problem is that for the C9xxx switches, PIDs no longer begin with „WS-C”.
The solution is to rewrite the script or modify the values before assigning the data output to the Genie parser in Ansible.
- name: show inventory
block:
- name: Run show inventory
ios_command:
commands: show inventory
register: show_inventory
- name: Print my data
set_fact:
int_data_modify: "{{ show_inventory['stdout'][0] | regex_replace('C9','WS-C9') }}"
- name: Print new data
set_fact:
inventory_data_final: "{{ int_data_modify | clay584.genie.parse_genie(command='show inventory', os='ios') }}"