【问题标题】:Trigger Azure Runbook whenever put new object in Azure bucket每当将新对象放入 Azure 存储桶时触发 Azure Runbook
【发布时间】:2020-05-09 13:00:23
【问题描述】:

我想自动化一个 azure 资源(前启动/停止 VM),目前我正在使用自动化帐户运行手册,它工作正常,但我需要实现一个框架,这就是:

1) 将新对象(excel 工作表)放入 azure 存储桶时触发运行手册。 2)读取输入变量的excel表

下面是运行手册代码

有人请告诉我触发适合上述框架的运行手册的最佳方法

""" Azure 自动化文档:https://aka.ms/azure-automation-python-documentation Azure Python SDK 文档:https://aka.ms/azure-python-sdk """ 导入操作系统 导入系统 从 azure.mgmt.compute 导入 ComputeManagementClient 导入 azure.mgmt.resource 导入自动化资产

def get_automation_runas_credential(runas_connection): 从 OpenSSL 导入加密 导入 binascii 从 msrestazure 导入 azure_active_directory 进口阿达尔

# 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.windows.net/"
authority_url = ("https://login.microsoftonline.com/"+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)
)

使用 Azure 自动化 RunAs 服务主体向 Azure 进行身份验证

runas_connection = automationassets.get_automation_connection("AzureRunAsConnection") azure_credential = get_automation_runas_credential(runas_connection)

使用 RunAs 凭据初始化计算管理客户端并指定要处理的订阅。

compute_client = ComputeManagementClient( azure_credential, str(runas_connection["SubscriptionId"]) )

print('\n启动虚拟机') async_vm_start = compute_client.virtual_machines.start(

'resource1', 'vm1') async_vm_start.wait() ''' print('\n停止虚拟机') async_vm_stop=compute_client.virtual_machines.power_off(resource_group_name, vm_name) async_vm_stop.wait()'''

【问题讨论】:

    标签: python azure triggers azure-automation runbook


    【解决方案1】:

    我相信,每当在 Azure 存储容器中添加新的 blob(或用你的话说为“对象”)(用你的话说为“桶”)时,实现触发 Runbook 要求的一种方法是利用事件订阅(事件网格)。相关信息请参考this文档。

    为了以更好的方式说明它,您必须转到 Azure 门户 -> 您的存储帐户(即 StorageV2 类型)-> 事件磁贴 -> 更多选项 -> 逻辑应用程序 -> 有 2 个步骤,如图所示在下面的屏幕截图中,它会验证是否添加了新的存储 blob,然后运行所需的运行手册

    您还可以添加后续步骤,例如在 Runbook 执行完成后发送邮件等。

    希望这会有所帮助!

    【讨论】:

    • 嗨,KrishnaG runbook 支持直到 2.x python 版本,所以我使用 azure 函数实现它,我使用 Visual Studio 部署了函数,但不知道如何进一步进行,你能给我你的邮件 ID,谢谢为您提供帮助?
    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 2021-10-08
    • 1970-01-01
    • 2017-12-07
    • 2020-09-16
    • 2018-03-02
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多