Ansible:EC2 配置和迭代

Ansible: EC2 provisioning and Iterations

我正在尝试启动一堆 EC2 实例,然后根据 AWS 提供的 IP 在它们上安装一些东西。只有一个 EC2,我可以毫无问题地添加主机并继续,但是当我使用 with_dict 链接它们时,我无法再实现它...

下面的运行如我所愿,但我无法理解如何处理注册的变量ec2_infos我从配置中得到...

- name: Create Test EC2 instances
      ec2:
        group: default
        image: ami-40d28157
        instance_type: '{{item.value.type}}'
        instance_tags:
          Name: "{{ tag+'-'+item.value.name }}"
        key_name: privatekey
        region: us-west-1
        vpc_subnet_id: subnet-REDACTD
        wait: yes
      with_dict: '{{ec2_stack}}'
      register: ec2_infos

这样的字典
ec2_stack:
    serv1:
        type: t2.micro
        name: server1
    serv2:
        type: t2.small
        name: server2

ec2_infos 是这样的结构:

"ec2_infos": {
    "changed": true,
    "msg": "All items completed",
    "results": [
        {
            "_ansible_item_result": true,
            "_ansible_no_log": false,
            "_ansible_parsed": true,
            "changed": true,
            "instance_ids": [
                "i-0fewq09812ddq6"
            ],
            "instances": [
                {
                    "ami_launch_index": "0",
                    "architecture": "x86_64",
                    "block_device_mapping": {
                        "/dev/sda1": {
                            "delete_on_termination": true,
                            "status": "attached",
                            "volume_id": "vol-0987654"
                        }
                    },
                    "dns_name": "",
                    "ebs_optimized": false,
                    "groups": {
                        "sg-qdwdww": "default"
                    },
                    "hypervisor": "xen",
                    "id": "i-083665656521dwq6",
                    "image_id": "ami-40d28157", 
                    "launch_time": "2016-11-24T20:38:53.000Z",
                    "placement": "us-west-1d",
                    "private_ip": "x.x.x.x",
                    "public_dns_name": "",
                    "public_ip": null,
                    "ramdisk": null,
                    "region": "us-east-1",
                    "root_device_name": "/dev/sda1",
                    "root_device_type": "ebs",
                    "state": "running",
                    "state_code": 16,
                    "tags": {
                        "Name": "server1",
                        "Team": "blah"
                    },
                    "tenancy": "default","tenancy": "default", 
                    "virtualization_type": "hvm"
                }
            ], 
            "invocation": {
                "module_args": {
                    "assign_public_ip": false,
                    "exact_count": null, 
                    "group": [
                        "default"
                    ], 
                    "group_id": null, 
                    "id": null, 
                    "image": "ami-40d28157", 
                    "instance_ids": null, 
                    "instance_initiated_shutdown_behavior": null, 
                    "instance_profile_name": null, 
                    "instance_tags": {
                        "Name": "server1", 
                        "Team": "blah"
                    }, 
                    "instance_type": "t2.micro", 
                    "kernel": null, 
                    "volumes": null, 
                    "vpc_subnet_id": "subnet-abcdfed", 
                    "wait": true, 
                    "wait_timeout": "300", 
                    "zone": null
                }, 
                "module_name": "ec2"
            }, 
            "item": {
                "key": "serv1", 
                "value": {
                    "name": "server1", 
                    "type": "t2.micro"
                }
            }, 
            "tagged_instances": []
        }, 
        {
            "_ansible_item_result": true, 
            "_ansible_no_log": false, 
            "_ansible_parsed": true, 
            "changed": true, 
            "instance_ids": [
                "i-0971278624334fd"
            ], 
            "instances": [
                {
                    "ami_launch_index": "0", 
                    "architecture": "x86_64", 
                    "block_device_mapping": {
                        "/dev/sda1": {
                            "delete_on_termination": true, 
                            "status": "attached", 
                            "volume_id": "vol-9999999"
                        }
                    }, 
                    "dns_name": "", 
                    "ebs_optimized": false, 
                    "groups": {
                        "sg-redactd": "default"
                    }, 
                    "launch_time": "2016-11-24T20:39:21.000Z", 
                    "private_ip": "y.y.y.y", 
                    "public_dns_name": "", 
                    "public_ip": null, 
                    "ramdisk": null, 
                    "state": "running", 
                    "state_code": 16, 
                    "tags": {
                        "Name": "serv2", 
                        "Team": "blah"
                    }, 
                    "tenancy": "default", 
                    "virtualization_type": "hvm"
                }
            ], 
            "invocation": {
                "module_args": {
                    "assign_public_ip": false, 

                    "wait_timeout": "300", 
                    "zone": null
                }, 
                "module_name": "ec2"
            }, 
            "item": {
                "key": "server2", 
                "value": {
                    "name": "serv2", 
                    "type": "t2.small"
                }
            }, 
            "tagged_instances": []
        }
    ]
}

我以不同的方式尝试了 with_items 和 with_subelements,但我无法获取新 EC2 的每个 IP。我什至不需要对它们进行排序,只需从实例部分提取它们并将它们提供给 add_host 这样我就可以继续了。

有谁知道这样做的简洁方法,或者愿意向我解释如何在循环后正确处理已注册的变量?

来自评论的回答:

ec2_infos.results | map(attribute='instances') | sum(start=[]) | map(attribute='private_ip') | list