# AWS ec2\_elb register multiple instances to and elb?

**URL:** <https://forum.ansible.com/t/aws-ec2-elb-register-multiple-instances-to-and-elb/17700>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [March 9, 2015, 1:36pm UTC](https://forum.ansible.com/t/aws-ec2-elb-register-multiple-instances-to-and-elb/17700 "2015-03-09T13:36:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eoin\_Verling](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Eoin\_Verling](https://forum.ansible.com/u/Eoin_Verling)\
**Post date:** [March 9, 2015, 1:36pm UTC](https://forum.ansible.com/t/aws-ec2-elb-register-multiple-instances-to-and-elb/17700/1 "2015-03-09T13:36:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vishal\_Uderani](https://avatars.discourse-cdn.com/v4/letter/v/c4cdca/32.png) [@Vishal\_Uderani](https://forum.ansible.com/u/Vishal_Uderani)\
**Post date:** [March 10, 2015, 7:18am UTC](https://forum.ansible.com/t/aws-ec2-elb-register-multiple-instances-to-and-elb/17700/2 "2015-03-10T07:18:44Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![Vishal\_Uderani](https://avatars.discourse-cdn.com/v4/letter/v/c4cdca/32.png) [@Vishal\_Uderani](https://forum.ansible.com/u/Vishal_Uderani)\
**Post date:** [March 11, 2015, 6:20am UTC](https://forum.ansible.com/t/aws-ec2-elb-register-multiple-instances-to-and-elb/17700/3 "2015-03-11T06:20:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Vishal\_Uderani](https://avatars.discourse-cdn.com/v4/letter/v/c4cdca/32.png) [@Vishal\_Uderani](https://forum.ansible.com/u/Vishal_Uderani)\
**Post date:** [March 11, 2015, 8:24am UTC](https://forum.ansible.com/t/aws-ec2-elb-register-multiple-instances-to-and-elb/17700/4 "2015-03-11T08:24:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Eoin\_Verling](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Eoin\_Verling](https://forum.ansible.com/u/Eoin_Verling)\
**Post date:** [March 12, 2015, 3:14pm UTC](https://forum.ansible.com/t/aws-ec2-elb-register-multiple-instances-to-and-elb/17700/5 "2015-03-12T15:14:59Z")

</div>

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!! 🙂

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
