Manage Python 2 packages in Azure Automation
This article describes how to import, manage, and use Python 2 packages in Azure Automation running on the Azure sandbox environment and Hybrid Runbook Workers. To help simplify runbooks, you can use Python packages to import the modules you need.
For information on managing Python 3 packages, see Manage Python 3 packages.
In your Automation account, select Python packages under Shared Resources. Click + Add a Python package.
On the Add Python Package page, select a local package to upload. The package can be a .whl or .tar.gz file.
Enter the name and select the Runtime version as 2.x.x
After a package has been imported, it's listed on the Python packages page in your Automation account. To remove a package, select the package and click Delete.
Import packages with dependencies
Azure automation doesn't resolve dependencies for Python packages during the import process. There are two ways to import a package with all its dependencies. Only one of the following steps needs to be used to import the packages into your Automation account.
C:\Python27\Scripts\pip2.7.exe download -d <output dir> <package name>
Once the packages are downloaded, you can import them into your automation account.
To obtain a runbook, import Python 2 packages from pypi into Azure Automation account from the Azure Automation GitHub organization into your Automation account. Make sure the Run Settings are set to Azure and start the runbook with the parameters. The runbook requires a Run As account for the Automation account to work. For each parameter make sure you start it with the switch as seen in the following list and image:
- -s <subscriptionId>
- -g <resourceGroup>
- -a <automationAccount>
- -m <modulePackage>
The runbook allows you to specify what package to download. For example, use of the
Azure parameter downloads all Azure modules and all dependencies (about 105).
After the runbook is complete, you can check the Python packages under Shared Resources in your Automation account to verify that the package has been imported correctly.
Use a package in a runbook
With a package imported, you can use it in a runbook. The following example uses the Azure Automation utility package. This package makes it easier to use Python with Azure Automation. To use the package, follow the instructions in the GitHub repository and add it to the runbook. For example, you can use
from azure_automation_utility import get_automation_runas_credential to import the function for retrieving the Run As account.
import azure.mgmt.resource import automationassets from azure_automation_utility import get_automation_runas_credential # Authenticate to Azure using the Azure Automation RunAs service principal runas_connection = automationassets.get_automation_connection("AzureRunAsConnection") azure_credential = get_automation_runas_credential() # Intialize the resource management client with the RunAs credential and subscription resource_client = azure.mgmt.resource.ResourceManagementClient( azure_credential, str(runas_connection["SubscriptionId"])) # Get list of resource groups and print them out groups = resource_client.resource_groups.list() for group in groups: print group.name
automationassets package is not available on pypi.org, so it's not available for import onto a Windows machine.
Develop and test runbooks offline
To develop and test your Python 2 runbooks offline, you can use the Azure Automation Python emulated assets module on GitHub. This module allows you to reference your shared resources such as credentials, variables, connections, and certificates.
To prepare a Python runbook, see Create a Python runbook.
Submit and view feedback for