Lab Guide: Filtering Azure Instances by Tags
A guide to filtering cloud resources in Microsoft Azure by purpose, owner, environment, or other criteria using 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.
This concludes your lab briefing. Please move forward. ---
2. Lab Guide: Hands-On Tasks
Estimated time to complete: 10 minutes
2.1. Task 1: Log into Ansible Automation Platform
Click on the Ansible Automation Platform 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 virtual machines based on a tag filter.
-
Navigate to the Templates page.
Expand the
Automation Executionmenu on the left.Automation Execution→Templates. -
Initiate the creation of a new job template.
Click the
+ Create Templatethen scroll down and clickCreate job template -
Enter the job template details.
Fill out the form with the following information:
Parameter
Value
Name
Retrieve instances by tagJob Type
RunInventory
Azure InventoryProject
Cloud Visibility ProjectPlaybook
playbooks/retrieve_vms_detail.ymlExecution Environment
Microsoft Azure Execution EnvironmentCredentials
azure_credentialTo select the azure_credential, click in the text field or on the drop down icon, then selectMicrosoft Azure Resource Manager. -
Save the job template.
Scroll to the bottom, click
Create job template
This particular filter (by default) looks like this:
tags: "{{ your_tags | default('demo:true') }}"
2.3. Task 3: Launch the Job Template
Next, you will launch the job template Retrieve instances by tag and review the output.
-
Launch the job template.
Select
🚀 Launch template. -
Review the output.
The output in the Ansible Automation Platform will show the details of the virtual machines.
-
Understand the playbook execution.
This playbook has three tasks:
-
Task one: uses the
azure_rm_virtualmachine_infomodule to retrieve all instances with the filtertags:- name: retrieve vms from your_location azure.azcollection.azure_rm_virtualmachine_info: tags: "{{ your_tags | default('demo:true') }}" register: retrieved_info -
Task two: uses the
debugmodule to print out how many instances matched the filter by using thelengthfilter- name: print retrieved_info ansible.builtin.debug: msg: "There are {{ retrieved_info.vms | length }} virtual machine(s) that match your filter" -
Task three: will print out specific information for each virtual machine.
- name: print tags ansible.builtin.debug: msg: - name: "{{ item.name }}" - tags: "{{ item.tags }}" - power_state: "{{ item.power_state }}" loop: "{{ retrieved_info.vms | list }}" loop_control: label: "virtual machine info and associated tags"
-
The output in the automation controller window will look like this:
2.4. Task 4: Create a Survey
To make the Retrieve instances by tag template more flexible, you will add a survey to filter the results by a specified tag. Surveys allow you to abstract complexity by setting playbook variables with user-friendly questions.
-
Navigate to the Survey tab for the template.
Go to the Templates page, click on the name
Retrieve instances by tag, and then click theSurveytab.
-
Create a new survey question.
Click the blue
+ Create survey questionbutton.Fill out the form with the following values:
Parameter
Value
Question
Provide a specific tagDescription
Provide a list of tags to filter onAnswer variable name
your_tagsAnswer type
Multiple Choice (multiple select)Required
Check the box ☑️
Multiple Choice Options
demo:trueos:linuxos:windowsdemo:falseusername:rheluserClick the blue
Create survey questionbutton to save the question. -
Enable and launch the survey.
Enable the survey by clicking the toggle switch at the top of the page.

-
Launch the
Retrieve instances by tagjob template again. This time, you will see a survey prompt. -
Use the survey to filter results.
Try using
os:windowsto retrieve only the Windows virtual machine, oros:linuxto retrieve only the RHEL machine. This is extremely useful for managing hundreds of virtual machines.Tryusing some other tag combinations to see what is returned, such as:
-
demo:false -
username:rheluser -
os:windows
-
3. Next Steps
Press the Next button below to proceed to the next challenge.
| The Ansible Playbook for this lab is sourced from this project on GitHub. |