我正在尝试以一个表格形式打印实例名称,卷ID,实例ID,实例专用IP,快照启动时快照ID。
代码: -
#!/usr/bin/python
import os
import boto.ec2
import time
import datetime
conn = boto.ec2.connect_to_region("eu-west-1")
reservations = conn.get_all_instances()
tags = conn.get_all_tags()
snapshots = conn.get_all_snapshots(owner="self")
instances = [i for r in reservations for i in r.instances]
import datetime
today = datetime.datetime.today()
one_day = datetime.timedelta(days=1)
yesterday = today - one_day
from datetime import datetime
for inst in instances:
for snap in snapshots:
start_time = datetime.strptime (snap.start_time.split('.')[0],'%Y-%m-%dT%H:%M:%S')
if start_time > yesterday and 'Name' in inst.tags:
print "%s (%s) (%s) (%s) [%s] " % (inst.tags['Name'], start_time, snap.volume_id
它再次遍历所有实例&再次。 E.g->我有17个实例然后它循环遍历所有实例17 * 17次并显示结果。
任何建议..
答案 0 :(得分:0)
您的快照似乎不是来自您的实例,因此您可以使用类似
的内容for inst, snap in zip(instances, snapshots):
do stuff
这样它将同时遍历两个,而不是每个实例的所有快照