Create ec2 instances with sequential name tags .

Hi Everyone ,

I am very new to Ansible and currently struggling for getting this done ,

I wrote a ec2 role which launch ec2 instances based on count provided by vars , role is working fine and I have also used tags , A tag with Name is also added while launch , My requirement is I want to add some sequence along with my tags , but I am not able to achieve same .

I tried some stackoverflow solutions but it didnt work ,

My launch task in ec2 role is

  • name: Launch EC2 instances

ec2:

key_name: “{{ key_pair }}”

instance_type: “{{ instance_type }}”

image: “{{ ami_id }}”

region: “{{ region }}”

wait: yes

group_id: “{{ security_group_id }}”

count: “{{ instance_count }}”

vpc_subnet_id: “{{ subnet_id }}”

assign_public_ip: no

termination_protection: no

register: ec2

  • name: Generate Sequence Number

with_sequence:

start={{ start_index }}

end={{ instance_count }}

register: sequence

  • name: tag instances

ec2_tag:

region: “{{ region }}”

resource: “{{ item.0.id }}”

tags:

Name: “{{tag_key_vals.Name}}-{{ item.1.msg }}”

with_together:

  • “{{ ec2.instances }}”

  • “{{ sequence.results }}”

But again this is not working .

Please suggest some way to do same.

Regards

Arvind

I am very new to Ansible and currently struggling for getting this done ,

I wrote a ec2 role which launch ec2 instances based on count provided by
vars , role is working fine and I have also used tags , A tag with Name is
also added while launch , My requirement is I want to add some sequence
along with my tags , but I am not able to achieve same .

I tried some stackoverflow solutions but it didnt work ,

I haven't used ec2 modules and can't comment on that, but I'll make some comments on the Ansible part.

My launch task in ec2 role is

- name: Launch EC2 instances
  ec2:
    key_name: "{{ key_pair }}"
    instance_type: "{{ instance_type }}"
    image: "{{ ami_id }}"
    region: "{{ region }}"
    wait: yes
    group_id: "{{ security_group_id }}"
    count: "{{ instance_count }}"
    vpc_subnet_id: "{{ subnet_id }}"
    assign_public_ip: no
    termination_protection: no
  register: ec2

- name: Generate Sequence Number
  with_sequence:
    start={{ start_index }}
    end={{ instance_count }}
  register: sequence

This task will not work, with_sequence is not a module but a directive to the task.
So this is not at valid task since it is missing a module.

- name: tag instances
  ec2_tag:
    region: "{{ region }}"
    resource: "{{ item.0.id }}"
    tags:
      Name: "{{tag_key_vals.Name}}-{{ item.1.msg }}"
  with_together:
    - "{{ ec2.instances }}"
    - "{{ sequence.results }}"

Instead of sequence.results you can replace that with the range[1] filter

- "{{ range(start_index, instance_count) }}"

[1] http://jinja.pocoo.org/docs/dev/templates/#range