Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

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!

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Jul 19, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. 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:
    
  2. 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 }}"
    
  3. 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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans