使用Ansible提供多个EC2实例

时间:2017-04-26 04:27:43

标签: amazon-ec2 ansible ansible-2.x

我正在尝试使用Ansible配置多台AWS EC2计算机。我想让“ availabilityZone ”和“ tag_name ”动态化。请在下面找到任务。如果我将计数更改为2,我应该能够在没有任何修改的情况下运行相同的任务来创建两个实例。

- name: Provision an instance
  ec2:
    key_name: "{{ keyPairName }}"
    instance_type: m3.medium
    image: "{{ amiId }}"
    region: us-east-1
    zone: us-east-1{{ item.0 }}
    group_id: "{{ securityGroup }}"
    vpc_subnet_id: "{{ subnet }}"
    wait: true
    count: 1
    instance_tags:
      Name: esl-master{{ item.1 }}-{{ deployenv }}
  register: ec2_master
  with_together:
    - [ 'a', 'b', 'c' ]
    - [ 1, 2, 3 ]
  when: isMaster

使用上述配置,我收到以下错误。

failed: [localhost] (item=[u'b', 2]) => {"failed": true, "item": ["b", 2], "msg": "Instance creation failed => InvalidParameterValue: Value (us-east-1b) for parameter availabilityZone is invalid. Subnet 'subnet-f22551d9' is in the availability zone us-east-1a"}
failed: [localhost] (item=[u'c', 3]) => {"failed": true, "item": ["c", 3], "msg": "Instance creation failed => InvalidParameterValue: Value (us-east-1c) for parameter availabilityZone is invalid. Subnet 'subnet-f22551d9' is in the availability zone us-east-1a"}

有没有更好的方法,因为我想利用'Zone'参数?

1 个答案:

答案 0 :(得分:1)

对于每次迭代,您尝试设置相同的vpc_subnet_id: "{{ subnet }}",并且在第二个和第三个中出现错误,因为已经使用了子网ID。

您需要使用不同的子网ID,例如

vpc_subnet_id: "{{ subnet }}{{ item.0 }}"