# Create ec2 instances with sequential name tags .

**URL:** <https://forum.ansible.com/t/create-ec2-instances-with-sequential-name-tags/26211>\
**Category:** Ansible Project\
**Created:** [December 6, 2017, 11:07am UTC](https://forum.ansible.com/t/create-ec2-instances-with-sequential-name-tags/26211 "2017-12-06T11:07:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arvind\_Sharma](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@Arvind\_Sharma](https://forum.ansible.com/u/Arvind_Sharma)\
**Post date:** [December 6, 2017, 11:07am UTC](https://forum.ansible.com/t/create-ec2-instances-with-sequential-name-tags/26211/1 "2017-12-06T11:07:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 6, 2017, 11:22am UTC](https://forum.ansible.com/t/create-ec2-instances-with-sequential-name-tags/26211/2 "2017-12-06T11:22:16Z")

</div>

> 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  
> &nbsp;&nbsp;ec2:  
> &nbsp;&nbsp;&nbsp;&nbsp;key\_name: "{{ key\_pair }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;instance\_type: "{{ instance\_type }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;image: "{{ ami\_id }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;region: "{{ region }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;wait: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;group\_id: "{{ security\_group\_id }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;count: "{{ instance\_count }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;vpc\_subnet\_id: "{{ subnet\_id }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;assign\_public\_ip: no  
> &nbsp;&nbsp;&nbsp;&nbsp;termination\_protection: no  
> &nbsp;&nbsp;register: ec2
> 
> - name: Generate Sequence Number  
> &nbsp;&nbsp;with\_sequence:  
> &nbsp;&nbsp;&nbsp;&nbsp;start={{ start\_index }}  
> &nbsp;&nbsp;&nbsp;&nbsp;end={{ instance\_count }}  
> &nbsp;&nbsp;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  
> &nbsp;&nbsp;ec2\_tag:  
> &nbsp;&nbsp;&nbsp;&nbsp;region: "{{ region }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;resource: "{{ item.0.id }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;tags:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: "{{tag\_key\_vals.Name}}-{{ item.1.msg }}"  
> &nbsp;&nbsp;with\_together:  
> &nbsp;&nbsp;&nbsp;&nbsp;- "{{ ec2.instances }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;- "{{ 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](http://jinja.pocoo.org/docs/dev/templates/#range)
