# Getting the ID of an AMI created with Ansible

**URL:** <https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185>\
**Category:** Ansible Project\
**Created:** [September 11, 2014, 7:00pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185 "2014-09-11T19:00:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Palmieri](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Peter\_Palmieri](https://forum.ansible.com/u/Peter_Palmieri)\
**Post date:** [September 11, 2014, 7:00pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/1 "2014-09-11T19:00:51Z")

</div>

I’m building an EC2 instance with Ansible, then creating an AMI from the instance. I’m sure I’m missing something here, but how do I get the ID of the newly created AMI? I’ve tried:

```auto
tasks:
- name: create an ami in us-east-1
  ec2_ami: wait=yes
           aws_access_key={{ ec2_access_key }}
           aws_secret_key={{ ec2_secret_key }}
           instance_id={{ item }}
           region={{ region1 }}
           name=data-mgmt-qa-006
  with_items: hostvars[inventory_hostname]['ansible_ec2_instance_id']
  register: ec2_ami_info

- debug: var=item
  with_items: ec2_ami_info.image_id 

```

and:

```auto
tasks:
- name: create an ami in us-east-1
  ec2_ami: wait=yes
           aws_access_key={{ ec2_access_key }}
           aws_secret_key={{ ec2_secret_key }}
           instance_id={{ item }}
           region={{ region1 }}
           name=data-mgmt-qa-006
  with_items: hostvars[inventory_hostname]['ansible_ec2_instance_id']
  register: instance

- debug: var=item
  with_items: instance.image_id

```

The latter ‘register’ is copied from the docs, but I’m not able to get the right with\_items obviously.

The AMI is being created fine. Any suggestions would be much appreciated.

---

<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:** [September 12, 2014, 1:52am UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/2 "2014-09-12T01:52:48Z")

</div>

You should be able to just do:

- debug: var=instance.image\_id

Which should print out the image’s ID. If not, could you share what it does show?

---

<div class="post-metadata">

**Author:** ![Peter\_Palmieri](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Peter\_Palmieri](https://forum.ansible.com/u/Peter_Palmieri)\
**Post date:** [September 12, 2014, 6:43pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/3 "2014-09-12T18:43:30Z")

</div>

Thanks for helping, it’s much appreciated.

I now have:

tasks:

- name: create an ami in us-east-1  
ec2\_ami: wait=yes  
aws\_access\_key={{ ec2\_access\_key }}  
aws\_secret\_key={{ ec2\_secret\_key }}  
instance\_id={{ item }}  
region={{ region1 }}  
name=some-name  
with\_items: hostvars[inventory\_hostname][‘ansible\_ec2\_instance\_id’]  
register: instance

- debug: var=instance.image\_id

Which returns:

TASK: [create an ami in us-east-1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [54.198.217.217] =\> (item=i-7c77c197)

TASK: [debug var=instance.image\_id] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [54.198.217.217] =\> {  
“instance.image\_id”: “{{ instance.image\_id }}”  
}

instead of the actual image id.

---

<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:** [September 12, 2014, 7:22pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/4 "2014-09-12T19:22:40Z")

</div>

Can you please share the output of:

- debug: var=instance

Thanks!

---

<div class="post-metadata">

**Author:** ![Peter\_Palmieri](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Peter\_Palmieri](https://forum.ansible.com/u/Peter_Palmieri)\
**Post date:** [September 12, 2014, 7:54pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/5 "2014-09-12T19:54:54Z")

</div>

That shows:

TASK: [debug var=instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [54.90.128.104] =\> {  
“instance”: {  
“changed”: true,  
“msg”: “All items completed”,  
“results”: [  
{  
“changed”: true,  
“image\_id”: “ami-be14b9d6”,  
“invocation”: {  
“module\_args”: “wait=yes aws\_access\_key=\*\*\*\* aws\_secret\_key=\*\*\*\* instance\_id=i-393284d2 region=us-east-1 name=blah”,  
“module\_name”: “ec2\_ami”  
},  
“item”: “i-393284d2”,  
“msg”: “AMI creation operation complete”,  
“state”: “available”  
}  
]  
}  
}

---

<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:** [September 12, 2014, 8:04pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/6 "2014-09-12T20:04:49Z")

</div>

In that case, looks like you want:

instance.results[0].image\_id

---

<div class="post-metadata">

**Author:** ![Peter\_Palmieri](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Peter\_Palmieri](https://forum.ansible.com/u/Peter_Palmieri)\
**Post date:** [September 12, 2014, 8:21pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/7 "2014-09-12T20:21:41Z")

</div>

Thank you very much, that did it.

---

<div class="post-metadata">

**Author:** ![Peter\_Palmieri](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Peter\_Palmieri](https://forum.ansible.com/u/Peter_Palmieri)\
**Post date:** [September 12, 2014, 9:36pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/8 "2014-09-12T21:36:20Z")

</div>

How can I reference this in an ini file module? I’m trying to write the value of the ami id to a file, to be consumed by another process. This didn’t work:

- ini\_file: dest=ami.ini section=qa option=ami value={{ var=instance.results[0].image\_id }}

I got:

TASK: [ini\_file dest=ami.ini section=qa option=ami value={{ var=instance.results[0].image\_id }}] \*\*\*  
failed: [54.166.40.120] =\> {“failed”: true}  
msg: this module requires key=value arguments ([‘dest=ami.ini’, ‘section=qa’, ‘option=ami’, ‘value={#’, ‘var=instance.results[0].image\_id’, ‘#}’])

---

<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:** [September 12, 2014, 9:52pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/9 "2014-09-12T21:52:30Z")

</div>

Take out the var=

{{ instance.results[0].image\_id }}

---

<div class="post-metadata">

**Author:** ![Peter\_Palmieri](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Peter\_Palmieri](https://forum.ansible.com/u/Peter_Palmieri)\
**Post date:** [September 12, 2014, 10:10pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/10 "2014-09-12T22:10:56Z")

</div>

Sorry, I should have got that one on my own. Thank you again for your help.

---

<div class="post-metadata">

**Author:** ![Peter\_Palmieri](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Peter\_Palmieri](https://forum.ansible.com/u/Peter_Palmieri)\
**Post date:** [September 12, 2014, 10:13pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/11 "2014-09-12T22:13:05Z")

</div>

Meant to add, Ansible is a huge boon to our infrastructure efforts. Thanks so much to the Ansible team for creating this tool.

---

<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:** [September 12, 2014, 11:45pm UTC](https://forum.ansible.com/t/getting-the-id-of-an-ami-created-with-ansible/16185/12 "2014-09-12T23:45:37Z")

</div>

Saying this collectively for hundreds of people here who have helped with things, and the rest of the team - you are welcome!!!
