ec2 module how to differ servers in the same launch

Hi all,

Following the example in 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.

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

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… ?

It sounds like you want to do something like this:

ec2: … instance_tags=“mytag={{ item }}”
with_items:

  • tag1
  • tag2
  • tagN

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

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

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 ?