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.

  1. 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 virtual machines based on a tag filter.

  1. Navigate to the Templates page.

    Expand the Automation Execution menu on the left. Automation ExecutionTemplates.

  2. Initiate the creation of a new job template.

    Click the + Create Template then scroll down and click Create 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

    Azure Inventory

    Project

    Cloud Visibility Project

    Playbook

    playbooks/retrieve_vms_detail.yml

    Execution Environment

    Microsoft Azure Execution Environment

    Credentials

    azure_credential

    To select the azure_credential, click in the text field or on the drop down icon, then select Microsoft Azure Resource Manager.
  4. 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.

  1. Launch the job template.

    Select 🚀 Launch template.

  2. Review the output.

    The output in the Ansible Automation Platform will show the details of the virtual machines.

  3. Understand the playbook execution.

    This playbook has three tasks:

    • Task one: uses the azure_rm_virtualmachine_info module to retrieve all instances with the filter tags:

      - 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 debug module to print out how many instances matched the filter by using the length filter

      - 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:

Job output showing formatted tag data

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.

  1. Navigate to the Survey tab for the template.

    Go to the Templates page, click on the name Retrieve instances by tag, and then click the Survey tab.

    Survey tab

  2. Create a new survey question.

    Click the blue + Create survey question button.

    Fill out the form with the following values:

    Parameter

    Value

    Question

    Provide a specific tag

    Description

    Provide a list of tags to filter on

    Answer variable name

    your_tags

    Answer type

    Multiple Choice (multiple select)

    Required

    Check the box ☑️

    Multiple Choice Options

    demo:true os:linux os:windows demo:false username:rheluser

    Click the blue Create survey question button to save the question.

  3. Enable and launch the survey.

    Enable the survey by clicking the toggle switch at the top of the page.

    Survey enabled toggle

  4. Launch the Retrieve instances by tag job template again. This time, you will see a survey prompt.

  5. Use the survey to filter results.

    Try using os:windows to retrieve only the Windows virtual machine, or os:linux to 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.