【发布时间】:2021-05-14 23:48:10
【问题描述】:
亲爱的朋友和大师在这里:
我正在编写一个 python 脚本来根据两个标签值关闭/打开实例。我为每个 ec2 实例分配了两个标签。一个是“schedule_name”标签,它定义了它应该关闭/打开的计划,另一个是“skip_shutdown”标签,它应该从关闭计划中删除该实例并将其值重置为“no”。 (这作为非正常原因的临时开关不关闭它)
我被如何使用堆叠的“标签['key']”困在这里
这是我的代码
for instance in instances:
for tag in instance.tags:
if tag['Key'] == 'Schedule_Name':
if tag['Value'] == 'App' and current_time == app_off.get(current_dayoftheweek):
if tag['Key'] == 'skip_shutdown':
if tag['Value'] == 'yes':
# reset the tag value to "no" for this tag
if tag['Value'] == 'no':
# add this instance to stopInstances variable to stop it.
stopInstances.append(instance.id)
pass
pass
【问题讨论】:
-
您应该使用
if ... elif ...而不是嵌套的if ... if..并且您应该为外部变量赋值 - 在for-loop 之后检查两个值是否正确。