# ec2 module how to differ servers in the same launch

**URL:** <https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486>\
**Category:** Ansible Project\
**Created:** [September 16, 2013, 3:50pm UTC](https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486 "2013-09-16T15:50:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [September 16, 2013, 3:50pm UTC](https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486/1 "2013-09-16T15:50:43Z")

</div>

Hi all,

Following the example in [The Manifesto for Agentic Automation](http://www.ansibleworks.com/docs/modules.html#ec2) I have the bellow playbook :

ec2-example.yml :  
“”"

# Launch instances, runs some tasks - name: Define mysql launch hosts: localhost gather\_facts: False vars: keypair: my\_keypair instance\_type: m1.small security\_group: my\_securitygroup image: my\_ami\_id region: us-east-1 tasks: - name: Launch instance local\_action: ec2 keypair={{ keypair }} group={{ security\_group }} instance\_type={{ instance\_type }} image={{ image }} count=5 wait=true region={{ region }} register: ec2 - name: Add new instance to host group local\_action: add\_host hostname={{ item.public\_ip }} groupname=launched with\_items: ec2.instances - name: Wait for SSH to come up local\_action: wait\_for host={{ item.public\_dns\_name }} port=22 delay=60 timeout=320 state=started with\_items: ec2.instances - name: Configure instance(s) hosts: launched sudo: True gather\_facts: True roles: - mysql\_role

“”"

As you can see this will launch 5 mysql servers ( count=5) , the configuration of all mysql servers is almost the same since they are part of the same mysql\_role. the only small difference is that I have in my roles a variable that defines if the mysql server will be the master or slave. Is there a way to define that the first instance launched will be the master and the rest will br slaves ??? This is not only a mysql setup case but I have more cases where servers are been setup by the same role but differ because of a variable..

I know I could separate the above in two different tasks or playbooks and use the add\_host hostname={{ item.public\_ip }} groupname=mysql-master / groupname=mysql-slave but I’m wondering if I could do this in a single playbook.

Also is there a way I could add the servers from the add\_host to a local file ?

Regards,  
Nicolas.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 16, 2013, 9:20pm UTC](https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486/2 "2013-09-16T21:20:39Z")

</div>

You can tag the instance as you like and then use the ec2 inventory script to apply roles based on the tag.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [September 17, 2013, 9:25am UTC](https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486/3 "2013-09-17T09:25:42Z")

</div>

I would like to tag the instance automatically based on the instance launch sequence and avoid any manual steps.

Is it possible to use ec2.instance\_ids\_#1 for the first instance launched, ec2.instance\_ids\_#2 for the second etc… ?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 17, 2013, 1:16pm UTC](https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486/4 "2013-09-17T13:16:33Z")

</div>

It sounds like you want to do something like this:

ec2: … instance\_tags=“mytag={{ item }}”  
with\_items:

- tag1
- tag2
- …
- tagN

---

<div class="post-metadata">

**Author:** ![Robert\_Wehner](https://avatars.discourse-cdn.com/v4/letter/r/51bf81/32.png) [@Robert\_Wehner](https://forum.ansible.com/u/Robert_Wehner)\
**Post date:** [September 18, 2013, 6:05pm UTC](https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486/5 "2013-09-18T18:05:48Z")

</div>

Another way to approach this that we have used successfully is to tag the instances based on the ‘ami\_launch\_index’ variable. This is only useful when you have created multiple instances in a single call to ec2 (as in your example of 5). If you spin these up separately, the ami\_launch\_index is always 0 which won’t work. Something like this in your playbook would probably work:

- name: Tag the Master MySQL Server  
local\_action: ec2\_tag resource={{ item.id }} region={{ region }} state=present  
with\_items: ec2.instances  
when: item[‘ami\_launch\_index’] == ‘0’  
args:  
tags:  
roles: mysql\_master

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [September 19, 2013, 8:28am UTC](https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486/6 "2013-09-19T08:28:05Z")

</div>

Thanks Robert, that’s exactly what I was looking for.

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [September 23, 2013, 11:16am UTC](https://forum.ansible.com/t/ec2-module-how-to-differ-servers-in-the-same-launch/13486/7 "2013-09-23T11:16:24Z")

</div>

do you know if there is a way to use the item[‘ami\_launch\_index’] directly in the ec2 module instead of ec2\_tag module ?
