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