每当将新对象放入Azure bucket时触发Azure Runbook

2024-05-29 03:02:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想自动化azure资源(ex-start/stop VM),目前我使用的是自动化帐户runbook,运行良好,但我需要实现一个框架,具体如下:

1)在azure bucket中放入新对象(excel工作表)时触发runbook。 2) 阅读excel表格中的输入变量

下面是runbook代码

有人请告诉我触发runbook的最佳方法,它适合上述框架

“”“ 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_凭证(runas_连接): 从OpenSSL导入加密 导入binascii 从msrestazure导入azure\u active\u目录 进口阿达尔

# 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自动化运行方式服务主体对Azure进行身份验证

runas\u connection=automationassets.get\u automation\u connection(“AzureRunAsConnection”) azure\u凭据=获取\u自动化\u运行方式\u凭据(运行方式\u连接)

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

compute_client=ComputeManagementClient( azure_凭证, str(runas_连接[“SubscriptionId”]) )

打印(“\n启动VM”) async_vm_start=计算客户端。虚拟机。启动(

“资源1”、“vm1”) 异步虚拟机启动。等待() ''' 打印(“\nStop VM”) async\u vm\u stop=compute\u client.virtual\u machines.power\u off(资源组名称、虚拟机名称) 异步\u vm\u停止。等待()


Tags: httpsidgetcertvmcertificate资源connection
1条回答
网友
1楼 · 发布于 2024-05-29 03:02:25

我相信,在Azure存储容器中添加新blob(或者用你的话说是“对象”)时(用你的话说是“bucket”)触发runbook的一种方法是利用事件订阅(事件网格)。有关信息,请参阅this文件

为了更好地说明这一点,您必须访问Azure门户->;您的存储帐户(即StorageV2类型)——>;活动瓷砖->;更多选项->;逻辑应用程序->;有如下屏幕截图所示的两个步骤,验证是否添加了新的存储blob,然后运行所需的runbook

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

希望这有帮助

enter image description here

相关问题 更多 >

    热门问题