如何使用 ANSIBLE 在不同的相应子网中旋转多个 EC2 实例?

How to spin a number of EC2 instances in different corresponding subnets using ANSIBLE?

我正在尝试在不同可用区和子网中使用 Ansible 旋转多个 EC2 实例,我在这里感到困惑的是如何传递与正确区域对应的正确子网?

假设我将我的子网变量传递为:

subnet_id_a: "subnet-9c3e38f8"
subnet_id_b: "subnet-88d171ff"

现在这些子网在不同的可用区中,我需要创建 n 个实例,这些实例需要在不同的可用区中旋转

我正在尝试使用:

  - 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_id }}"
      zone: "{{ aws_region }}{{ item.0 }}"
      region: "{{ aws_region }}"
      group: "{{ aws_sec_group_name }}"
    with_items:
      - [ 'a' , 'b']
    register: ec2_details

但我不确定如何根据此传递相应的子网,以便每个实例都在不同的 az 中旋转?请帮助

你可以像这样重构变量:

subnet_ids:
  a: subnet-9c3e38f8
  b: subnet-88d171ff

在你的任务中:

...
vpc_subnet_id: "{{ subnet_ids[item] }}"
...
with_items: [a, b]

我想 zone 没有必要,因为子网已经绑定到某个 AZ。
而且你不需要像这样 - [a, b] 嵌套你的循环列表,只使用 [a, b] 来避免 item.0.