Azure Management Automation Client Library
The `azure-mgmt-automation` library provides Python APIs for managing Azure Automation resources, including Automation Accounts, Runbooks, Jobs, Schedules, and Hybrid Runbook Workers. It is part of the Azure SDK for Python (Track 2) and is currently at version 1.0.0. Azure SDKs typically follow an 'as-needed' release cadence, with updates when new service features or bug fixes are available.
Common errors
-
ModuleNotFoundError: No module named 'azure.mgmt.automation'
cause The `azure-mgmt-automation` package is not installed in your Python environment.fixRun `pip install azure-mgmt-automation` to install the library. -
CredentialUnavailableError: DefaultAzureCredential failed to retrieve a token.
cause The `DefaultAzureCredential` could not find any valid authentication credentials configured in your environment (e.g., Azure CLI login, environment variables, Managed Identity).fixFor local development, run `az login` in your terminal or set `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and `AZURE_CLIENT_SECRET` environment variables. Ensure the logged-in user or service principal has the necessary permissions. -
azure.core.exceptions.ResourceNotFoundError: The resource group 'myNonExistentResourceGroup' could not be found.
cause The specified resource group name does not exist or is misspelled, or the authenticated identity lacks permissions to view it.fixVerify the resource group name for correctness. Use `az group list` to check existing resource groups. Ensure your Azure identity has 'Reader' or 'Contributor' permissions on the subscription or resource group. -
azure.core.exceptions.ClientAuthenticationError: Access denied to subscription 'your-subscription-id'.
cause The authenticated identity does not have sufficient permissions to perform the requested operation on the specified subscription or resource.fixCheck the role assignments for your Azure identity in the Azure portal or via Azure CLI (`az role assignment list`). Ensure it has roles like 'Automation Contributor' or 'Contributor' on the relevant scope (subscription, resource group, or specific resource).
Warnings
- breaking Migration from 'Track 1' (older) to 'Track 2' Azure SDKs involves significant breaking changes in package names, import paths, and client constructors. `azure-mgmt-automation` version 1.0.0 is a 'Track 2' SDK.
- gotcha Authentication can be complex, especially managing credentials across development environments (local, dev, prod). `DefaultAzureCredential` attempts multiple authentication methods sequentially.
- gotcha Resource group and subscription ID are crucial context for almost all Azure management operations. Incorrect IDs or names will lead to 'Resource Not Found' errors.
- deprecated Direct usage of older credential types like `ServicePrincipalCredentials` or `TokenCredential` subclasses from `msrestazure.azure_active_directory` is deprecated in favor of `azure-identity`'s `DefaultAzureCredential` or specific credential types from `azure.identity`.
Install
-
pip install azure-mgmt-automation
Imports
- AutomationClient
from azure.mgmt.automation import AutomationClient
- DefaultAzureCredential
from azure.common.credentials import ServicePrincipalCredentials
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.automation import AutomationClient
# Ensure you have your Azure Subscription ID set as an environment variable
# For example: export AZURE_SUBSCRIPTION_ID='your-subscription-id'
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', '')
resource_group_name = 'myResourceGroup'
automation_account_name = 'myAutomationAccount'
if not subscription_id:
raise ValueError("AZURE_SUBSCRIPTION_ID environment variable not set.")
# Authenticate using DefaultAzureCredential (supports various methods: AZURE_CLI, ENV VARS, Managed Identity, etc.)
credential = DefaultAzureCredential()
# Create an Automation client
automation_client = AutomationClient(credential, subscription_id)
try:
# Example: List automation accounts in a resource group
print(f"Listing automation accounts in resource group '{resource_group_name}':")
automation_accounts = automation_client.automation_accounts.list_by_resource_group(resource_group_name)
for account in automation_accounts:
print(f" - {account.name} (Location: {account.location})")
# Example: Get a specific automation account
account = automation_client.automation_accounts.get(resource_group_name, automation_account_name)
print(f"\nRetrieved automation account: {account.name}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure the resource group and automation account exist and your credentials have permission.")