Extracting Single IP from launched EC2 instances

97 views
Skip to first unread message

MikeA

unread,
Apr 1, 2015, 5:16:12 PM4/1/15
to ansible...@googlegroups.com
I'm launching a cluster of ec2 instances and would like one (don't care which one) of them to become a head node and pass it's IP to the other cluster members. I've been looking for an easy way to do this but could not fine one so would appreciate any assistance.

Here is what I've got.
from my main playbook I'm calling a sub playbook that launches the ec2 instances. there i add the instances to the {{ group_name }} group. where group_name is a variable defined in a var file.

later on the main playbook I'd like to pull one of the IP's and feed it into a startup script to be called on all the hosts.

I could not find a way to extract element 0 from the group_name var array.I tried this: set_fact: headnode={{ groups[[{{ group_name }}][0]] }} but it did not work. I had to use the actual group_var value and then I got the IP but it was not a clean string rather it looked like this: [u'10.77.0.211', 

Any idea how to get a clean IP from the group_name set? BDW in the sub playbook where I launch the instances I register the private ip's from the instances: set_fact: private_ips: "{{ ec2.tagged_instances|join(', ', attribute='private_ip') }}" but I cannot refer to this variable back in the main playbook.

thanks in advance.

James Martin

unread,
Apr 3, 2015, 2:26:54 PM4/3/15
to ansible...@googlegroups.com
Mike,

This may give you some insight:


Particularly:   

primary_node: "{{ hostvars[groups['riak_cluster'][0]]['riak_outputs']['node_name'] }}"

- James

Tzach Livyatan

unread,
Apr 12, 2015, 5:29:03 AM4/12/15
to ansible...@googlegroups.com
Here is a similar example from my project

Creating the instance on EC2:
---
- name: Create a cluster nodes on EC2
  hosts: localhost
  connection: local

  tasks:
    - name: Launch instances
      local_action:
        module: ec2
        region: "{{region}}"
        keypair: "{{key_name}}"
        group: "{{security_group}}"
        instance_type: "{{instance_type}}"
        image: "{{image}}"
            count: "{{cluster_nodes}}"
        wait: yes
      register: ec2_cluster

        - name: Add instances to private ip group
      local_action: add_host name={{item.private_ip}} groupname=Private
      with_items: ec2_cluster.instances

In the role vars/main.yaml:
seed: "{{groups.Private[0]}}"

And using "seed" in the relevant template.

MikeA

unread,
Apr 30, 2015, 1:08:37 PM4/30/15
to ansible...@googlegroups.com
That worked. Thanks!

jbr...@mozilla.com

unread,
Apr 30, 2015, 2:28:24 PM4/30/15
to ansible...@googlegroups.com
Anyone know if there is a way to do this same sort of discovery based on EC2 tags? i.e. Gather the IP associated with tag role: master and make use of it?
Reply all
Reply to author
Forward
0 new messages