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.
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.
-
Navigate to the Automation Controller UI.
Click on the Automation Controller tab at the top of your lab window.
-
Log in with the provided credentials.
Username
adminPassword
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.
-
Navigate to the Templates page.
In the left navigation menu, go to Automation Execution → Templates.
-
Initiate the creation of a new job template.
Click the Create template button, then select Create job template.

-
Enter the job template details.
Fill out the form with the following information:
Parameter
Value
Name
Retrieve INSTANCES by tagJob Type
RunInventory
Demo InventoryProject
AWS Demos ProjectExecution Environment
AWS Execution EnvironmentPlaybook
playbooks/lab3-challenge2.ymlCredentials
AWS_CredentialTo find the AWS_Credential, you may need to filter the Credential Type toAmazon Web Services. -
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.
-
Launch the job template.
Navigate back to the Templates page, find the
Retrieve INSTANCES by tagjob template, and click the Launch icon (🚀).
-
Understand the playbook execution.
This playbook runs three main tasks:
-
It uses the
amazon.aws.ec2_instance_infomodule to find instances matching thefilter_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
debugmodule and thelengthfilter 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 alltagsfor 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 }}"
-
-
Review the job output.
The job output will show that two instances matched the default filter (
"tag:Name": "rhel*").
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.
-
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.

-
Navigate to the Survey tab.
At the top of the template’s details page, click the Survey tab.
-
Create a new survey question.
Click the blue Add button.
-
Configure the survey question.
Fill out the survey form with the following values:
Parameter
Value
Question
Provide an EC2 filterDescription
Provide the dictionary filter for AWS.Answer variable name
filter_inputAnswer type
TextareaRequired
Check the box ☑️
-
Save the survey question.
Scroll to the bottom and click Save.
-
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 OFFtoSURVEY ON.
-
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"
