如何通过两个标签来处理ec2实例



尊敬的朋友和大师们:

我正在编写一个python脚本,根据两个标记值关闭/打开实例。我为每个ec2实例分配了两个标签。一个是";schedule_name;定义应该关闭/打开哪个时间表的标签;skip_shutdown";标签,该标签应将该实例从关机时间表中删除并将其值重置为"0";否";。(由于非正常原因,这是一个临时开关,无法关闭电源(

我被如何使用堆叠的";标签[密钥]

这是我的代码

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

for-循环中,您应该只获取值和assing到变量。

for-循环之后,您应该使用这两个变量来打开/关闭

for instance in instances:
# - before loop `for tag`-       
Schedule_Name = None
skip_shutdown = None
# - loop `for tag`-       

for tag in instance.tags:
if tag['Key'] == 'Schedule_Name':
Schedule_Name = tag['Value']
elif tag['Key'] == 'skip_shutdown':
skip_shutdown = tag['Value']

# - after loop `for tag`-       

if Schedule_Name == 'App' and current_time == app_off.get(current_dayoftheweek):
if skip_shutdown == 'yes':
# reset the tag value to "no" for this tag
elif tag['Value'] == 'no':
# add this instance to stopInstances variable to stop it.
stopInstances.append(instance.id)

最新更新