通过ansible创建实例并进行标记 [英] Creating instance and tagging via ansible

查看:110
本文介绍了通过ansible创建实例并进行标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ansible在不同的AZ中创建实例,我在这里面临两个问题:

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

  1. 我需要在AZ中创建3个实例,但是下面的播放创建了6个实例,使得AZ-a中的3个实例和AZ-b中的3个实例
  2. 我正在尝试使用创建序列时标记这些实例,但是我无法传递/遍历json 返回.
  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.

这是戏剧:

---
  - 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 }}"

我不确定问题1是如何发生的以及如何解决,对于第二个问题,我需要遍历结果的帮助,以便可以在文件中标记和创建清单.

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.

调试的输出

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

请帮助

推荐答案

根据您的剧本和您发布的剧本的输出,我对AWS并不了解.

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"
                    ], 

此处 count 为3,因此它将在两个区域中创建三个实例 a b

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

这篇关于通过ansible创建实例并进行标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆