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.
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.
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
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.
-
Navigate to the Ansible Automation Platform UI.
Click on the Ansible Automation Platform tab at the top of your lab window.
-
Log in with the provided credentials.
Parameter
Value
Username
adminPassword
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.
-
Navigate to the Templates page.
Expand the
Automation Executionmenu on the left.Automation Execution→Templates. -
Create the 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
Stop instancesJob Type
RunInventory
Azure InventoryProject
Azure Demos ProjectPlaybook
project/stop_azure_vm.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. -
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.
-
Navigate to the Templates page and initiate workflow creation.
Expand the
Automation Executionmenu on the left.Automation Execution→Templates.Click the
+ Create Templatethen scroll down and clickCreate WORKFLOW job template
-
Enter the workflow details.
Parameter
Value
Name
Azure WorkflowOrganization
DefaultClick
Create workflow job template. The Workflow Visualizer will open. -
Add the first node (Retrieve Instances).
Click the blue
+ Add Stepbutton. Configure the first step as follows:-
Node Type:
Job Template -
Job Template: Select
Retrieve instances by tag. ClickNext. -
Survey: In the
Provide a specific tagfield, enter/selectos:windows. -
Click
Next, thenFinishto add the node.
-
-
Add the second node (Stop Instances).
Hover over the elipsis (3 dots) of the
Retrieve instances by tagnode, click the+ Add Step and linkoption.Configure it as follows:
-
Job Template: Select
Stop instances. -
Status: Select
Run On Success. ClickNext. -
Click
Finish.
-
-
Save the workflow.
In the top left corner of the Visualizer, click
Save.
2.4. Task 4: Launch the Workflow
Now you are ready to run the complete workflow.
-
Navigate to the Templates page.
Expand the
Automation Executionmenu on the left.Automation Execution→Templates. -
Launch the workflow.
Find the
Azure Workflowin the list and click🚀 Launch templateicon.
-
Monitor the Workflow visualizer.
The workflow visualizer will show the real-time progress. The workflow will:
-
Run the
Retrieve instances by tagnode to identify theWIN-ansibleinstance. -
Upon success, run the
Stop instancesnode, 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
-
Navigate to the inventory source.
In the left navigation menu, Expand
Automation Execution→Infrastructure→Inventories.SelectAzure Inventory, then select the Sources tab. -
Sync the inventory.
Click on
Rocket Launcher Rocket 🚀 iconand wait for the status to show Successful. -
Check the hosts.
Switch to the
Hoststab. You should now see that theWIN-ansiblehost 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
-
Create the 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
Delete instancesJob Type
RunInventory
Azure InventoryProject
Azure Demos ProjectPlaybook
project/change_state_rhel_vm_demo.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. -
Edit the workflow.
Go to the
Azure Workflowtemplate and click theEditbutton. -
Add a new node to the end of the workflow (to Delete the Instance).
Hover over the elipsis (3 dots) of the
Stop instancesnode, click the+ Add Step and linkoption.Configure it as follows:
-
Job Template: Select
Delete instances. -
Status: Select
Run On Success. ClickNext. -
Click
Finish.
-
-
Save the workflow.
In the top left corner of the Visualizer, click
Save. -
**Launch the Workflow
Now you are ready to run the complete workflow.
-
Launch the workflow.
Find the
Azure Workflowin the list and click🚀 Launch templateicon.
-
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. -
Check the instance status.
You should see that the
RHEL-ansibleinstance is still running, while the Windows instance `WIN-ansible`has been successfully DELETED. -
Want to do more with the workflow?
How about you add a step between the
Stop instancesandDelete instancesnodes 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. |