AMI

This role does not need to make any choices, it simply takes our host from the ec2_instances group and creates an image of it. To start, let's create the role:

$ ansible-galaxy init roles/ami

The role is made up of three tasks, one of which is a pause. First of all, in roles/ami/tasks/main.yml, we need to find out some information about the instance. We are using the ec2_instance_facts module:

- name: find out some facts about the instance we have been using  ec2_instance_facts:    region: "{{ ec2_region }}"    filters:      dns-name: "{{ groups['ec2_instance'][0] }}"  register: "our_instance"

Now we know about the instance, we can create the AMI:

- name: create the AMI  ec2_ami:    region: "{{ ec2_region }}" instance_id: "{{ our_instance.instances.0.instance_id ...

Get Learn Ansible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.