Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
This guide explains how to use Python and the Azure SDK to start a service on an Azure VM and check its status. We’ll use the RunCommandInput
from the Azure Compute Management Client to send shell commands directly to the VM, allowing us to manage services (such as Nginx) without logging into the VM manually.
pip
: “pip install azure-identity azure-mgmt-compute
“This script will:
DefaultAzureCredential
.stdout
and stderr
messages in JSON format.Here’s the complete code:
import json
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import RunCommandInput
# Define subscription and resource details
subscription_id = "{ YOUR SUBSCRIPTION }"
resource_group_name = "{ RESOURCE GROUP NAME }"
vm_name = "{ YOUR VM NAME }"
# Initialize Azure credentials and client
credential = DefaultAzureCredential()
compute_client = ComputeManagementClient(credential, subscription_id)
# Define the service to manage (in this case, Nginx)
services_os = "nginx"
# Define the command to run on the VM
command = RunCommandInput(
command_id='RunShellScript',
script=[
f'systemctl start {services_os}',
f'systemctl is-active {services_os}'
]
)
# Execute the command
stop_nginx = compute_client.virtual_machines.begin_run_command(
resource_group_name, vm_name, command
)
result = stop_nginx.result()
# Parse the output
output_messages = []
for message in result.value:
stdout_start = message.message.find("[stdout]") + len("[stdout]\n")
stderr_start = message.message.find("[stderr]") + len("[stderr]\n")
stdout = message.message[stdout_start:stderr_start - len("[stderr]\n")].strip()
stderr = message.message[stderr_start:].strip()
output_message = {
"code": message.code,
"stdout": stdout,
"stderr": stderr
}
output_messages.append(output_message)
# Print the result in JSON format
result_dict = {"output": output_messages}
print(json.dumps(result_dict, indent=4))
DefaultAzureCredential
to authenticate with Azure allows for seamless authentication, accommodating multiple methods, including environment variables and managed identities.RunCommandInput
object specifies the command to start the Nginx service and check its active status.begin_run_command()
on the compute_client.virtual_machines
object to send the command to the specified VM.stdout
and stderr
for each response, organizing them into a dictionary.To run the script, use the following command in your terminal:
python manage_service_on_vm.py
On successful execution, the output will look something like this:
{
"output": [
{
"code": "ProvisioningState/succeeded",
"stdout": "active",
"stderr": ""
}
]
}
This script can be used for various scenarios:
Using the Azure SDK’s RunCommandInput
, we can streamline the management of VM services directly from a Python script, saving time and reducing the need for manual VM access. This approach is particularly useful for automated management and monitoring of services on remote VMs.