Lab Guide: Understanding Tags for Azure Optimization

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


1. Lab Briefing

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

1.1. Lab Summary

Welcome to the Ansible Hybrid Cloud Automation - Cloud Optimization lab. In this lab, you will use Microsoft Azure as the public cloud provider. We have created an Azure account just for this lab so you can automate against a real Microsoft Azure environment with two virtual machines already running.

Diagram of cloud optimization

1.2. Cloud Resources

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. There is no limit to how many tags a resource can have.

Various Azure cloud resources

1.3. Example of Tags

This screenshot from the Microsoft Azure console shows a virtual machine with several tags. These tags help to quickly understand what the instance is used for, who provisioned it, who owns it, and more.

Example of tags on an Azure virtual machine

This concludes your lab briefing. Please move forward. ---

2. Lab Guide: Hands-On Tasks

Estimated time to complete: 10 minutes

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.

2.1. Task 1: Log into Automation Controller

First, you will log in to the Ansible Automation Platform to begin the lab exercises.

  1. Navigate to the Ansible Automation Platform UI.

    Click on the Ansible Automation Platform 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

Next, you will explore the pre-configured dynamic inventory and inspect the host variables and tags that were automatically discovered from Azure.

  1. Navigate to the Inventories page.

    To view them, Expand the Automation Execution menu on the left. Automation ExecutionInfrastructureInventories.

  2. Select the Azure Inventory.

    The inventory has been created for you and synced from Azure automatically. Click on the inventory named Azure Inventory.

    Azure Inventory details page
  3. View the hosts.

    Click on the Hosts tab. You will see two hosts listed.

    Inventory Hosts tab

  4. Inspect a host’s variables.

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

    List of returned hosts
  5. Examine the tags.

    On the host’s Details tab, find the VARIABLES section.

    Scroll down to the tags section in the Variables frame to see the key-value pairs discovered from the Azure virtual machine. Take note of the tags, as you will use them in a future challenge. You can view the details in both YAML and JSON format.

    Azure tags in the host variables
    To see these same tags on the Azure resource, you can switch to the Cloud(Azure) tab at the top of your lab environment.

2.3. Task 3: Create a Job Template to Display Tags

Now, you will create a job template to run a playbook that retrieves and displays tag information.

  1. Navigate to the Templates page.

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

  2. Create 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

    Display tag information

    Job Type

    Run

    Inventory

    Azure Inventory

    Project

    Cloud Visibility Project

    Playbook

    playbooks/display_tags.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.

The Ansible Playbooks for this lab are sourced from this project on GitHub.

2.4. Task 4: Launch the Job and Review Output

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

  1. Launch the job template.

    Scroll to the bottom, and then 🚀 Launch template.

    Launch Job Icon

  2. Understand the playbook execution.

    This playbook runs three tasks:

    • It uses the azure.azcollection.azure_rm_virtualmachine_info module to retrieve information for all virtual machines.

    • The second task prints the entire JSON payload from the first task.

    • The third task prints a formatted summary of the name, tags, and power state for each VM.

      - 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"
  3. Review the job output.

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

    Job output showing formatted tag data

3. Next Steps

Press the Next button below to proceed to the next challenge.