我想自动化一个天蓝色的资源(启动/停止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()'''