# Use id of AMI I have created as a variable in later tasks

**URL:** <https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190>\
**Category:** Ansible Project\
**Tags:** ubuntu, aws\
**Created:** [December 4, 2018, 12:04pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190 "2018-12-04T12:04:52Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![shane.do](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@shane.do](https://forum.ansible.com/u/shane.do)\
**Post date:** [December 4, 2018, 12:04pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/1 "2018-12-04T12:04:52Z")

</div>

I am trying to create a playbook which does the following:

- creates an image of an AWS instance
- copies the image to a different region
- installs updates to the instance

I can do all of this apart from getting the AMI to copy to a different region. I’ve tried a few things but this is my current playbook (this is only for the AMI creation and copying, installing updates is handled separately).

`

- hosts: webservers  
remote\_user: ubuntu  
tasks:

- name: create an ami  
ec2\_ami:  
region: eu-west-2  
instance\_id: i-0771a2e4289c057e9  
name: “{{ inventory\_hostname }}-{{ ansible\_date\_time.iso8601\_basic\_short }}”  
tags:  
Name: “{{ inventory\_hostname }}-{{ ansible\_date\_time.iso8601\_basic\_short }}”

- name: set ID name  
set\_fact:  
new\_ami\_name: ec2\_ami.image\_id

- pause:  
seconds: 60

- name: copy AMI to a different region  
ec2\_ami\_copy:  
source\_region: eu-west-2  
source\_image\_id: “{{ new\_ami\_name }}”  
name: “{{ new\_ami\_name }}”  
region: eu-west-1  
`

For which the creation, setting fact, and pause work, but on the last part I get this error output:

`  
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ClientError: An error occurred (InvalidAMIID.Malformed) when calling the CopyImage operation: Invalid id: “ec2\_ami.image\_id” (expecting “ami-…”)

`

I’ve tried a few different things I’m fairly new to Ansible, but basically I can’t figure out how to pass the image\_id of the newly created AMI to the next play so that it can use this as the basis of the copying.

I can see clearly that it’s not registering the image\_id as it’s saying it’s not in the expected “ami-” format, but I’m stuck from there.

---

<div class="post-metadata">

**Author:** ![shane.do](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@shane.do](https://forum.ansible.com/u/shane.do)\
**Post date:** [December 4, 2018, 3:14pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/2 "2018-12-04T15:14:46Z")

</div>

After more searching I also tried this:

`

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 4, 2018, 3:26pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/3 "2018-12-04T15:26:17Z")

</div>

You need to wrap wrap the variable up in curly braces so that it is interpreted rather than taken literally.

I.e., where you have:  
`new_ami_id: ami.results[0].image_id`

you need:  
`new_ami_id: "{{ ami.results[0].image_id }}"`

Regards, K.

---

<div class="post-metadata">

**Author:** ![shane.do](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@shane.do](https://forum.ansible.com/u/shane.do)\
**Post date:** [December 4, 2018, 3:37pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/4 "2018-12-04T15:37:10Z")

</div>

I had actually tried that already, and when I do I get this error:

“msg”: "The task includes an option with an undefined variable. The error was: ‘new\_ami\_id’ is undefined\

Which is confusing because it seems to me it clearly is being defined.

---

<div class="post-metadata">

**Author:** ![shane.do](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@shane.do](https://forum.ansible.com/u/shane.do)\
**Post date:** [December 4, 2018, 3:39pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/5 "2018-12-04T15:39:22Z")

</div>

I accidentally replied to the email - haven’t used Google Groups a lot, sorry about that.

I had actually tried this already. When I do I get this error:

`  
“msg”: "The task includes an option with an undefined variable. The error was: ‘new\_ami\_id’ is undefined\

`

Which is confusing because it seems to me it clearly is being defined.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 4, 2018, 4:02pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/6 "2018-12-04T16:02:59Z")

</div>

On what line was that error thrown?

I think you should provide the entire playbook and the complete output.

Regards, K.

---

<div class="post-metadata">

**Author:** ![shane.do](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@shane.do](https://forum.ansible.com/u/shane.do)\
**Post date:** [December 4, 2018, 4:17pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/7 "2018-12-04T16:17:37Z")

</div>

This is the playbook:

`

- hosts: webservers  
remote\_user: ubuntu  
tasks:

- name: create an ami  
ec2\_ami:  
region: eu-west-2  
instance\_id: i-077xxxxxxxxxxx  
name: “{{ inventory\_hostname }}-{{ ansible\_date\_time.iso8601\_basic\_short }}”  
tags:  
Name: “{{ inventory\_hostname }}-{{ ansible\_date\_time.iso8601\_basic\_short }}”  
register: ami

- name: set ami id  
set\_fact:  
new\_ami\_id: “{{ ami.results[0].image\_id }}”

- pause:  
seconds: 60

- name: copy AMI to a different region  
ec2\_ami\_copy:  
source\_region: eu-west-2  
source\_image\_id: “{{ new\_ami\_id }}”  
name: “{{ new\_ami\_id }}”  
region: eu-west-1  
`

And the full output:

`  
ansible-playbook 2.6.4  
config file = /etc/ansible/ansible.cfg  
configured module search path = [u’/home/ubuntu/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/dist-packages/ansible  
executable location = /usr/bin/ansible-playbook  
python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 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*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /opt/ansible-scripts/update/create-ami.new.yml:2  
ok: [[server.mydomain.com](http://server.mydomain.com)]  
META: ran handlers

TASK [create an ami] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /opt/ansible-scripts/update/create-ami.new.yml:5  
changed: [[server.mydomain.com](http://server.mydomain.com)] =\> {“architecture”: “x86\_64”, “block\_device\_mapping”: {}, “changed”: true, “creationDate”: “2018-12-04T16:14:50.000Z”, “description”: “”, “enhanced\_networking”: true, “hypervisor”: “xen”, “image\_id”: “ami-02867e4b6aec13ee5”, “image\_owner\_alias”: null, “image\_type”: “machine”, “is\_public”: false, “kernel\_id”: null, “launch\_permissions”: , “location”: “541818391026/server.mydomain.com-20181204T161449”, “msg”: “AMI creation operation complete.”, “name”: “server.mydomain.com-20181204T161449”, “ownerId”: “541818391026”, “platform”: null, “product\_codes”: , “ramdisk\_id”: null, “root\_device\_name”: null, “root\_device\_type”: “ebs”, “sriov\_net\_support”: null, “state”: “pending”, “state\_reason”: null, “tags”: {“Name”: “server.mydomain.com-20181204T161449”}, “virtualization\_type”: “hvm”}

TASK [set ami id] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /opt/ansible-scripts/update/create-ami.new.yml:14  
ok: [[server.mydomain.com](http://server.mydomain.com)] =\> {“ansible\_facts”: {}, “changed”: false}

TASK [pause] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /opt/ansible-scripts/update/create-ami.new.yml:18  
Pausing for 60 seconds  
(ctrl+C then ‘C’ = continue early, ctrl+C then ‘A’ = abort)  
ok: [[server.mydomain.com](http://server.mydomain.com)] =\> {“changed”: false, “delta”: 60, “echo”: true, “rc”: 0, “start”: “2018-12-04 16:14:51.149347”, “stderr”: “”, “stdout”: “Paused for 60.0 seconds”, “stop”: “2018-12-04 16:15:51.149596”, “user\_input”: “”}

TASK [copy AMI to a different region] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /opt/ansible-scripts/update/create-ami.new.yml:21  
fatal: [[server.mydomain.com](http://server.mydomain.com)]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘new\_ami\_id’ is undefined\n\nThe error appears to have been in ‘/opt/ansible-scripts/update/create-ami.new.yml’: line 21, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: copy AMI to a different region\n ^ here\n”}  
to retry, use: --limit @/opt/ansible-scripts/update/create-ami.new.retry

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[server.mydomain.com](http://server.mydomain.com) : ok=4 changed=1 unreachable=0 failed=1  
`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 4, 2018, 4:27pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/8 "2018-12-04T16:27:48Z")

</div>

This WARNING is the hint, you indentation of new\_ami\_id in set\_fact is missing two spaces.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 4, 2018, 4:29pm UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/9 "2018-12-04T16:29:30Z")

</div>

The line where you set new\_ami\_id, immediately below set\_fact:, does not appear to be indented correctly. Indent it a few spaces and try again.

Regards, K.

---

<div class="post-metadata">

**Author:** ![shane.do](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@shane.do](https://forum.ansible.com/u/shane.do)\
**Post date:** [December 5, 2018, 8:23am UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/10 "2018-12-05T08:23:35Z")

</div>

Ah yes, apologies for that. So I’ve corrected that, but getting an undefined variable error.

`fatal: [server.mydomain.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'results'\n\nThe error appears to have been in '/opt/ansible-scripts/update/create-ami.new.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: set ami id\n ^ here\n"}`

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [December 5, 2018, 10:59am UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/11 "2018-12-05T10:59:51Z")

</div>

Read the documentation for ec2\_ami:

[https://docs.ansible.com/ansible/latest/modules/ec2\_ami\_module.html#return-values](https://docs.ansible.com/ansible/latest/modules/ec2_ami_module.html#return-values)

… or the doco for the particular version of Ansible that you are using.

The section “Return values” describes the structure inside your registered variable “ami”.

It looks to me as if to retrieve the AMI ID, you need to use “{{ ami.image\_id }}”.

`'dict object' has no attribute 'result' means "there is no thing called 'result' inside the variable you are looking at".`

`That means you either have the wrong variable (and 'result' is to be found in a different variable) or you have the correct variable, but there is nothing called "result" inside it.`

Regards, K.

---

<div class="post-metadata">

**Author:** ![shane.do](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@shane.do](https://forum.ansible.com/u/shane.do)\
**Post date:** [December 5, 2018, 11:52am UTC](https://forum.ansible.com/t/use-id-of-ami-i-have-created-as-a-variable-in-later-tasks/29190/12 "2018-12-05T11:52:36Z")

</div>

You were right - I had to change that line to:

`  
new\_ami\_id: “{{ ami.image\_id }}”

`

Thanks for the help. I’ve given the documentation a good read, and will keep doing so, but these things can still be hard to figure out.
