我想自动化一个azure资源(前启动/停止VM),目前我正在使用Automation Account runbook,它工作正常,但我需要实现一个框架,如下所示:
1)每当将新对象(excel工作表)放入azure存储桶中时,都会触发runbook。2)读取excel表格中的输入变量
以下是runbook代码
有没有人能告诉我触发runbook的最好方法
“Azure自动化文档:https://aka.ms/azure-automation-python-documentation Azure Python SDK文档:https://aka.ms/azure-python-sdk”导入os从azure.mgmt.compute导入sys导入ComputeManagementClient导入azure.mgmt.resource导入自动化资产
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.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进行身份验证
automationassets.get_automation_connection("AzureRunAsConnection") = get_automation_runas_credential(runas_connection) = runas_connection = azure_credential
使用运行方式凭据初始化计算管理客户端,并指定要使用的订阅。
compute_client = ComputeManagementClient( azure_credential,str(runas_connection"SubscriptionId") )
print(‘\n启动VM') async_vm_start = compute_client.virtual_machines.start(
‘async_vm_stop=compute_client.virtual_machines.power_off(resource_group_name,1’,'vm1') async_vm_start.wait()‘打印(’\n停止VM') async_vm_stop.wait()‘’
发布于 2020-01-29 19:59:53
我相信,只要在Azure存储容器中添加了新的blob (或者用你的话来说是“对象”),就可以通过利用事件订阅(Event Grid)来满足触发runbook的要求。有关相关信息,请参阅this文档。
为了更好地说明它,您必须转到Azure Portal ->您的存储帐户(即StorageV2类型) -> Events tile -> More options ->逻辑应用程序->有两个步骤,如以下屏幕截图所示,它会验证是否添加了新的存储blob,然后运行所需的runbook
您还可以添加后续步骤,如在runbook执行完成后发送邮件等。
希望这能有所帮助!

https://stackoverflow.com/questions/59875454
复制相似问题