在 Azure 自动化中管理 Python 2 包Manage Python 2 packages in Azure Automation

通过 Azure 自动化,可以在 Azure 和 Linux 混合 Runbook 辅助角色上运行 Python 2 runbook。Azure Automation allows you to run Python 2 runbooks on Azure and on Linux Hybrid Runbook Workers. 为了帮助简化 runbook,可以使用 Python 包导入所需的模块。To help in simplification of runbooks, you can use Python packages to import the modules that you need. 本文介绍如何在 Azure 自动化中管理和使用 Python 包。This article describes how to manage and use Python packages in Azure Automation.

导入包Import packages

在 Azure 自动化帐户中,选择“共享资源”下的“Python 2 包” 。In your Automation account, select Python 2 packages under Shared Resources. 单击“+ 添加 Python 2 包”。Click + Add a Python 2 package.

“Python 2 包”页的屏幕截图显示了左侧菜单中的“Python 2 包”,并突出显示了“添加 Python 2 包”。

在“添加 Python 2 包”页中,选择要上传的本地包。On the Add Python 2 Package page, select a local package to upload. 包可以是 .whl 或 .tar.gz 文件 。The package can be a .whl or .tar.gz file. 选择包后,单击“确定”以上传。When the package is selected, click OK to upload it.

“Python 2 包”页的屏幕截图显示了左侧菜单中的“Python 2 包”,并突出显示了“添加 Python 2 包”。

导入包后,它将列在 Azure 自动化帐户中的“Python 2 包”页面上。Once a package has been imported, it's listed on the Python 2 packages page in your Automation account. 如果需要移除包,请选择包并单击“删除”。If you need to remove a package, select the package and click Delete.

“Python 2 包”页的屏幕截图显示了左侧菜单中的“Python 2 包”,并突出显示了“添加 Python 2 包”。

导入具有依赖项的包Import packages with dependencies

在导入过程中,Azure 自动化不会解析 Python 包的依赖项。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. 只需使用以下步骤之一,即可将包导入到 Azure 自动化帐户中。Only one of the following steps needs to be used to import the packages into your Automation account.

手动下载Manually download

在安装有 Python2.7pip 的 Windows 64 位计算机上,运行以下命令以下载包及其所有依赖项:On a Windows 64-bit machine with Python2.7 and pip installed, run the following command to download a package and all its dependencies:

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.

RunbookRunbook

要获取 runbook,请将 Python 2 包从 pypi 导入到 Azure 自动化帐户中,从库导入到 Azure 自动化帐户中。To obtain a runbook, import Python 2 packages from pypi into Azure Automation account from the gallery into your Automation account. 请确保“运行设置”设置为“Azure”并启动具有参数的 runbook。Make sure the Run Settings are set to Azure and start the runbook with the parameters. runbook 需要有一个运行方式帐户,Azure 自动化帐户才能正常使用。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>-s <subscriptionId>
  • -g <resourceGroup>-g <resourceGroup>
  • -a <automationAccount>-a <automationAccount>
  • -m <modulePackage>-m <modulePackage>

“Python 2 包”页的屏幕截图显示了左侧菜单中的“Python 2 包”,并突出显示了“添加 Python 2 包”。

runbook 允许你指定下载的包。The runbook allows you to specify what package to download. 例如,使用 Azure 参数将下载所有 Azure 模块和所有依赖项(大约 105 个)。For example, use of the Azure parameter downloads all Azure modules and all dependencies (about 105).

完成 runbook 后,可以在 Azure 自动化帐户中的“共享资源”下查看 Python 2 包,以验证是否已正确导入包。 Once the runbook is complete, you can check the Python 2 packages under Shared Resources in your Automation account to verify that the package has been imported correctly.

在 runbook 中使用包Use a package in a runbook

导入包后,可以在 runbook 中使用。With a package imported, you can use it in a runbook. 以下示例使用 Azure 自动化实用工具包The following example uses the Azure Automation utility package. 使用此包,可以轻松地通过 Azure 自动化使用 Python。This package makes it easier to use Python with Azure Automation. 要使用该包,请按照 GitHub 存储库中的说明进行,并将其添加到 runbook。To use the package, follow the instructions in the GitHub repository and add it to the runbook. 例如,可以使用 from azure_automation_utility import get_automation_runas_credential 导入用于检索运行方式帐户的函数。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"]),
    "2017-05-10",
    "https://management.chinacloudapi.cn")

# Get list of resource groups and print them out
groups = resource_client.resource_groups.list()
for group in groups:
    print group.name

脱机开发并测试 runbookDevelop and test runbooks offline

要脱机开发并测试你的 Python 2 runbook,可以使用 GitHub 上的 Azure Automation Python 模拟资产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.

后续步骤Next steps

要准备 Python runbook,请参阅创建 Python runbookTo prepare a Python runbook, see Create a Python runbook.