【问题标题】:Use ansible to create ec2 instances in different AZ使用ansible在不同AZ创建ec2实例
【发布时间】:2020-08-04 23:38:47
【问题描述】:

我创建了一个剧本来在不同的可用区部署 ec2 实例。当我为计数区域和子网添加“with_items”时它失败了。如何克服这一点。

我关注了这个Ansible: Create instances in different subnets,但它不起作用所以创建了一个新的 oone,因为我无法更新它

剧本来了

- name: Deploy EC2 Master node
  hosts: localhost
  gather_facts: False
  vars_files:
    - ~/aws-common/automation/ansible/config/var_input.yml
  tasks:
    - name: Get AWS credentials
      sts_assume_role:
        role_arn: "{{ role_arn }}"
        role_session_name: "{{ role_session_name }}"
      register: assumed_role
    - name: Provision a set of instances
      ec2:
         key_name: "{{ key_name }}"
         group: "{{ group }}"
         instance_type: "{{ instance_type }}"
         region: "{{ region }}"
         image: "{{ image }}"
         wait: "{{ wait }}"
         wait_timeout: "{{ wait_timeout }}"
         #count: "{{ count }}"
         count: "{{ item.ec2_count }}"
         instance_profile_name: "{{ instance_profile_name }}"
         instance_tags:
           Name: "{{ Name }}"
           deployer: "{{ deployer }}"
           resourceowner: "{{ resourceowner }}"
         monitoring: "{{ monitoring }}"
         ec2_zone: "{{ item.zone }}"
         vpc_subnet_id: "{{ item.subnet }}"
         assign_public_ip: "{{ assign_public_ip }}"
         aws_access_key: "{{ assumed_role.sts_creds.access_key }}"
         aws_secret_key: "{{ assumed_role.sts_creds.secret_key }}"
         security_token: "{{ assumed_role.sts_creds.session_token }}"
         volumes:
         - device_name: /dev/sda1
           volume_type: gp2
           encrypted: true
           volume_name: HadoopMaster-rootvolume /
           volume_size: 100
           delete_on_termination: true
         - device_name: /dev/sdf
           volume_type: gp2
           encrypted: true
           volume_name:  HadoopMaster /edw/xxx
           volume_size: 70
           delete_on_termination: true
      with_items: "{{ ec2_zone_subnet }}"
      register: ec2

    - name: Add new instance to host group
      add_host:
        hostname: "{{ item.public_ip }}"
        groupname: launched
      loop: "{{ ec2.instances }}"

    - name: Wait for SSH to come up
      delegate_to: "{{ item.public_dns_name }}"
      wait_for_connection:
        delay: 60
        timeout: 320
      loop: "{{ ec2.instances }}"

- hosts: launched
  name: Mounting the attached EBS volumes
  user: ec2-user
  become: yes
  gather_facts: no
  tasks:
      - name: Run a script with arguments (free form)
        script: ~/files/format.sh

这是我在变量文件中的条目

ec2_zone_subnet: [{"zone":"us-east-1a", "subnet":"subnet-5f87a23b", "ec2_count":1},{"zone":"us-east-1b", "subnet":"subnet-a9d2bd86","ec2_count":1},{"zone":"us-east-1c", "subnet":"subnet-0cab05cf89718e652","ec2_count":0},{"zone":"us-east-1d", "subnet":"subnet-079c66d30dcd53ce7","ec2_count":0}]

我得到的错误

ok: [localhost] => (item={u'subnet': u'subnet-0cab05cf89718e652', u'ec2_count': 0, u'zone': u'us-east-1c'}) => {"changed": false, "instance_ids": [], "instances": [], "item": {"ec2_count": 0, "subnet": "subnet-0cab05cf89718e652", "zone": "us-east-1c"}, "tagged_instances": []}
ok: [localhost] => (item={u'subnet': u'subnet-079c66d30dcd53ce7', u'ec2_count': 0, u'zone': u'us-east-1d'}) => {"changed": false, "instance_ids": [], "instances": [], "item": {"ec2_count": 0, "subnet": "subnet-079c66d30dcd53ce7", "zone": "us-east-1d"}, "tagged_instances": []}

TASK [Add new instance to host group] **********************************************************************************************************************
task path: /home/desind/aws-common/automation/ansible/files/testnew.yaml:51
fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute 'instances'"}

这些物品现在只有ec2-countsubnethost。我没有看到其他属性。有人可以帮我使用 register:ec2 和 with_items

【问题讨论】:

  • 我不知道为什么有人投了反对票,这个问题清楚有效。

标签: amazon-ec2 ansible


【解决方案1】:

这些是我对剧本所做的以下更改

  1. 已更改 vpc_subnet_id:“{{ item }}”
  2. with_items: "{{ subnet_ids }}"
- name: Add all instance private IPs to host group
      add_host: hostname={{ item.instances[0].private_ip }} instance_id={{ item.instance_ids[0] }} groups=launched
      with_items: '{{ ec2.results }}'

    - name: Wait for SSH to come up
      delegate_to: "{{ item.instances[0].private_ip }}"
      wait_for_connection:
        delay: 100
        timeout: 320
      loop: "{{ ec2.results }}"
  1. 在变量文件中添加了这样的子网 subnet_ids:[子网-xxxx,子网-axxx,子网-xxxx]

【讨论】:

    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多