# register ec2 instance id and use it in ec2\_ami module

**URL:** <https://forum.ansible.com/t/register-ec2-instance-id-and-use-it-in-ec2-ami-module/17025>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [December 11, 2014, 2:36pm UTC](https://forum.ansible.com/t/register-ec2-instance-id-and-use-it-in-ec2-ami-module/17025 "2014-12-11T14:36:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navid\_Paya](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Navid\_Paya](https://forum.ansible.com/u/Navid_Paya)\
**Post date:** [December 11, 2014, 2:36pm UTC](https://forum.ansible.com/t/register-ec2-instance-id-and-use-it-in-ec2-ami-module/17025/1 "2014-12-11T14:36:35Z")

</div>

Hi all  
I’m trying to make some kind of automation to create an AMI out of an EC2 instance. So I have this chunk that creates the instance:

`

- name: Launch an Ubuntu 14.04 EC2 instance  
hosts: localhost  
connection: local  
gather\_facts: no  
tasks:
- name: Find the latest Ubuntu AMI  
ec2\_ami\_search: distro=ubuntu release=trusty region=ap-southeast-1 store=ebs-ssd virt=hvm  
register: ubuntu\_image
- name: Start the new EC2 instance  
ec2:  
image: “{{ ubuntu\_image.ami }}”  
region: ap-southeast-1  
zone: ap-southeast-1b  
instance\_type: t2.small  
vpc\_subnet\_id: xxxxxxxxxxxxxxxx  
group\_id: [‘sg-xxxxxxxx’, ‘sg-xxxxxxx’]  
key\_name: random\_key  
wait: yes  
wait\_timeout: 500  
register: ec2
- name: Add the new instance to host group  
add\_host: hostname={{ item.private\_ip }} groupname=launched  
with\_items: ec2.instances
- name: Wait for SSH to come up on the new instance  
wait\_for: host={{ item.private\_ip }} port=22 delay=60 timeout=320 state=started  
with\_items: ec2.instances

`

Then I try to use the instance and generate an AMI out of that:

`

- name: Create an AMI from the provisioned instance  
hosts: localhost  
connection: local  
gather\_facts: no  
tasks:

- ec2\_ami: region=ap-southeast-1 instance\_id={{ item.id }} wait=no name=basebox-2000000  
with\_items:

- ec2.instances

- name: Delete the instance now that the AMI is created  
hosts: localhost  
connection: local  
gather\_facts: no  
tasks:

- ec2:  
region: ap-southeast-1  
instance\_ids: “{{ item.id }}”  
state: ‘absent’  
with\_items:

- ec2.instances

`

But I get errors like this:

`

TASK: [ec2\_ami region=ap-southeast-1 instance\_id={{ item.id }} wait=no name=basebox-2000000] \*\*\*  
fatal: [localhost] =\> One or more undefined variables: ‘str object’ has no attribute ‘id’

`

I guess I’m missing something with iterating over a hash but can’t figure what. Any ideas?

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [December 11, 2014, 3:30pm UTC](https://forum.ansible.com/t/register-ec2-instance-id-and-use-it-in-ec2-ami-module/17025/2 "2014-12-11T15:30:10Z")

</div>

Hi Navid, what version of Ansible are you running? You might try using jinja2 braces around the ec2.instances variable in the with\_items list, as there was a bug recently about variables like that not expanding properly (which should be fixed in 1.8.2).

---

<div class="post-metadata">

**Author:** ![Navid\_Paya](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Navid\_Paya](https://forum.ansible.com/u/Navid_Paya)\
**Post date:** [December 12, 2014, 1:21am UTC](https://forum.ansible.com/t/register-ec2-instance-id-and-use-it-in-ec2-ami-module/17025/3 "2014-12-12T01:21:38Z")

</div>

Thanks, James. Upgrading Ansible from 1.8.1 to 1.8.2 and adding the braces totally did the job.

---

<div class="post-metadata">

**Author:** ![Stuart\_Davidson](https://avatars.discourse-cdn.com/v4/letter/s/4af34b/32.png) [@Stuart\_Davidson](https://forum.ansible.com/u/Stuart_Davidson)\
**Post date:** [March 31, 2015, 8:29am UTC](https://forum.ansible.com/t/register-ec2-instance-id-and-use-it-in-ec2-ami-module/17025/4 "2015-03-31T08:29:05Z")

</div>

Hey Navid, you couldn’t post an example could you?

I seem to be running into the same problem as you, but 1.8.2 nor the brackets are fixing the problem.

---

<div class="post-metadata">

**Author:** ![Navid\_Paya](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Navid\_Paya](https://forum.ansible.com/u/Navid_Paya)\
**Post date:** [March 31, 2015, 1:55pm UTC](https://forum.ansible.com/t/register-ec2-instance-id-and-use-it-in-ec2-ami-module/17025/5 "2015-03-31T13:55:45Z")

</div>

Hi Stuart  
This works fine for me with Ansible 1.8 onwards:

- name: Create an AMI from the provisioned instance  
ec2\_ami: region=ap-southeast-1 instance\_id={{ [item.id](http://item.id) }} name=baseami-{{ timestamp.stdout }} wait=yes wait\_timeout=600  
with\_items:
- “{{ ec2.instances }}”  
register: instance
- name: Delete the instance now that the AMI is created  
ec2:  
region: ap-southeast-1  
instance\_ids: “{{ ec2.instance\_ids }}”  
state: ‘absent’
