无法将 boto3 过滤的数据发送到列表

Trouble sending boto3 filtered data to a list

我使用 anaconda python 2.7 生成了一个有效的 boto3 实例过滤器。我需要将 IP 地址和实例名称分配给列表以供进一步处理。

代码如下所示:

for instance in instances:
    print(instance.id, instance.instance_type, instance.key_name, instance.private_ip_address, instance.public_dns_name ,instance.tags[0].get("Value"))

tagged   = ec2.instances.filter(Filters=[{'Name': 'tag:My', 'Values': ['tag']}])

for instance in tagged:
    print(name, instance.id, instance.private_ip_address)

object_list={}
object_list['requests']=[]

for instance in tagged:
        tagged.collections.OrderedDict()
        tagged['ip-address'] = instance.private_ip_address
        tagged['machine'] = name
        tagged['roles'] = ['tagged',instance.id,name]
        object_list['requests'].append(tagged)

这是错误:

AttributeError: 'ec2.instancesCollection' object has no attribute 'collections'

想法?

这不存在,但你有一个神秘的错误,因为你正在重用你的 tagged 变量,它是 ec2.instancesCollection

的一个实例
 tagged.collections.OrderedDict()

(因为它不受影响所以什么都不做)

你(大概)want/wanted:

tagged = collections.OrderedDict()

但这没有意义,因为您已经在迭代 tagged

你必须声明另一个变量

for instance in tagged:
    t = collections.OrderedDict()
    t['ip-address'] = instance.private_ip_address
    t['machine'] = name
    t['roles'] = ['tagged',instance.id,name]
    object_list['requests'].append(t)