Lab Guide: Cloud Optimization with Ansible

A guide to understanding basic cloud optimization exercises, starting with dynamic inventory and resource tagging.


1. Lab Briefing

This section provides an overview of the lab environment and the importance of resource tagging in the cloud.

1.1. Lab Instructions

Please click the green Start button on the bottom right corner to create the lab environment if it has not already started. To navigate through this briefing, click the white > arrow on the right.

Start Button

1.2. Lab Diagram

The lab topology consists of one Ansible Automation Platform controller node and two Red Hat Enterprise Linux (RHEL) nodes running in Amazon Web Services (AWS) Elastic Compute Cloud (EC2).

Lab topology diagram

1.3. The Importance of Cloud Tags

Cloud environments are composed of numerous resources, including instances, containers, networks, and more. To effectively manage these resources, it is crucial to use tags. Tags are simple key-value pairs that help you organize, filter, and track your assets.

Various AWS cloud resources

For example, the tags on this EC2 instance clearly define its purpose, owner, and provisioning details, which is invaluable for cost management, security audits, and operational clarity.

Example of tags on an AWS EC2 instance

This concludes your lab briefing.


2. Lab Guide: Hands-On Tasks

Welcome to the Ansible Hybrid Cloud Automation - Cloud Optimization lab. In this first challenge, you will learn about the pre-configured Dynamic Inventory for this lab and see how it uses tags to identify resources.

Estimated time to complete: 10 minutes

2.1. Task 1: Log into Ansible Automation Platform

First, you will log in to the automation controller to begin the lab exercises.

  1. Navigate to the Automation Controller UI.

    Click on the Automation Controller tab at the top of your lab window.

  2. Log in with the provided credentials.

    Username

    admin

    Password

    ansible123!

After logging in, you will land on the main dashboard.

2.2. Task 2: Examine the Dynamic Inventory

In this task, you will explore the pre-configured dynamic inventory and inspect the host variables and tags that are automatically discovered from AWS.

  1. Navigate to the Inventories menu.

    In the left navigation menu, go to Automation ExecutionInfrastructureInventories.

  2. Select the AWS Inventory.

    Click on the inventory named AWS Inventory.

    AWS Inventory details page

  3. View the hosts.

    Click on the Hosts tab. You will see two hosts listed, identified by their Name tag from AWS EC2.

  4. Inspect a host’s variables.

    Click on one of the hosts to open its details page.

  5. Examine the tags.

    On the host’s Details tab, scroll down to the VARIABLES section. Note the tags section, which contains key-value pairs discovered directly from the EC2 instance. We will use these tags in a later step.

    EC2 instance tags in automation controller

To see these same tags in the AWS console, you can open the AWS console tab in your lab environment, log in, and navigate to the EC2 service to inspect the instances.

2.3. Task 3: Create a Job Template

Next, you will create a job template to run a playbook that retrieves and displays tag information from your AWS resources.

  1. Navigate to the Templates page.

    From the left navigation menu, select Automation ExecutionTemplates.

  2. Initiate the creation of a new job template.

    Click the Create template button, then select Create job template.

    Create a new job template

  3. Enter the job template details.

    Fill out the form with the following information:

    Parameter

    Value

    Name

    Retrieve tag information

    Job Type

    Run

    Inventory

    Demo Inventory

    Project

    AWS Demos Project

    Execution Environment

    AWS Execution Environment

    Playbook

    playbooks/tag_info_aws.yml

    Credentials

    AWS_Credential

    To find the AWS_Credential, you may need to filter the Credential Type to Amazon Web Services.
  4. Save the job template.

    Scroll to the bottom and click the blue Save button.

The Ansible Playbooks for this lab are sourced from the following project: ansible-cloud/aws_demos.

2.4. Task 4: Launch the Job Template and Review Output

Finally, you will run the job template and examine the structured data it collects.

  1. Launch the job template.

    Navigate back to the Templates page, find the Retrieve tag information job template, and click the Launch icon (🚀).

    Launch Job Icon

  2. Understand the playbook execution.

    This playbook runs four tasks to gather and display information:

    • It uses the amazon.aws.ec2_vpc_net_info module to get data for all VPCs.

    • It uses the debug module to print the VPC ID, name, and tags.

    • It uses the amazon.aws.ec2_instance_info module to get data for all EC2 instances.

    • It uses another debug task to print the EC2 instance name, ID, and tags.

  3. Review the job output.

    The final task in the playbook uses a loop to display the information for each instance.

    - name: Display AWS EC2 info and tags information to terminal
      debug:
        msg:
          - "{{ item.tags['Name'] | default('The tag *Name* Does not exist') }}"
          - "{{ item.instance_id }}"
          - "{{ item.tags }}"
      loop: "{{ ec2_instance_info.instances }}"

    The output in the automation controller will show this structured data clearly for each resource.

    JSON output from job run


3. Next Steps

You have successfully completed this lab. Press the Check button in your lab environment to proceed to the next challenge.

4. Troubleshooting

If you have encountered an issue or have noticed somethi�?