在 Azure 自动化中管理 Python 3 程序包(预览版)Manage Python 3 packages (preview) in Azure Automation

通过 Azure 自动化,可以在 Azure 和 Linux 混合 Runbook 辅助角色上运行 Python 3 runbook(预览版)。Azure Automation allows you to run Python 3 runbooks (preview) 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. 若要导入单个包,请参阅导入包To import a single package, see Import a package. 若要导入具有多个包的包,请参阅导入具有依赖项的包To import a package with multiple packages, see Import a package with dependencies. 本文介绍了如何在 Azure 自动化中管理和使用 Python 3 程序包(预览版)。This article describes how to manage and use Python 3 packages (preview) in Azure Automation.

导入包Import a package

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

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

在“添加 Python 包”页上,选择“Python 3”作为版本,然后选择要上传的本地包。On the Add Python Package page, select Python 3 for the Version, and select a local package to upload. 包可以是 .whl 或 .tar.gz 文件 。The package can be a .whl or .tar.gz file. 选择程序包后,选择“确定”以上传程序包。When the package is selected, select OK to upload it.

屏幕截图显示了“添加 Python 3 程序包”页面,其中选择了已上传的 tar.gz 文件。

导入程序包之后,该程序包将在自动化帐户的“Python 2 程序包”页中列出,位于“Python 3 程序包(预览版)”选项卡下。如果需要删除某个程序包,请选择该程序包并单击“删除”。Once a package has been imported, it's listed on the Python packages page in your Automation account, under the Python 3 packages (preview) tab. If you need to remove a package, select the package and click Delete.

屏幕截图显示了导入程序包后的“Python 3 程序包”页。

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

可以导入 Python 3 包及其依赖项,方法是将以下 Python 脚本导入到 Python 3 Runbook 中,然后运行它。You can import a Python 3 package and its dependencies by importing the following Python script into a Python 3 runbook, and then running it.

https://github.com/azureautomation/runbooks/blob/master/Utility/Python/import_py3package_from_pypi.py

将脚本导入到 Runbook 中Importing the script into a runbook

有关导入 Runbook 的信息,请参阅通过 Azure 门户导入 RunbookFor information on importing the runbook, see Import a runbook from the Azure portal. 在运行导入之前,将文件从 GitHub 复制到门户可访问的存储。Copy the file from GitHub to storage that the portal can access before you run the import.

“导入 Runbook”页默认设置 Runbook 名称以与脚本名称匹配。The Import a runbook page defaults the runbook name to match the name of the script. 如果你有权访问该字段,则可以更改名称。If you have access to the field, you can change the name. “Runbook 类型”可能默认设置为“Python 2”。Runbook type may default to Python 2. 如果是这样,请确保将其更改为“Python 3”。If it does, make sure to change it to Python 3.

显示 Python 3 Runbook 导入页的屏幕截图。

执行 Runbook 以导入包和依赖项Executing the runbook to import the package and dependencies

创建并发布 Runbook 后,运行它以导入包。After creating and publishing the runbook, run it to import the package. 有关执行 Runbook 的详细信息,请参阅在 Azure 自动化中启动 RunbookSee Start a runbook in Azure Automation for details on executing the runbook.

脚本 (import_py3package_from_pypi.py) 需要以下参数。The script (import_py3package_from_pypi.py) requires the following parameters.

参数Parameter 说明Description
subscription_idsubscription_id 自动化帐户的订阅 IDSubscription ID of the Automation account
resource_groupresource_group 定义自动化帐户的资源组的名称Name of the resource group that the Automation account is defined in
automation_accountautomation_account 自动化帐户名称Automation account name
module_namemodule_name 要从 pypi.org 中导入的模块的名称Name of the module to import from pypi.org

有关将参数与 Runbook 结合使用的详细信息,请参阅使用 Runbook 参数For more information on using parameters with runbooks, see Work with runbook parameters.

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

导入程序包后,可以在 runbook 中使用它。With the package imported, you can use it in a runbook. 添加以下代码以列出 Azure 订阅中的所有资源组。Add the following code to list all the resource groups in an Azure subscription.

import os  
import azure.mgmt.resource  
import automationassets  

def get_automation_runas_credential(runas_connection):  
    from OpenSSL import crypto  
    import binascii  
    from msrestazure import azure_active_directory  
    import adal 

    # Get the Azure Automation RunAs service principal certificate  
    cert = automationassets.get_automation_certificate("AzureRunAsCertificate")  
    pks12_cert = crypto.load_pkcs12(cert)  
    pem_pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM,pks12_cert.get_privatekey())  

    # Get run as connection information for the Azure Automation service principal 
    application_id = runas_connection["ApplicationId"]  
    thumbprint = runas_connection["CertificateThumbprint"]  
    tenant_id = runas_connection["TenantId"]  

    # Authenticate with service principal certificate  
    resource ="https://management.core.chinacloudapi.cn/"  
    authority_url = ("https://login.partner.microsoftonline.cn/"+tenant_id)  
    context = adal.AuthenticationContext(authority_url)  
    return azure_active_directory.AdalAuthentication(  
    lambda: context.acquire_token_with_client_certificate(  
            resource,  
            application_id,  
            pem_pkey,  
            thumbprint) 
    ) 

# Authenticate to Azure using the Azure Automation RunAs service principal  
runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")  
azure_credential = get_automation_runas_credential(runas_connection)  

# 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) 

后续步骤Next steps

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