# Need assist with syntax for EC2 assignment of Elastic IP Address

**URL:** <https://forum.ansible.com/t/need-assist-with-syntax-for-ec2-assignment-of-elastic-ip-address/15025>\
**Category:** Ansible Project\
**Created:** [April 15, 2014, 9:52pm UTC](https://forum.ansible.com/t/need-assist-with-syntax-for-ec2-assignment-of-elastic-ip-address/15025 "2014-04-15T21:52:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark\_david\_mcCreary](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mark\_david\_mcCreary](https://forum.ansible.com/u/mark_david_mcCreary)\
**Post date:** [April 15, 2014, 9:52pm UTC](https://forum.ansible.com/t/need-assist-with-syntax-for-ec2-assignment-of-elastic-ip-address/15025/1 "2014-04-15T21:52:20Z")

</div>

I want to finish off an Ansible playbook by assigning a static IP address to it. I want to do this at the end, because I think it takes a few minutes to take effect. Not sure if doing it at the beginning would cause Ansible to loose the connection or not.

I launch an instance like this

- name: Launch instance  
local\_action: ec2 keypair={{ keypair }} instance\_type={{ instance\_type }}  
image={{ image }} region={{ region }}  
aws\_access\_key={{ aws\_access\_key }}  
aws\_secret\_key={{ aws\_secret\_key }}  
group={{ security\_group }} wait=true  
register: ec2

I’ve tried a couple of different ways, both fail.

- name: associate [testing.example.com](http://testing.example.com) with this instance  
local\_action: ec2\_eip instance\_id={{ item.id }} ip=54.243.243.102 region=us-east-1  
with\_items: ec2.instances

TASK: [missing | associate [testing.example.com](http://testing.example.com) with this instance] \*\*\*\*\*\*  
fatal: [54.198.204.2] =\> One or more undefined variables: ‘str object’ has no attribute ‘id’

- name: associate [testing.example.com](http://testing.example.com) with this instance  
local\_action: ec2\_eip instance\_id={{ ec2.instance\_ids[0] }} ip=54.243.243.102 region=us-east-1

TASK: [missing | associate [testing.example.com](http://testing.example.com) with this instance] \*\*\*\*\*\*  
fatal: [54.197.175.124] =\> One or more undefined variables: ‘ec2’ is undefined

Can somebody show me the correct syntax.

Thanks

mark

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 15, 2014, 10:39pm UTC](https://forum.ansible.com/t/need-assist-with-syntax-for-ec2-assignment-of-elastic-ip-address/15025/2 "2014-04-15T22:39:45Z")

</div>

First run with the “-v” flag and see what the returns from the ec2 module looks like, which will help with testing how to use the resultant data.

---

<div class="post-metadata">

**Author:** ![mark\_david\_mcCreary](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mark\_david\_mcCreary](https://forum.ansible.com/u/mark_david_mcCreary)\
**Post date:** [April 16, 2014, 9:42pm UTC](https://forum.ansible.com/t/need-assist-with-syntax-for-ec2-assignment-of-elastic-ip-address/15025/3 "2014-04-16T21:42:29Z")

</div>

My playbook looks like this

---

<div class="post-metadata">

**Author:** ![mark\_david\_mcCreary](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@mark\_david\_mcCreary](https://forum.ansible.com/u/mark_david_mcCreary)\
**Post date:** [April 16, 2014, 10:11pm UTC](https://forum.ansible.com/t/need-assist-with-syntax-for-ec2-assignment-of-elastic-ip-address/15025/4 "2014-04-16T22:11:53Z")

</div>

If I reverse the order, like this

- 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: associate [test.example.com](http://test.example.com) with this instance  
local\_action: ec2\_eip instance\_id={{ item.id }} ip=54.123.243.14 region=us-east-1  
with\_items: ec2.instances

it gets through those ok, but hangs in the middle of the next stanza.

So assigning an Elastic IP address disturbs the environment.

mark

---

<div class="post-metadata">

**Author:** ![vaida.dan](https://avatars.discourse-cdn.com/v4/letter/v/5daacb/32.png) [@vaida.dan](https://forum.ansible.com/u/vaida.dan)\
**Post date:** [June 23, 2014, 9:20am UTC](https://forum.ansible.com/t/need-assist-with-syntax-for-ec2-assignment-of-elastic-ip-address/15025/5 "2014-06-23T09:20:28Z")

</div>

Hi Mark.

This is the typical outcome of AWS’ flow. When you assign an EIP to an instance, you can forget about the public dns and public ip entries. they are updated according to the EIP.  
As a tip, I am registering the output of the ec2\_eip module and use that instead of querying the ec2 module’s results. One can also (supposedly) use the ec2\_facts module or just output the added EIP to a file with the lineinfile module and read it from there or use the in-memory inventory with add\_host.

FYI, here’s what ec2\_facts gets:

`  
[dvaida@scat ansible-aws]$ ansible -i host all --user=admin --private-key=ansible-ec2.pem -m ec2\_facts  
The authenticity of host ‘54.76.235.184 (54.76.235.184)’ can’t be established.  
ECDSA key fingerprint is ec:11:61:76:a0:28:b5:83:10:a7:3c:b5:d8:02:71:bb.  
Are you sure you want to continue connecting (yes/no)? yes  
54.76.235.184 | success \>\> {  
“ansible\_facts”: {  
“ansible\_ec2\_ami\_id”: “ami-630fcb14”,  
“ansible\_ec2\_ami\_launch\_index”: “0”,  
“ansible\_ec2\_ami\_manifest\_path”: “(unknown)”,  
“ansible\_ec2\_block\_device\_mapping\_ami”: “/dev/sda”,  
“ansible\_ec2\_block\_device\_mapping\_root”: “/dev/sda”,  
“ansible\_ec2\_hostname”: “ip-10-0-0-8.eu-west-1.compute.internal”,  
“ansible\_ec2\_instance\_action”: “none”,  
“ansible\_ec2\_instance\_id”: “i-9edd71dd”,  
“ansible\_ec2\_instance\_type”: “c3.2xlarge”,  
“ansible\_ec2\_kernel\_id”: “aki-52a34525”,  
“ansible\_ec2\_local\_hostname”: “ip-10-0-0-8.eu-west-1.compute.internal”,  
“ansible\_ec2\_local\_ipv4”: “10.0.0.8”,  
“ansible\_ec2\_mac”: “06:31:2e:4a:82:e2”,  
“ansible\_ec2\_metrics\_vhostmd”: “\<?xml version=\"1.0\" encoding=\"UTF-8\"?\>”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_device\_number”: “0”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_interface\_id”: “eni-4fb93f38”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_ipv4\_associations\_54.76.235.184”: “10.0.0.8”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_local\_hostname”: “ip-10-0-0-8.eu-west-1.compute.internal”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_local\_ipv4s”: “10.0.0.8”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_mac”: “06:31:2e:4a:82:e2”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_owner\_id”: “785351690827”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_public\_hostname”: “[ec2-54-76-235-184.eu-west-1.compute.amazonaws.com](http://ec2-54-76-235-184.eu-west-1.compute.amazonaws.com)”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_public\_ipv4s”: “54.76.235.184”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_security\_group\_ids”: “sg-0b10de6e”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_security\_groups”: “allinone”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_subnet\_id”: “subnet-ddca27aa”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_subnet\_ipv4\_cidr\_block”: “10.0.0.0/24”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_vpc\_id”: “vpc-9037d9f5”,  
“ansible\_ec2\_network\_interfaces\_macs\_06\_31\_2e\_4a\_82\_e2\_vpc\_ipv4\_cidr\_block”: “10.0.0.0/16”,  
“ansible\_ec2\_placement\_availability\_zone”: “eu-west-1a”,  
“ansible\_ec2\_placement\_region”: “eu-west-1”,  
“ansible\_ec2\_profile”: “default-paravirtual”,  
“ansible\_ec2\_public\_hostname”: “[ec2-54-76-235-184.eu-west-1.compute.amazonaws.com](http://ec2-54-76-235-184.eu-west-1.compute.amazonaws.com)”,  
“ansible\_ec2\_public\_ipv4”: “54.76.235.184”,  
“ansible\_ec2\_public\_key”: “ssh-rsa XXX ansible-ec2\n”,  
“ansible\_ec2\_reservation\_id”: “r-a5fe14e6”,  
“ansible\_ec2\_security\_groups”: “allinone”,  
“ansible\_ec2\_services\_domain”: “[amazonaws.com](http://amazonaws.com)”,  
“ansible\_ec2\_user\_data”: null  
},  
“changed”: false  
}

`  
I believe this module should get better documentation.  
Hope this helps you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [January 16, 2015, 5:51am UTC](https://forum.ansible.com/t/need-assist-with-syntax-for-ec2-assignment-of-elastic-ip-address/15025/6 "2015-01-16T05:51:50Z")

</div>

Dan is absolutely right, here is an example what he is saying. Thanks

- name: Associate new EIP to the NAT Instance  
local\_action:  
module: ec2\_eip  
region: “{{ vpc\_region }}”  
instance\_id: “{{ item.id }}”  
with\_items: ec2.results.instances  
register: eip

- name: Add the newly created EC2 instance(s) to the local host group  
local\_action: lineinfile  
dest=“./hosts”  
regexp=“{{ eip.results.public\_ip }}”  
insertafter=“[nat]”
