Lab Guide: Filtering Cloud Resources by Tags


1. Lab Briefing

This section provides an overview of the lab challenge and instructions for getting started.

1.1. Challenge Summary

In this challenge, you will use filters to control what information is retrieved from your cloud provider. One of the most common and powerful ways to filter resources is by using tags.

Tags are key-value pairs that act as metadata for organizing your cloud resources. Each tag has two parts:

  • A tag key (e.g., CostCenter, operating_system, owner).

  • A tag value (e.g., 1234, windows, nuno).

You can use tags to categorize resources by purpose, owner, environment, or any other criteria you define.

1.2. Getting Started

That is the end of your challenge briefing! Please click the green Start button in the bottom right corner of this window if the lab has not already started.

Start Button


2. Lab Guide: Hands-On Tasks

Estimated time to complete: 15 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: Create a Job Template with a Filter

Next, you will create a job template that runs a playbook to retrieve specific EC2 instances based on a tag filter.

  1. Navigate to the Templates page.

    In the left navigation menu, go to 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 INSTANCES by tag

    Job Type

    Run

    Inventory

    Demo Inventory

    Project

    AWS Demos Project

    Execution Environment

    AWS Execution Environment

    Playbook

    playbooks/lab3-challenge2.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.

This playbook uses a default filter to find all instances where the Name tag starts with rhel. The filter is defined as an extra_var in the playbook.

+

filter_input:
  "tag:Name": "rhel*"
The Ansible Playbooks for this lab are sourced from the following project: ansible-cloud/aws_demos.

2.3. Task 3: Launch the Job Template

Now, you will run the template to see the default filter in action.

  1. Launch the job template.

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

    Launch Job Icon

  2. Understand the playbook execution.

    This playbook runs three main tasks:

    • It uses the amazon.aws.ec2_instance_info module to find instances matching the filter_input.

      - name: Retrieve info for the EC2 instances
        amazon.aws.ec2_instance_info:
          region: "{{ ec2_region }}"
          filters: "{{ filter_input }}"
        register: ec2_instance_info
    • It uses the debug module and the length filter to count how many instances were found.

      - name: Display how many instances match the filter
        debug:
          msg:
            - "There are {{ ec2_instance_info.instances | length }} instances that match your filter"
    • It loops through the results and prints the Name, instance_id, and all tags 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 }}"
  3. Review the job output.

    The job output will show that two instances matched the default filter ("tag:Name": "rhel*").

    Job output showing two instances found

2.4. Task 4: Create a Survey to Control the Filter

To make the job template more flexible, you will add a survey that allows the user to specify a custom filter at launch time.

  1. Open the job template’s settings.

    Navigate back to the Templates page and click on the name of the Retrieve INSTANCES by tag job template.

    Job template details page

  2. Navigate to the Survey tab.

    At the top of the template’s details page, click the Survey tab.

  3. Create a new survey question.

    Click the blue Add button.

  4. Configure the survey question.

    Fill out the survey form with the following values:

    Parameter

    Value

    Question

    Provide an EC2 filter

    Description

    Provide the dictionary filter for AWS.

    Answer variable name

    filter_input

    Answer type

    Textarea

    Required

    Check the box ☑️

  5. Save the survey question.

    Scroll to the bottom and click Save.

  6. Enable the survey.

    You must enable the survey by clicking the toggle switch at the top of the survey page. The text will change from SURVEY OFF to SURVEY ON.

    Enable Survey Toggle

  7. Launch the template with a new filter.

    Click the Launch button. This time, a survey will prompt you for input. Enter the following text into the text area to find only the instance named rhel1:

    "tag:Name": "rhel1"

    Launch the job and observe that this time, only one instance is returned in the output.

Try launching the job again with some other tag combinations to see what is returned:
  • "tag:Index": "0"

  • "tag-key": "ansible-demo"

  • "tag:ansible-demo": "true"

  • "tag:instruqt": "true"


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 something not quite right, please link:https://github.com/ansible/instruqt/issues/new?title=Issue+with+Ansible+Hybrid+Cloud+Automation+-+Infrastructure+optimization&assignees=hichammourad[orset=utf