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 7.3.0]
Using /etc/ansible/ansible.cfg as config file
[WARNING]: Ignoring invalid attribute: new_ami_id
PLAYBOOK: create-ami.new.yml **************************************************************************************************************************************************************************************
1 plays in create-ami.new.yml
PLAY [webservers] *************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
task path: /opt/ansible-scripts/update/create-ami.new.yml:2
ok: [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] => {“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] => {“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] => {“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]: 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 : ok=4 changed=1 unreachable=0 failed=1
`