# Ansible 2.0 module: ec2\_ami\_copy ec2\_ami

**URL:** <https://forum.ansible.com/t/ansible-2-0-module-ec2-ami-copy-ec2-ami/20858>\
**Category:** Ansible Project\
**Tags:** ubuntu, aws\
**Created:** [March 14, 2016, 2:51pm UTC](https://forum.ansible.com/t/ansible-2-0-module-ec2-ami-copy-ec2-ami/20858 "2016-03-14T14:51:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![estuble](https://avatars.discourse-cdn.com/v4/letter/e/58f4c7/32.png) [@estuble](https://forum.ansible.com/u/estuble)\
**Post date:** [March 14, 2016, 2:51pm UTC](https://forum.ansible.com/t/ansible-2-0-module-ec2-ami-copy-ec2-ami/20858/1 "2016-03-14T14:51:39Z")

</div>

Hey Team,

I am having a little trouble with some of the AMI Ansible Modules.  
Note: have only just started using Ansible 2.0+

It seems that when i use the ec2\_ami\_copy and ec2\_ami(destroy) it is doing it in pairs of 3.

- item=msg
- item=changed
- item=results

Just seeing if any of you have run into this issue and if anyone may know how to avoid making 3 copies of one image?

Thanks in advanced!  
– see code and output below

Code Block:

#(This Creates 1 AMI)

- name: Create an ami based off the ec2 instance  
ec2\_ami:  
instance\_id: “{{ item.id }}”  
region: “{{ ec2\_region }}”  
wait: yes  
name: ‘pre-{{ ami\_image\_type }}-{{ ec2\_tag\_Environment }}’  
tags:  
Name: ‘base {{ ami\_image\_type }}’  
Environment: ‘{{ ec2\_tag\_Environment }}’  
Region: ‘{{ ec2\_region }}’  
register: ami  
with\_items: ec2.instances  
changed\_when: ’ “AMI creation operation complete” not in ami.msg’  
failed\_when: ‘“AMI name already present” in ami.msg’

#(This takes the AMI i just crested and copies it to the east region, however, makes 3 copies of it)

- name: create/duplicate ami image in us-east-1  
ec2\_ami\_copy:  
source\_region: “{{ ec2\_region }}”  
region: ‘us-east-1’  
source\_image\_id: ‘{{ ami.results[0].image\_id }}’  
name: ‘ubuntu-{{ ec2\_tag\_Environment }}’  
tags:  
Name: ‘core {{ami\_image\_type}}’  
Type: “{{ ami\_image\_type }}”  
Environment: “{{ ec2\_tag\_Environment }}”  
wait: yes  
register: image\_east  
with\_items: ami

#(This removes the original ami, however, failes because it tries to remove 3 images)

- name: remove pre-ami image  
ec2\_ami:  
region: “{{ ec2\_region }}”  
image\_id: “{{ ami.results[0].image\_id }}”  
delete\_snapshot: True  
state: ‘absent’  
with\_items: ami

Debug and Errors:

**Create the AMI** \*

TASK [ami-configure : Create an ami based off the ec2 instance] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /opt/jenkins/olab-ansible/ec2-blank/roles/ami-configure/tasks/main.yml:9  
[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax (‘{{ec2.instances}}’).  
This feature will be removed in a future  
release. Deprecation warnings can be disabled by setting deprecation\_warnings=False in ansible.cfg.  
ESTABLISH LOCAL CONNECTION FOR USER: jenkins  
127.0.0.1 EXEC /bin/sh -c ‘mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1457909749.51-1977756954142`” && echo “`echo $HOME/.ansible/tmp/ansible-tmp-1457909749.51-1977756954142`”’  
127.0.0.1 PUT /tmp/tmpmUg\_cy TO /opt/jenkins/.ansible/tmp/ansible-tmp-1457909749.51-1977756954142/ec2\_ami  
127.0.0.1 EXEC /bin/sh -c ‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /opt/jenkins/.ansible/tmp/ansible-tmp-1457909749.51-1977756954142/ec2\_ami; rm -rf “/opt/jenkins/.ansible/tmp/ansible-tmp-1457909749.51-1977756954142/” \> /dev/null 2\>&1’  
ok: [localhost] =\> (item={u’kernel’: None, u’root\_device\_type’: u’ebs’, u’private\_dns\_name’: u’ip-10-3-0-242.us-west-1.compute.internal’, u’public\_ip’: None, u’private\_ip’: u’10.3.0.242’, u’id’: u’i-9139fb24’, u’ebs\_optimized’: False, u’state’: u’running’, u’virtualization\_type’: u’hvm’, u’architecture’: u’x86\_64’, u’ramdisk’: None, u’block\_device\_mapping’: {u’/dev/sda1’: {u’status’: u’attached’, u’delete\_on\_termination’: True, u’volume\_id’: u’vol-950e8628’}}, u’key\_name’: u’noc-staging’, u’image\_id’: u’ami-1d25557d’, u’tenancy’: u’default’, u’groups’: {u’sg-4d13d228’: u’default’}, u’public\_dns\_name’: u’‘, u’state\_code’: 16, u’tags’: {u’Environment’: u’ops-staging’, u’Type’: u’base’, u’Name’: u’ami-test-estuble-1’}, u’placement’: u’us-west-1b’, u’ami\_launch\_index’: u’0’, u’dns\_name’: u’‘, u’region’: u’us-west-1’, u’launch\_time’: u’2016-03-13T22:54:18.000Z’, u’instance\_type’: u’m3.medium’, u’root\_device\_name’: u’/dev/sda1’, u’hypervisor’: u’xen’}) =\> {“changed”: false, “failed”: false, “failed\_when\_result”: false, “image\_id”: “ami-92d6a5f2”, “invocation”: {“module\_args”: {“aws\_access\_key”: null, “aws\_secret\_key”: null, “delete\_snapshot”: null, “description”: “”, “device\_mapping”: null, “ec2\_url”: null, “image\_id”: null, “instance\_id”: “i-9139fb24”, “launch\_permissions”: null, “name”: “pre-ubuntu-ops-staging”, “no\_reboot”: false, “profile”: null, “region”: “us-west-1”, “security\_token”: null, “state”: “present”, “tags”: {“Environment”: “ops-staging”, “Name”: “base ubuntu”, “Region”: “us-west-1”}, “validate\_certs”: true, “wait”: true, “wait\_timeout”: “900”}, “module\_name”: “ec2\_ami”}, “item”: {“ami\_launch\_index”: “0”, “architecture”: “x86\_64”, “block\_device\_mapping”: {“/dev/sda1”: {“delete\_on\_termination”: true, “status”: “attached”, “volume\_id”: “vol-950e8628”}}, “dns\_name”: “”, “ebs\_optimized”: false, “groups”: {“sg-4d13d228”: “default”}, “hypervisor”: “xen”, “id”: “i-9139fb24”, “image\_id”: “ami-1d25557d”, “instance\_type”: “m3.medium”, “kernel”: null, “key\_name”: “noc-staging”, “launch\_time”: “2016-03-13T22:54:18.000Z”, “placement”: “us-west-1b”, “private\_dns\_name”: “ip-10-3-0-242.us-west-1.compute.internal”, “private\_ip”: “10.3.0.242”, “public\_dns\_name”: “”, “public\_ip”: null, “ramdisk”: null, “region”: “us-west-1”, “root\_device\_name”: “/dev/sda1”, “root\_device\_type”: “ebs”, “state”: “running”, “state\_code”: 16, “tags”: {“Environment”: “ops-staging”, “Name”: “ami-test-estuble-1”, “Type”: “base”}, “tenancy”: “default”, “virtualization\_type”: “hvm”}, “msg”: “AMI creation operation complete”, “state”: “available”}

_ **Copy AMI** _  
TASK [ami-configure : create/duplicate ami image in us-east-1] \*\*\*  
task path: /opt/jenkins/olab-ansible/ec2-blank/roles/ami-configure/tasks/main.yml:66  
[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax (‘{{ami}}’).  
This feature will be removed in a future release.  
Deprecation warnings can be disabled by setting deprecation\_warnings=False in ansible.cfg.  
ESTABLISH LOCAL CONNECTION FOR USER: jenkins  
127.0.0.1 EXEC /bin/sh -c ‘mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1457912054.53-185222114970372`” && echo “`echo $HOME/.ansible/tmp/ansible-tmp-1457912054.53-185222114970372`”’  
127.0.0.1 PUT /tmp/tmpraK1rL TO /opt/jenkins/.ansible/tmp/ansible-tmp-1457912054.53-185222114970372/ec2\_ami\_copy  
127.0.0.1 EXEC /bin/sh -c ‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /opt/jenkins/.ansible/tmp/ansible-tmp-1457912054.53-185222114970372/ec2\_ami\_copy; rm -rf “/opt/jenkins/.ansible/tmp/ansible-tmp-1457912054.53-185222114970372/” \> /dev/null 2\>&1’  
changed: [localhost] =\> (item=msg) =\> {“changed”: true, “image\_id”: “ami-4719f727”, “invocation”: {“module\_args”: {“aws\_access\_key”: null, “aws\_secret\_key”: null, “description”: “”, “ec2\_url”: null, “name”: “ubuntu-ops-staging”, “profile”: null, “region”: “us-west-2”, “security\_token”: null, “source\_image\_id”: “ami-92d6a5f2”, “source\_region”: “us-west-1”, “tags”: {“Environment”: “ops-staging”, “Name”: “Chef Ubuntu”, “Type”: “ubuntu”}, “validate\_certs”: true, “wait”: true, “wait\_timeout”: “1200”}, “module\_name”: “ec2\_ami\_copy”}, “item”: “msg”, “msg”: “AMI copy operation complete”, “state”: “available”}  
127.0.0.1 EXEC /bin/sh -c ‘mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1457912370.04-93363397180930`” && echo “`echo $HOME/.ansible/tmp/ansible-tmp-1457912370.04-93363397180930`”’  
127.0.0.1 PUT /tmp/tmpVpxnYX TO /opt/jenkins/.ansible/tmp/ansible-tmp-1457912370.04-93363397180930/ec2\_ami\_copy  
127.0.0.1 EXEC /bin/sh -c ‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /opt/jenkins/.ansible/tmp/ansible-tmp-1457912370.04-93363397180930/ec2\_ami\_copy; rm -rf “/opt/jenkins/.ansible/tmp/ansible-tmp-1457912370.04-93363397180930/” \> /dev/null 2\>&1’  
changed: [localhost] =\> (item=changed) =\> {“changed”: true, “image\_id”: “ami-1c19f77c”, “invocation”: {“module\_args”: {“aws\_access\_key”: null, “aws\_secret\_key”: null, “description”: “”, “ec2\_url”: null, “name”: “ubuntu-ops-staging”, “profile”: null, “region”: “us-west-2”, “security\_token”: null, “source\_image\_id”: “ami-92d6a5f2”, “source\_region”: “us-west-1”, “tags”: {“Environment”: “ops-staging”, “Name”: “Chef Ubuntu”, “Type”: “ubuntu”}, “validate\_certs”: true, “wait”: true, “wait\_timeout”: “1200”}, “module\_name”: “ec2\_ami\_copy”}, “item”: “changed”, “msg”: “AMI copy operation complete”, “state”: “available”}  
127.0.0.1 EXEC /bin/sh -c ‘mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1457912697.05-9517430380691`” && echo “`echo $HOME/.ansible/tmp/ansible-tmp-1457912697.05-9517430380691`”’  
127.0.0.1 PUT /tmp/tmphKP3iB TO /opt/jenkins/.ansible/tmp/ansible-tmp-1457912697.05-9517430380691/ec2\_ami\_copy  
127.0.0.1 EXEC /bin/sh -c ‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /opt/jenkins/.ansible/tmp/ansible-tmp-1457912697.05-9517430380691/ec2\_ami\_copy; rm -rf “/opt/jenkins/.ansible/tmp/ansible-tmp-1457912697.05-9517430380691/” \> /dev/null 2\>&1’  
changed: [localhost] =\> (item=results) =\> {“changed”: true, “image\_id”: “ami-841ff1e4”, “invocation”: {“module\_args”: {“aws\_access\_key”: null, “aws\_secret\_key”: null, “description”: “”, “ec2\_url”: null, “name”: “ubuntu-ops-staging”, “profile”: null, “region”: “us-west-2”, “security\_token”: null, “source\_image\_id”: “ami-92d6a5f2”, “source\_region”: “us-west-1”, “tags”: {“Environment”: “ops-staging”, “Name”: “Chef Ubuntu”, “Type”: “ubuntu”}, “validate\_certs”: true, “wait”: true, “wait\_timeout”: “1200”}, “module\_name”: “ec2\_ami\_copy”}, “item”: “results”, “msg”: “AMI copy operation complete”, “state”: “available”}

_ **REMOVE AMI** _

TASK [ami-cleanup : remove pre-ami image] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /opt/jenkins/olab-ansible/ec2-blank/roles/ami-cleanup/tasks/main.yml:9  
[DEPRECATION WARNING]: Using bare variables is deprecated. Update your playbooks so that the environment value uses the full variable syntax (‘{{ami}}’).  
This feature will be removed in a future release.  
Deprecation warnings can be disabled by setting deprecation\_warnings=False in ansible.cfg.  
ESTABLISH LOCAL CONNECTION FOR USER: jenkins  
127.0.0.1 EXEC /bin/sh -c ‘mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1457913034.75-161819032042794`” && echo “`echo $HOME/.ansible/tmp/ansible-tmp-1457913034.75-161819032042794`”’  
127.0.0.1 PUT /tmp/tmpFmOJaM TO /opt/jenkins/.ansible/tmp/ansible-tmp-1457913034.75-161819032042794/ec2\_ami  
127.0.0.1 EXEC /bin/sh -c ‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /opt/jenkins/.ansible/tmp/ansible-tmp-1457913034.75-161819032042794/ec2\_ami; rm -rf “/opt/jenkins/.ansible/tmp/ansible-tmp-1457913034.75-161819032042794/” \> /dev/null 2\>&1’  
changed: [localhost] =\> (item=msg) =\> {“changed”: true, “invocation”: {“module\_args”: {“aws\_access\_key”: null, “aws\_secret\_key”: null, “delete\_snapshot”: “True”, “description”: “”, “device\_mapping”: null, “ec2\_url”: null, “image\_id”: “ami-92d6a5f2”, “instance\_id”: null, “launch\_permissions”: null, “name”: null, “no\_reboot”: false, “profile”: null, “region”: “us-west-1”, “security\_token”: null, “state”: “absent”, “tags”: null, “validate\_certs”: true, “wait”: false, “wait\_timeout”: “900”}, “module\_name”: “ec2\_ami”}, “item”: “msg”, “msg”: “AMI deregister/delete operation complete”}  
127.0.0.1 EXEC /bin/sh -c ‘mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1457913040.93-45535154371361`” && echo “`echo $HOME/.ansible/tmp/ansible-tmp-1457913040.93-45535154371361`”’  
127.0.0.1 PUT /tmp/tmpbrWAUv TO /opt/jenkins/.ansible/tmp/ansible-tmp-1457913040.93-45535154371361/ec2\_ami  
127.0.0.1 EXEC /bin/sh -c ‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /opt/jenkins/.ansible/tmp/ansible-tmp-1457913040.93-45535154371361/ec2\_ami; rm -rf “/opt/jenkins/.ansible/tmp/ansible-tmp-1457913040.93-45535154371361/” \> /dev/null 2\>&1’  
127.0.0.1 EXEC /bin/sh -c ‘mkdir -p “`echo $HOME/.ansible/tmp/ansible-tmp-1457913046.65-256419645847339`” && echo “`echo $HOME/.ansible/tmp/ansible-tmp-1457913046.65-256419645847339`”’  
failed: [localhost] (item=changed) =\> {“failed”: true, “invocation”: {“module\_args”: {“aws\_access\_key”: null, “aws\_secret\_key”: null, “delete\_snapshot”: “True”, “description”: “”, “device\_mapping”: null, “ec2\_url”: null, “image\_id”: “ami-92d6a5f2”, “instance\_id”: null, “launch\_permissions”: null, “name”: null, “no\_reboot”: false, “profile”: null, “region”: “us-west-1”, “security\_token”: null, “state”: “absent”, “tags”: null, “validate\_certs”: true, “wait”: false, “wait\_timeout”: “900”}, “module\_name”: “ec2\_ami”}, “item”: “changed”, “msg”: “InvalidAMIID.Unavailable: The image ID ‘ami-92d6a5f2’ is no longer available”}  
127.0.0.1 PUT /tmp/tmpxT1iqz TO /opt/jenkins/.ansible/tmp/ansible-tmp-1457913046.65-256419645847339/ec2\_ami  
127.0.0.1 EXEC /bin/sh -c ‘LANG=en\_US.UTF-8 LC\_ALL=en\_US.UTF-8 LC\_MESSAGES=en\_US.UTF-8 /usr/bin/python /opt/jenkins/.ansible/tmp/ansible-tmp-1457913046.65-256419645847339/ec2\_ami; rm -rf “/opt/jenkins/.ansible/tmp/ansible-tmp-1457913046.65-256419645847339/” \> /dev/null 2\>&1’  
failed: [localhost] (item=results) =\> {“failed”: true, “invocation”: {“module\_args”: {“aws\_access\_key”: null, “aws\_secret\_key”: null, “delete\_snapshot”: “True”, “description”: “”, “device\_mapping”: null, “ec2\_url”: null, “image\_id”: “ami-92d6a5f2”, “instance\_id”: null, “launch\_permissions”: null, “name”: null, “no\_reboot”: false, “profile”: null, “region”: “us-west-1”, “security\_token”: null, “state”: “absent”, “tags”: null, “validate\_certs”: true, “wait”: false, “wait\_timeout”: “900”}, “module\_name”: “ec2\_ami”}, “item”: “results”, “msg”: “InvalidAMIID.Unavailable: The image ID ‘ami-92d6a5f2’ is no longer available”}

Note: I have gotten around this by using the aws-cli instead but would prefer to stick with the Ansible modules when possible
