I am working on automating the processes of connecting to the switch and upgrading the image. Code I have so far is:
def check_redundancy(task):
get_redundancy = task.run(task = netmiko_send_command, command_string = 'show redundancy summary', use_ttp = True, ttp_template = ' ')
task.host['redundancy'] = get_redundancy.result
results = 'SHOW REDUNDANCY INFO'
print(task.host['redundancy'])
return Result(host = task.host, result = results)
def verify_redundancy(task):
peer_state = 'ACTIVE'
for i in task.host['redundancy']:
if i['peer_state'] == peer_state:
results = str(f"Redundancy check, looking for {i['peer_state']} -- found")
check = 'Y'
task.host['check'] = 'Y'
else:
results = str(f"Redundancy check, looking for {i['peer_state']} -- did not find it")
check = 'N'
task.host['check'] = 'N'
return Result(host=task.host, result=f"{(Style.BRIGHT)} {(Fore.WHITE)}{(Back.GREEN)} {results}",check=check)
return Result(host=task.host, result=f"{(Style.BRIGHT)} {(Fore.WHITE)}{(Back.YELLOW)} {results}",check=check)
task.host['redundancy'] output looks like this:
[ [ { 'redundancy': { 'link_encryption': 'ENABLED',
'local_state': 'STANDBY HOT',
'mobility_mac': 'xxx',
'peer_state': 'ACTIVE',
'redundancy_mode': 'SSO ENABLED',
'redundancy_port': 'UP',
'redundancy_state': 'SSO',
'unit': 'Primary',
'unit_id': 'xxx',
'verage_management_gateway_reachability_latency': '933 '
'Micro '
'Seconds',
'verage_redundancy_peer_reachability_latency': '293 '
'Micro '
'Seconds'}}]]
After I run this, I get error:
if i['peer_state'] == peer_state:
TypeError: list indices must be integers or slices, not str
Any ideas how to fix this so I can look at the output and see compare the peer_state? Thank you in advance
CodePudding user response:
Your JSON object contains 2 lists within each other.
If you do:
for i in task.host['redundancy']
you only tap into the first. Depending on how your object scales you should probably add another loop or remove unnecessary lists.