Image creation in AWS provides for both simplified system management and improved deployment performance. Ansible can be leveraged to automate AMI upkeep, and we will be doing just that in this exercise!
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create `/home/ansible/updateAMI.yml`, and Add an Ansible Play that Updates the Software on `node1` Then Updates `/home/ansible/image.txt`, as Described in the Instructions
In our solution, we’ll complete the required steps and add a task for collecting facts from EC2 metadata, which will help us collect the instance ID of
node1
.Edit the playbook such that it resembles the following:
- hosts: node1 become: yes tasks: - name: Update packages yum: name: "*" state: latest - name: Update image.txt lineinfile: path: /home/ansible/image.txt line: "Image updated {{ ansible_date_time.date }}" - name: Gather facts ec2_metadata_facts:
- Edit the `/home/ansible/updateAMI.yml` and Add an Ansible Play that Collects the instance_id of `node1`, stops `node1`, Creates the AMI as Described in the Instructions, and Stores the AMI ID in the File `/home/ansible/ami.txt`
We will be using the facts we collected in the first play to satisfy the objectives for this task.
Edit the playbook such that it resembles the following:
- hosts: localhost tasks: - name: Stop node1 local_action: ec2 args: region: us-east-1 state: stopped instance_id: "{{ hostvars['node1'].ansible_ec2_instance_id }}" wait: yes - name: Create AMI local_action: ec2_ami args: state: present instance_id: "{{ hostvars['node1'].ansible_ec2_instance_id }}" name: UpdatedImage register: ami_output - name: Write AMI info to file lineinfile: create: yes path: /home/ansible/ami.txt line: "{{ ami_output.image_id }}"
- Run `/home/ansible/updateAMI.yml` to Perform the Required Tasks and Then Log into the AWS Console to Verify Your Work
- Change some environment variables:
source keys.sh
- Run the playbook:
ansible-playbook /home/ansible/updateAMI.yml
- Log into the AWS Console and confirm the new EC2 instance:
- Search for "EC2" in the AWS console search and select the EC2 dashboard.
- Confirm the AMI was updated:
- Select AMIs from the menu on the left.
- Change some environment variables: