每当将新对象放在Azure存储桶中时,触发Azure Runbook

我想自动化一个天蓝色的资源(启动/停止VM),目前我正在使用Automation account Runbook及其正常运行,但是我需要实现一个框架来解决这个问题:

1)每当在天蓝色的存储桶中放置新对象(excel工作表)时,触发运行手册。 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_credential(runas_connection): 从OpenSSL导入加密 进口binascii 从msrestazure导入azure_active_directory 进口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.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 Automation 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启动VM') async_vm_start = compute_client.virtual_machines.start(

'resource1','vm1') async_vm_start.wait() ''' 打印('\ n停止VM') async_vm_stop = compute_client.virtual_machines.power_off(资源组名称,虚拟机名称) async_vm_stop.wait()'''

qiu397249612 回答:每当将新对象放在Azure存储桶中时,触发Azure Runbook

我认为,只要在Azure存储容器中(在您的词“存储桶”中)添加了新的Blob(或您的词“对象”),就可以满足触发Runbook的一种方法是利用事件订阅(事件格)。有关相关信息,请参阅this文档。

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

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

希望这会有所帮助!

enter image description here

本文链接:https://www.f2er.com/2726351.html

大家都在问