Vinil
Vinil

Reputation: 33

Provision of multiple EC2 instances with Ansible

I am trying to provision multiple AWS EC2 machines using Ansible. I wanted to make the "availabilityZone" and "tag_name" dynamic. Please find the task below. If I change the count to 2, i should be able to run the same task without any modifications to create two instances.

- 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

With the above configuration, i am getting the below error.

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

Is there a better way of doing this, since i want to take advantage of 'Zone' parameter ?

Upvotes: 0

Views: 1006

Answers (1)

techraf
techraf

Reputation: 68609

For every iteration you try to set the same vpc_subnet_id: "{{ subnet }}" and you get an error in the second and third one as the subnet ID, was already used.

You need to use different subnet IDs, e.g.

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

Upvotes: 1

Related Questions