Lab Guide: Building an Azure Optimization Workflow

A guide to creating a workflow to filter instances and stop them using Ansible Automation Platform in a Microsoft Azure environment.


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 create a Workflow Template. Public cloud environments are often used by many different teams, and by using automation, you can create workflows to help enforce organizational policies.

Example of a completed workflow

1.2. Infrastructure Optimization

While a lot of cloud automation focuses on provisioning and de-provisioning, we can also use automation to help tame our public cloud environments and keep them under control. This is often called Day-2 operations or infrastructure optimization.

The cycle of infrastructure optimization

This can include tasks like identifying non-compliant resources, scheduling instances to be turned off outside of business hours, or finding and removing orphaned resources. (or even removing resources that are no longer needed)

Two examples of infrastructure optimization are shown below

Example of identifying a non-compliant VM instance
Example of scheduling an instance to be stopped

This concludes your lab briefing. Please move forward.

2. Lab Guide: Hands-On Tasks

Estimated time to complete: 20 minutes

In this third challenge, you will create a workflow that uses your existing job template to find specific instances and then performs an action on them.

2.1. Task 1: Log into Ansible Automation Platform

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.

    Parameter

    Value

    Username

    admin

    Password

    ansible123!

After logging in, you will land on the main dashboard.

2.2. Task 2: Create the "Stop Instances" Job Template

You will start by creating a job template whose purpose is to STOP Azure virtual machines.

  1. Navigate to the Templates page.

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

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

    Stop instances

    Job Type

    Run

    Inventory

    Azure Inventory

    Project

    Azure Demos Project

    Playbook

    project/stop_azure_vm.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.

  5. Understand the playbook execution.

    This playbook has one task:

It uses the azure.azcollection.azure_rm_virtualmachine module to change the state of VMs passed to it via the vm_name_list variable.

- name: Toggle VM state (STOP the VM)
  azure.azcollection.azure_rm_virtualmachine:
    resource_group: "{{ resource_group_name }}"
    name: "{{ item }}"
    started: "{{ power_state | default('false') }}" # Set to true to start the VM, false to STOP it.
  loop: "{{ vm_name_list }}"

2.3. Task 3: Build the Azure Optimization Workflow

Now, you will combine the template from the previous lab (Retrieve instances by tag) with the new Stop instances template into a single workflow template.

  1. Navigate to the Templates page and initiate workflow creation.

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

    Click the + Create Template then scroll down and click Create WORKFLOW job template

    Add workflow template button
  2. Enter the workflow details.

    Parameter

    Value

    Name

    Azure Workflow

    Organization

    Default

    Click Create workflow job template. The Workflow Visualizer will open.

  3. Add the first node (Retrieve Instances).

    Click the blue + Add Step button. Configure the first step as follows:

    1. Node Type: Job Template

    2. Job Template: Select Retrieve instances by tag. Click Next.

    3. Survey: In the Provide a specific tag field, enter/select os:windows.

    4. Click Next, then Finish to add the node.

  4. Add the second node (Stop Instances).

    Hover over the elipsis (3 dots) of the Retrieve instances by tag node, click the + Add Step and link option.

    Configure it as follows:

    1. Job Template: Select Stop instances.

    2. Status: Select Run On Success. Click Next.

    3. Click Finish.

  5. Save the workflow.

    In the top left corner of the Visualizer, click Save.

    Save workflow button

2.4. Task 4: Launch the Workflow

Now you are ready to run the complete workflow.

  1. Navigate to the Templates page.

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

  2. Launch the workflow.

    Find the Azure Workflow in the list and click 🚀 Launch template icon.

    Launch Job Icon

  3. Monitor the Workflow visualizer.

    The workflow visualizer will show the real-time progress. The workflow will:

  4. Run the Retrieve instances by tag node to identify the WIN-ansible instance.

  5. Upon success, run the Stop instances node, which will POWER OFF the instance identified in the first step.

2.5. Task 5: Verify the Instance is Stopped

Finally, you will confirm the result.

2.5.1. By Updating the Azure Inventory Source

  1. Navigate to the inventory source.

    In the left navigation menu, Expand Automation ExecutionInfrastructureInventories.Select Azure Inventory, then select the Sources tab.

  2. Sync the inventory.

    Click on Rocket Launcher Rocket 🚀 icon and wait for the status to show Successful.

  3. Check the hosts.

    Switch to the Hosts tab. You should now see that the WIN-ansible host is no longer in the inventory (as it is no longer running).

2.6. Task 6: EXTRA CHALLENGE - Update the workflow to delete the instance

  1. Create the new job template.

    Click the + Create Template then scroll down and click Create job template

  2. Enter the job template details.

    Fill out the form with the following information:

    Parameter

    Value

    Name

    Delete instances

    Job Type

    Run

    Inventory

    Azure Inventory

    Project

    Azure Demos Project

    Playbook

    project/change_state_rhel_vm_demo.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.
  3. Save the job template.

    Scroll to the bottom, click Create job template.

  4. Edit the workflow.

    Go to the Azure Workflow template and click the Edit button.

  5. Add a new node to the end of the workflow (to Delete the Instance).

    Hover over the elipsis (3 dots) of the Stop instances node, click the + Add Step and link option.

    Configure it as follows:

    1. Job Template: Select Delete instances.

    2. Status: Select Run On Success. Click Next.

    3. Click Finish.

  6. Save the workflow.

    In the top left corner of the Visualizer, click Save.

  7. **Launch the Workflow

    Now you are ready to run the complete workflow.

  8. Launch the workflow.

    Find the Azure Workflow in the list and click 🚀 Launch template icon.

    Launch Job Icon

  9. Validate the result in the Cloud Azure tab.

    Click on the Cloud (Azure) tab at the top of the lab environment to open it, and view the Azure resources. You may ned to refresh the page to see the changes by clicing on the circular refresh icon next to the tab name.

  10. Check the instance status.

    You should see that the RHEL-ansible instance is still running, while the Windows instance `WIN-ansible`has been successfully DELETED.

  11. Want to do more with the workflow?

    How about you add a step between the Stop instances and Delete instances nodes to require an admin approval before deleting the instance?! If you do this, you will need to re-run the existing job template to spin up the Windows server instance again. Then you can test your workflow with the admin approval step. Have fun!

3. Why Cloud Optimization is Important

You have successfully completed this challenge. This simple lab demonstrates a powerful concept for managing cloud costs and resources. Automation workflows can help you:

  • Turn off unused resources: Automatically shut down instances that are no longer needed.

  • Right-size cloud resources: Identify over-provisioned instances and adjust them.

  • Recover orphaned resources: Find and remove resources left behind by failed processes.

Imagine scheduling a workflow to run nightly, searching for any development instances left running for more than a few hours. This gives cloud teams peace of mind that their infrastructure is not incurring unnecessary charges.

Thank you for taking the time to learn about automating cloud infrastructure optimization tasks in Azure using the Red Hat Ansible Automation Platform!

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