- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Cloud

Working with AMIs Using Ansible
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!

Lab Info
Table of Contents
-
Challenge
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:
-
Challenge
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 }}"
-
Challenge
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:
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.