0
0
Azurecloud~15 mins

VM states (running, stopped, deallocated) in Azure - Mini Project: Build & Apply

Choose your learning style9 modes available
Azure VM States Management
📖 Scenario: You are managing virtual machines (VMs) in Microsoft Azure. Each VM can be in one of three states: running, stopped, or deallocated. You want to create a simple configuration that tracks the state of each VM in your resource group.
🎯 Goal: Build a dictionary in Python that holds VM names as keys and their states as values. Then, add a configuration variable to filter VMs by a specific state. Finally, write code to list all VMs currently in that state.
📋 What You'll Learn
Create a dictionary called vm_states with exact VM names and states
Add a variable called filter_state to specify which VM state to filter
Use a for loop with variables vm and state to iterate over vm_states.items()
Create a list called filtered_vms that contains VM names matching filter_state
💡 Why This Matters
🌍 Real World
Cloud administrators often need to track and manage the states of virtual machines to optimize costs and performance.
💼 Career
Understanding how to represent and filter VM states programmatically is essential for roles in cloud operations and infrastructure management.
Progress0 / 4 steps
1
Create the VM states dictionary
Create a dictionary called vm_states with these exact entries: 'vm1': 'running', 'vm2': 'stopped', 'vm3': 'deallocated', 'vm4': 'running', 'vm5': 'stopped'.
Azure
Need a hint?

Use curly braces {} to create a dictionary with VM names as keys and their states as values.

2
Add a filter state variable
Add a variable called filter_state and set it to the string 'running' to filter VMs by this state.
Azure
Need a hint?

Just assign the string 'running' to the variable filter_state.

3
Filter VMs by state
Use a for loop with variables vm and state to iterate over vm_states.items(). Inside the loop, add VMs to a list called filtered_vms if their state matches filter_state. Initialize filtered_vms as an empty list before the loop.
Azure
Need a hint?

Start with an empty list, then loop through each VM and its state. Add the VM to the list if the state matches filter_state.

4
Complete the VM filtering configuration
Add a final line that sets a variable called result equal to the filtered_vms list to complete the configuration.
Azure
Need a hint?

Assign the list filtered_vms to a new variable result to complete the setup.