Создание экземпляров Ec2 и запись имени и IP в файл - PullRequest
0 голосов
/ 25 сентября 2019

мне нужна ваша помощь, пожалуйста.

у меня 2 задачи 1 - создание 2 экземпляров ec2 2 - запись имени и ip в файл

Я получаю ошибку в задаче 2,я не знаю, как получить информацию из фактов.

- name: Creating DB VM
  ec2_instance:
    name: "{{ item }}"
    vpc_subnet_id: "{{ db_ec2_vpc_subnet }}"
    key_name: "awsitg"
    instance_type: "{{ aws_db_ec2_size }}"
    security_group: Galeria_DB_sec_group
    network:
      assign_public_ip: true
    image_id: ami-0cc293023f983ed53
    tags:
      Env: dev
      srvrules: "{{ item }}"
  loop:
    - db01
    - db02
  register: ec2_db_facts
- debug: var=ec2_db_facts 


- name: Add the newly created EC2 instance(s)
  lineinfile:
    dest: hosts_aws
    insertafter: "[ec2db]"
    line: "{{ item.tags.Name }} ansible_connection=ssh ansible_host={{ item.public_ip_address }} ansible_user=ec2-user"
  loop: "{{ ec2_db_facts.results }}"

как я могу получить информацию из цикла?item.tags.Name item.public_ip_address

Ошибкаec2_db_facts

ok: [localhost] => {
    "ec2_db_facts": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": true, 
                "failed": false, 
                "instance_ids": [
                    "i-018ca7b0f4f81bf28"
                ], 
                "instances": [
                    {
                        "ami_launch_index": 0, 
                        "architecture": "x86_64", 
                        "block_device_mappings": [
                            {
                                "device_name": "/dev/xvda", 
                                "ebs": {
                                    "attach_time": "2019-09-25T21:55:08+00:00", 
                                    "delete_on_termination": true, 
                                    "status": "attached", 
                                    "volume_id": "vol-0cd2d31b018389eb6"
                                }
                            }
                        ], 
                        "capacity_reservation_specification": {
                            "capacity_reservation_preference": "open"
                        }, 
                        "client_token": "", 
                        "cpu_options": {
                            "core_count": 1, 
                            "threads_per_core": 2
                        }, 
                        "ebs_optimized": false, 
                        "ena_support": true, 
                        "hibernation_options": {
                            "configured": false
                        }, 
                        "hypervisor": "xen", 
                        "image_id": "ami-0cc293023f983ed53", 
                        "instance_id": "i-018ca7b0f4f81bf28", 
                        "instance_type": "t3.micro", 
                        "key_name": "", 
                        "launch_time": "2019-09-25T21:55:07+00:00", 
                        "monitoring": {
                            "state": "disabled"
                        }, 
                        "network_interfaces": [
                            {
                                "association": {
                                    "ip_owner_id": "amazon", 
                                    "public_dns_name": "", 
                                    "public_ip": "54.93.55."
                                }, 
                                "attachment": {
                                    "attach_time": "2019-09-25T21:55:07+00:00", 
                                    "attachment_id": "eni-attach-0adb2b6326218d65e", 
                                    "delete_on_termination": true, 
                                    "device_index": 0, 
                                    "status": "attached"
                                }, 
                                "description": "", 
                                "groups": [
                                    {
                                        "group_id": "sg-09809ccd93f699720", 
                                        "group_name": "Galeria_DB_sec_group"
                                    }
                                ], 
                                "interface_type": "interface", 
                                "ipv6_addresses": [], 
                                "mac_address": "06:5f:83:00:31:ae", 
                                "network_interface_id": "eni-0ee471e55a118c399", 
                                "owner_id": "901777084119", 
                                "private_ip_address": "10.1.4.147", 
                                "private_ip_addresses": [
                                    {
                                        "association": {
                                            "ip_owner_id": "amazon", 
                                            "public_dns_name": "", 
                                            "public_ip": "54.93.55."
                                        }, 
                                        "primary": true, 
                                        "private_ip_address": "10.1.4.147"
                                    }
                                ], 
                                "source_dest_check": true, 
                                "status": "in-use", 
                                "subnet_id": "subnet-0108618c4b56ea424", 
                                "vpc_id": "vpc-0cff9f49608fc5865"
                            }
                        ], 
                        "placement": {
                            "availability_zone": "eu-central-1b", 
                            "group_name": "", 
                            "tenancy": "default"
                        }, 
                        "private_dns_name": "ip-10-1-4-147.eu-central-1.compute.internal", 
                        "private_ip_address": "10.1.4.147", 
                        "product_codes": [], 
                        "public_dns_name": "", 
                        "public_ip_address": "54.93.55.", 
                        "root_device_name": "/dev/xvda", 
                        "root_device_type": "ebs", 
                        "security_groups": [
                            {
                                "group_id": "sg-09809ccd93f699720", 
                                "group_name": "Galeria_DB_sec_group"
                            }
                        ], 
                        "source_dest_check": true, 
                        "state": {
                            "code": 16, 
                            "name": "running"
                        }, 
                        "state_transition_reason": "", 
                        "subnet_id": "subnet-0108618c4b56ea424", 
                        "tags": {
                            "Env": "dev", 
                            "Name": "db0111111", 
                            "srvrules": "db0111111"
                        }, 
                        "virtualization_type": "hvm", 
                        "vpc_id": "vpc-0cff9f49608fc5865"
                    }
                ], 
                "invocation": {
                    "module_args": {
                        "availability_zone": null, 
                        "aws_access_key": null, 
                        "aws_secret_key": null, 
                        "cpu_credit_specification": null, 
                        "cpu_options": null, 
                        "debug_botocore_endpoint_logs": false, 
                        "detailed_monitoring": null, 
                        "ebs_optimized": null, 
                        "ec2_url": null, 
                        "filters": {
                            "image-id": [
                                "ami-0cc293023f983ed53"
                            ], 
                            "instance-state-name": [
                                "pending", 
                                "running", 
                                "stopping", 
                                "stopped"
                            ], 
                            "subnet-id": [
                                "subnet-0108618c4b56ea424"
                            ], 
                            "tag:Name": [
                                "db0111111"
                            ]
                        }, 
                        "image": null, 
                        "image_id": "ami-0cc293023f983ed53", 
                        "instance_ids": [], 
                        "instance_initiated_shutdown_behavior": null, 
                        "instance_role": null, 
                        "instance_type": "t3.micro", 
                        "key_name": "", 
                        "launch_template": null, 
                        "name": "db0111111", 
                        "network": {
                            "assign_public_ip": true
                        }, 
                        "placement_group": null, 
                        "profile": "", 
                        "purge_tags": false, 
                        "region": null, 
                        "security_group": "Galeria_DB_sec_group", 
                        "security_groups": [], 
                        "security_token": null, 
                        "state": "present", 
                        "tags": {
                            "Env": "dev", 
                            "Name": "db0111111", 
                            "srvrules": "db0111111"
                        }, 
                        "tenancy": null, 
                        "termination_protection": null, 
                        "tower_callback": null, 
                        "user_data": null, 
                        "validate_certs": true, 
                        "volumes": null, 
                        "vpc_subnet_id": "subnet-0108618c4b56ea424", 
                        "wait": true, 
                        "wait_timeout": 600
                    }
                }, 
                "item": "db0111111", 
                "spec": {
                    "BlockDeviceMappings": [], 
                    "ClientToken": "", 
                    "ImageId": "ami-0cc293023f983ed53", 
                    "InstanceType": "t3.micro", 
                    "KeyName": "awsitg", 
                    "MaxCount": 1, 
                    "MinCount": 1, 
                    "NetworkInterfaces": [
                        {
                            "AssociatePublicIpAddress": true, 
                            "DeviceIndex": 0, 
                            "Groups": [
                                "sg-09809ccd93f699720"
                            ], 
                            "SubnetId": "subnet-0108618c4b56ea424"
                        }
                    ], 
                    "TagSpecifications": [
                        {
                            "ResourceType": "volume", 
                            "Tags": [
                                {
                                    "Key": "srvrules", 
                                    "Value": "db0111111"
                                }, 
                                {
                                    "Key": "Name", 
                                    "Value": "db0111111"
                                }, 
                                {
                                    "Key": "Env", 
                                    "Value": "dev"
                                }
                            ]
                        }, 
                        {
                            "ResourceType": "instance", 
                            "Tags": [
                                {
                                    "Key": "srvrules", 
                                    "Value": "db0111111"
                                }, 
                                {
                                    "Key": "Name", 
                                    "Value": "db0111111"
                                }, 
                                {
                                    "Key": "Env", 
                                    "Value": "dev"
                                }
                            ]
                        }
                    ]
                }
            }
        ]
    }
}
...