我正在自动化连接到交换机和升级图像的过程。到目前为止,我的代码是:
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)
任务。Host ['redundancy']输出如下:
[ [ { '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'}}]]
在我运行这个之后,我得到错误:
if i['peer_state'] == peer_state:
TypeError: list indices must be integers or slices, not str
有什么办法可以解决这个问题,以便我可以查看输出并比较peer_state?提前谢谢你
您的JSON对象中包含两个列表。
如果你这样做:
for i in task.host['redundancy']
你只能进入第一个。根据你的对象的大小,你可能应该添加另一个循环或删除不必要的列表。