AWS ec2_elb register multiple instances to and elb?

Hi,

I’m trying to add instances to an ELB, I may be looking at this the wrong way.

I have X number of instances, running ElasticSearch for example.
I want to create an ELB, and add (register) all the instance ids, with my “elasticsearch” tag, to that ELB.

Is the “ec2_elb” module designed to “register a single instance id to a list of ELBs, at the instance creation time”, as opposed to, “register a list of instance ids, to a single ELB, at any stage”?

So, what I am trying to to:-

  1. create ec2 instances
  2. install elasticsearch on all “tag:Name=elasticsearch”
  3. create ELB
  4. get list of all instance ids with “tag:Name=elasticsearch”
  5. register this list with ELB
  • name: add instances to LB
    ec2_elb:
    instance_id: “{{ item }}”
    ec2_elbs: “{{ my_elb_name }}”
    state: present
    with_items: my_es_instances.stdout_lines

TASK: [elb | debug var=my_es_instances] ***************************************
ok: [localhost] => {
“my_es_instances”: {
“changed”: true,
“cmd”: “ec2-describe-instances --hide-tags --region eu-west-1 -F ‘tag:Name=elasticsearch’ | grep INSTANCE | awk ‘{print $2}’”,
“delta”: “0:00:01.477150”,
“end”: “2015-03-06 17:27:25.737070”,
“invocation”: {
“module_args”: “ec2-describe-instances --hide-tags --region eu-west-1 -F ‘tag:Name=elasticsearch’ | grep INSTANCE | awk ‘{print $2}’”,
“module_name”: “shell”
},
“rc”: 0,
“start”: “2015-03-06 17:27:24.259920”,
“stderr”: “”,
“stdout”: “i-123456\ni-abcdef”,
“stdout_lines”: [
“i-123456”,
“i-abcdef”
],
“warnings”:
}
}

Step 5 is where I’m stuck. Am I loosing my mind?
thanks
e

Im facing a similar issue where my workflow is as follows

  1. deploy code to an instance
  2. create an ami from the said instance
  3. spawn x instances from the said AMI
  4. register all newly created instances to the elb using ec2_elb module

step 4) code looks like

  • name: adding the new instances to the ELB

ec2_elb:

aws_access_key: “{{ ec2_access_key }}”

aws_secret_key: “{{ ec2_secret_key }}”

region: “{{ region }}”

instance_id: “{{ new_instance_ids.instance_ids }}”

ec2_elbs: ansible-test

state: present

wait: yes

new_instance_ids is what i register after the instances are created and query the instance_id field and get the below error

failed: [test-ami-2] => {“failed”: true, “parsed”: false}

SUDO-SUCCESS-fscsgqdhhmhsmegnguzilbdrrlylbpbk

Traceback (most recent call last):

File “/home/ec2-user/.ansible/tmp/ansible-tmp-1425971748.07-61887153074694/ec2_elb”, line 2126, in

main()

File “/home/ec2-user/.ansible/tmp/ansible-tmp-1425971748.07-61887153074694/ec2_elb”, line 326, in main

elb_man.register(wait, enable_availability_zone, timeout)

File “/home/ec2-user/.ansible/tmp/ansible-tmp-1425971748.07-61887153074694/ec2_elb”, line 154, in register

self._enable_availailability_zone(lb)

File “/home/ec2-user/.ansible/tmp/ansible-tmp-1425971748.07-61887153074694/ec2_elb”, line 179, in _enable_availailability_zone

instance = self._get_instance()

File “/home/ec2-user/.ansible/tmp/ansible-tmp-1425971748.07-61887153074694/ec2_elb”, line 283, in _get_instance

return ec2.get_only_instances(instance_ids=[self.instance_id])[0]

AttributeError: ‘EC2Connection’ object has no attribute ‘get_only_instances’

nevermind i figured my ec2_elb problem was though . the instance_ids need to be passed in via complex args , since they are an array and the standard key=value argument method would not work here . To workaround this , I passed the ids through the jinja2 join filter
instance_id: ‘{{ new_instances.instance_ids|join(“,”) }}’

This will turn the argument into instance_ids=id1,id2,id3, and the parameter parsing in the module will know to turn it back into a list based on the fact that it is set to type=‘list’.

Eion : You should try and do this to fix your problem

Update:

This only works when you have value from instance_ids . Having multiple id’s creates a malformed string “i-xxxxx,i-xxxx”
while it should look like “i-4f8cf126’, 'i-0bb7ca62”

How can this be fixed ? Any help would be appreciated

Thanks Vishal,

getting closer, thanks for the help … I think I’m simply in “YAML wonderland”, I’m almost at the strategy of “pressing every button” to see what works!! :slight_smile:

So, for me … this will work

`

  • name: add instances to LB
    ec2_elb:
    instance_id: “{{ item }}”
    ec2_elbs: “{{ my_name }}”
    state: present
    with_items:
  • “{{ my_es_instances.stdout_lines[0] }}”
  • “{{ my_es_instances.stdout_lines[1] }}”

`

so, what I want is to be able to create a list … from (what I thought was already a list!) a variable, without referencing each item [0], [1], etc …

where “my_es_instances” is:-

`

TASK: [elb | debug var=my_es_instances] ***************************************
ok: [localhost] => {
“my_es_instances”: {
“changed”: true,
“cmd”: “ec2-describe-instances --hide-tags --region eu-west-1 -F ‘tag:Name=elasticsearch’ | grep INSTANCE | awk ‘{print $2}’”,
“delta”: “0:00:01.525650”,
“end”: “2015-03-12 15:10:27.021719”,
“invocation”: {
“module_args”: “ec2-describe-instances --hide-tags --region eu-west-1 -F ‘tag:Name=elasticsearch’ | grep INSTANCE | awk ‘{print $2}’”,
“module_name”: “shell”
},
“rc”: 0,
“start”: “2015-03-12 15:10:25.496069”,
“stderr”: “”,
“stdout”: “i-fca5221b\ni-8ae76a6d”,
“stdout_lines”: [
“i-fca5221b”,
“i-8ae76a6d”
],
“warnings”:
}
}

`

thanks
e