Creating instance and tagging via ansible

2019-07-25 04:30发布

问题:

I am using ansible to create instance in different AZ , i am facing two issues here:

  1. I need to create 3 instances in AZ , but the below play creates 6 instances such that 3 instances in AZ-a and 3 in AZ-b
  2. I am trying to tag these instances as well, using a sequence as they are being created, but i am not able to pass/traverse the json returned.

Here are the plays:

---
  - name: Create ES Master Node instances
    ec2:
      key_name: "{{ aws_key_name }}"
      instance_type: "{{ aws_instance_type }}"
      image: "{{ aws_ami }}"
      wait: yes
      wait_timeout: 500
      count: "{{ master_instance_count }}"
      instance_tags:
        Name: "{{ master_tag_name }}"
      volumes:
        - device_name: /dev/sda1
          volume_type: gp2
          volume_size: 100
      vpc_subnet_id: "{{ subnet_ids[item] }}"
      region: "{{ aws_region }}"
      group: "{{ aws_sec_group_name }}"
    with_items:
      - a
      - b
    register: ec2_details

  - name: Generate Sequence ID for tagging
    debug: msg="{{ item }}"
    with_sequence: start="{{ startindex }}" end="{{ master_instance_count }}" format=%02d
    register: sequence

  - name: Debug the variable name
    debug:
          var: ec2_details
          #msg={{ ec2_details | type_debug }}

  - name: Tag Created instance as Masters
    ec2_tag:
      region: "{{ aws_region }}"
      resource: "{{ item.0.id }}"
      tags:
        Name: "{{ master_tag_name }}-{{ item.1.msg }}"
    with_together:
      - "{{ ec2_details.results.instance_ids }}"
      - "{{ sequence.results }}"

  - name: Add Instances to local inventory file
    local_action:
      lineinfile path="~/ansible-environments/hosts"
        insertafter="\[esmaster\]"
        line="{{ item.private_ip }}"
        state="present"
    with_items: "{{ ec2_details.results.instances }}"

I am not sure how is issue 1 happening and how to solve, and for the 2nd i need help in traversing the result so that i can tag and create inventory in a file.

OUTPUT of DEBUG

TASK [provision_es_cluster : Debug the variable name] *****************************************************************************************
task path: /home/nishantsingh/daas-ansible-playbooks/roles/provision_es_cluster/tasks/master_node.yml:29
ok: [localhost] => {
    "ec2_details": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "_ansible_parsed": true, 
                "changed": true, 
                "failed": false, 
                "instance_ids": [
                    "i-05aee7d659a14f6c8", 
                    "i-0aa09975bdb96b9aa", 
                    "i-094b151b09ae19b27"
                ], 
                "instances": [
                    {
                        "ami_launch_index": "2", 
                        "architecture": "x86_64", 
                        "block_device_mapping": {
                            "/dev/sda1": {
                                "delete_on_termination": false, 
                                "status": "attached", 
                                "volume_id": "vol-08b2167e65a8047ea"
                            }
                        }, 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "groups": {
                            "sg-1e3c9d79": "elasticsearch-mayassar-production", 
                            "sg-4659bc3f": "Test_nishant", 
                            "sg-86018ce2": "mayassar-es-external"
                        }, 
                        "hypervisor": "xen", 
                        "id": "i-05aee7d659a14f6c8", 
                        "image_id": "ami-325d2e4e", 
                        "instance_type": "m4.4xlarge", 
                        "kernel": null, 
                        "key_name": "ansible-managed", 
                        "launch_time": "2018-01-30T05:07:42.000Z", 
                        "placement": "ap-southeast-1a", 
                        "private_dns_name": "ip-172-16-14-80.ap-southeast-1.compute.internal", 
                        "private_ip": "172.16.14.80", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "ap-southeast-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "tags": {
                            "Name": "Kill-master-es"
                        }, 
                        "tenancy": "default", 
                        "virtualization_type": "hvm"
                    }, 
                    {
                        "ami_launch_index": "1", 
                        "architecture": "x86_64", 
                        "block_device_mapping": {
                            "/dev/sda1": {
                                "delete_on_termination": false, 
                                "status": "attached", 
                                "volume_id": "vol-036062675c6119993"
                            }
                        }, 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "groups": {
                            "sg-1e3c9d79": "elasticsearch-mayassar-production", 
                            "sg-4659bc3f": "Test_nishant", 
                            "sg-86018ce2": "mayassar-es-external"
                        }, 
                        "hypervisor": "xen", 
                        "id": "i-0aa09975bdb96b9aa", 
                        "image_id": "ami-325d2e4e", 
                        "instance_type": "m4.4xlarge", 
                        "kernel": null, 
                        "key_name": "ansible-managed", 
                        "launch_time": "2018-01-30T05:07:42.000Z", 
                        "placement": "ap-southeast-1a", 
                        "private_dns_name": "ip-172-16-14-176.ap-southeast-1.compute.internal", 
                        "private_ip": "172.16.14.176", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "ap-southeast-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "tags": {
                            "Name": "Kill-master-es"
                        }, 
                        "tenancy": "default", 
                        "virtualization_type": "hvm"
                    }, 
                    {
                        "ami_launch_index": "0", 
                        "architecture": "x86_64", 
                        "block_device_mapping": {
                            "/dev/sda1": {
                                "delete_on_termination": false, 
                                "status": "attached", 
                                "volume_id": "vol-0566ea20a2ef4d40b"
                            }
                        }, 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "groups": {
                            "sg-1e3c9d79": "elasticsearch-mayassar-production", 
                            "sg-4659bc3f": "Test_nishant", 
                            "sg-86018ce2": "mayassar-es-external"
                        }, 
                        "hypervisor": "xen", 
                        "id": "i-094b151b09ae19b27", 
                        "image_id": "ami-325d2e4e", 
                        "instance_type": "m4.4xlarge", 
                        "kernel": null, 
                        "key_name": "ansible-managed", 
                        "launch_time": "2018-01-30T05:07:42.000Z", 
                        "placement": "ap-southeast-1a", 
                        "private_dns_name": "ip-172-16-14-171.ap-southeast-1.compute.internal", 
                        "private_ip": "172.16.14.171", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "ap-southeast-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "tags": {
                            "Name": "Kill-master-es"
                        }, 
                        "tenancy": "default", 
                        "virtualization_type": "hvm"
                    }
                ], 
                "invocation": {
                    "module_args": {
                        "assign_public_ip": false, 
                        "aws_access_key": null, 
                        "aws_secret_key": null, 
                        "count": 3, 
                        "count_tag": null, 
                        "ebs_optimized": false, 
                        "ec2_url": null, 
                        "exact_count": null, 
                        "group": [
                            "mayassar-es-external", 
                            "elasticsearch-mayassar-production", 
                            "Test_nishant"
                        ], 
                        "group_id": null, 
                        "id": null, 
                        "image": "ami-325d2e4e", 
                        "instance_ids": null, 
                        "instance_initiated_shutdown_behavior": null, 
                        "instance_profile_name": null, 
                        "instance_tags": {
                            "Name": "Kill-master-es"
                        }, 
                        "instance_type": "m4.4xlarge", 
                        "kernel": null, 
                        "key_name": "ansible-managed", 
                        "monitoring": false, 
                        "network_interfaces": null, 
                        "placement_group": null, 
                        "private_ip": null, 
                        "profile": null, 
                        "ramdisk": null, 
                        "region": "ap-southeast-1", 
                        "security_token": null, 
                        "source_dest_check": true, 
                        "spot_launch_group": null, 
                        "spot_price": null, 
                        "spot_type": "one-time", 
                        "spot_wait_timeout": "600", 
                        "state": "present", 
                        "tenancy": "default", 
                        "termination_protection": null, 
                        "user_data": null, 
                        "validate_certs": true, 
                        "volumes": [
                            {
                                "device_name": "/dev/sda1", 
                                "volume_size": 100, 
                                "volume_type": "gp2"
                            }
                        ], 
                        "vpc_subnet_id": "subnet-9c3e38f8", 
                        "wait": true, 
                        "wait_timeout": "500", 
                        "zone": null
                    }
                }, 
                "item": "a", 
                "tagged_instances": []
            }, 
            {
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "_ansible_parsed": true, 
                "changed": true, 
                "failed": false, 
                "instance_ids": [
                    "i-08d33c0bda075f2a7", 
                    "i-0e05e337933456b48", 
                    "i-0aeca61abca10406e"
                ], 
                "instances": [
                    {
                        "ami_launch_index": "0", 
                        "architecture": "x86_64", 
                        "block_device_mapping": {
                            "/dev/sda1": {
                                "delete_on_termination": false, 
                                "status": "attached", 
                                "volume_id": "vol-06b5f8518cd368c12"
                            }
                        }, 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "groups": {
                            "sg-1e3c9d79": "elasticsearch-mayassar-production", 
                            "sg-4659bc3f": "Test_nishant", 
                            "sg-86018ce2": "mayassar-es-external"
                        }, 
                        "hypervisor": "xen", 
                        "id": "i-08d33c0bda075f2a7", 
                        "image_id": "ami-325d2e4e", 
                        "instance_type": "m4.4xlarge", 
                        "kernel": null, 
                        "key_name": "ansible-managed", 
                        "launch_time": "2018-01-30T05:07:58.000Z", 
                        "placement": "ap-southeast-1b", 
                        "private_dns_name": "ip-172-16-9-206.ap-southeast-1.compute.internal", 
                        "private_ip": "172.16.9.206", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "ap-southeast-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "tags": {
                            "Name": "Kill-master-es"
                        }, 
                        "tenancy": "default", 
                        "virtualization_type": "hvm"
                    }, 
                    {
                        "ami_launch_index": "1", 
                        "architecture": "x86_64", 
                        "block_device_mapping": {
                            "/dev/sda1": {
                                "delete_on_termination": false, 
                                "status": "attached", 
                                "volume_id": "vol-0b9cf0da059d23238"
                            }
                        }, 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "groups": {
                            "sg-1e3c9d79": "elasticsearch-mayassar-production", 
                            "sg-4659bc3f": "Test_nishant", 
                            "sg-86018ce2": "mayassar-es-external"
                        }, 
                        "hypervisor": "xen", 
                        "id": "i-0e05e337933456b48", 
                        "image_id": "ami-325d2e4e", 
                        "instance_type": "m4.4xlarge", 
                        "kernel": null, 
                        "key_name": "ansible-managed", 
                        "launch_time": "2018-01-30T05:07:58.000Z", 
                        "placement": "ap-southeast-1b", 
                        "private_dns_name": "ip-172-16-9-141.ap-southeast-1.compute.internal", 
                        "private_ip": "172.16.9.141", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "ap-southeast-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "tags": {
                            "Name": "Kill-master-es"
                        }, 
                        "tenancy": "default", 
                        "virtualization_type": "hvm"
                    }, 
                    {
                        "ami_launch_index": "2", 
                        "architecture": "x86_64", 
                        "block_device_mapping": {
                            "/dev/sda1": {
                                "delete_on_termination": false, 
                                "status": "attached", 
                                "volume_id": "vol-06057ca9f82fb8c53"
                            }
                        }, 
                        "dns_name": "", 
                        "ebs_optimized": false, 
                        "groups": {
                            "sg-1e3c9d79": "elasticsearch-mayassar-production", 
                            "sg-4659bc3f": "Test_nishant", 
                            "sg-86018ce2": "mayassar-es-external"
                        }, 
                        "hypervisor": "xen", 
                        "id": "i-0aeca61abca10406e", 
                        "image_id": "ami-325d2e4e", 
                        "instance_type": "m4.4xlarge", 
                        "kernel": null, 
                        "key_name": "ansible-managed", 
                        "launch_time": "2018-01-30T05:07:58.000Z", 
                        "placement": "ap-southeast-1b", 
                        "private_dns_name": "ip-172-16-9-201.ap-southeast-1.compute.internal", 
                        "private_ip": "172.16.9.201", 
                        "public_dns_name": "", 
                        "public_ip": null, 
                        "ramdisk": null, 
                        "region": "ap-southeast-1", 
                        "root_device_name": "/dev/sda1", 
                        "root_device_type": "ebs", 
                        "state": "running", 
                        "state_code": 16, 
                        "tags": {
                            "Name": "Kill-master-es"
                        }, 
                        "tenancy": "default", 
                        "virtualization_type": "hvm"
                    }
                ], 
                "invocation": {
                    "module_args": {
                        "assign_public_ip": false, 
                        "aws_access_key": null, 
                        "aws_secret_key": null, 
                        "count": 3, 
                        "count_tag": null, 
                        "ebs_optimized": false, 
                        "ec2_url": null, 
                        "exact_count": null, 
                        "group": [
                            "mayassar-es-external", 
                            "elasticsearch-mayassar-production", 
                            "Test_nishant"
                        ], 
                        "group_id": null, 
                        "id": null, 
                        "image": "ami-325d2e4e", 
                        "instance_ids": null, 
                        "instance_initiated_shutdown_behavior": null, 
                        "instance_profile_name": null, 
                        "instance_tags": {
                            "Name": "Kill-master-es"
                        }, 
                        "instance_type": "m4.4xlarge", 
                        "kernel": null, 
                        "key_name": "ansible-managed", 
                        "monitoring": false, 
                        "network_interfaces": null, 
                        "placement_group": null, 
                        "private_ip": null, 
                        "profile": null, 
                        "ramdisk": null, 
                        "region": "ap-southeast-1", 
                        "security_token": null, 
                        "source_dest_check": true, 
                        "spot_launch_group": null, 
                        "spot_price": null, 
                        "spot_type": "one-time", 
                        "spot_wait_timeout": "600", 
                        "state": "present", 
                        "tenancy": "default", 
                        "termination_protection": null, 
                        "user_data": null, 
                        "validate_certs": true, 
                        "volumes": [
                            {
                                "device_name": "/dev/sda1", 
                                "volume_size": 100, 
                                "volume_type": "gp2"
                            }
                        ], 
                        "vpc_subnet_id": "subnet-88d171ff", 
                        "wait": true, 
                        "wait_timeout": "500", 
                        "zone": null
                    }
                }, 
                "item": "b", 
                "tagged_instances": []
            }
        ]
    }
}

Please help

回答1:

I don't have good knowledge about AWS as per your output of the playbook and the playbook you posted.

           "invocation": {
                "module_args": {
                    "assign_public_ip": false, 
                    "aws_access_key": null, 
                    "aws_secret_key": null, 
                    "count": 3, 
                    "count_tag": null, 
                    "ebs_optimized": false, 
                    "ec2_url": null, 
                    "exact_count": null, 
                    "group": [
                        "mayassar-es-external", 
                        "elasticsearch-mayassar-production", 
                        "Test_nishant"
                    ], 

Here the count is 3, so it creates three instances in both the regions a and b

If you want to traverse output you can use the following method

http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-subelements