Ansible:EC2供应和迭代(Ansible: EC2 provisioning and Iterations)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
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,以便我可以继续。

任何人都知道一个干净的方法,或者会友好地向我解释如何在正确的循环后处理已注册的变量?

I am trying to start a bunch of EC2 instances, then install something on them based on the IP given by AWS. With only one EC2, I can add the host and proceed without any issue,but when I chain them using with_dict, I can't achieve it anymore...

The following runs as I want, but I can't understand how to deal with the registered variable ec2_infos I got from the provisioning...

- 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

With a dictionary like

ec2_stack: serv1: type: t2.micro name: server1 serv2: type: t2.small name: server2

ec2_infos is structures like:

"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": [] } ] }

I tried with_items and with_subelements in different ways, but I can't manage to get every IPs of the new EC2. I don't even need to sort them just extract them from the instances part and feed them to add_host so I can proceed.

Anybody knows a clean way to do so, or would be kind enough to explain to me how to deal with a registered variable after a loop properly ?

最满意答案

回答评论:

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

Answer from the comments:

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

更多推荐

本文发布于:2023-04-12 20:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/ede12ed1583761a3aae0d71d56a614bd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:迭代   Ansible   provisioning   Iterations

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!