我想自动化一个 azure 资源(前启动/停止 VM),目前我正在使用自动化帐户运行手册,它工作正常,但我需要实现一个框架,这就是:
1) 每当将新对象(excel 工作表)放入 azure 存储桶时触发运行手册。2)读取输入变量的excel表
下面是运行手册代码
有人请告诉我触发适合上述框架的运行手册的最佳方法
""" Azure 自动化文档:https ://aka.ms/azure-automation-python-documentation Azure Python SDK 文档:https ://aka.ms/azure-python-sdk """ import os import sys from 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 自动化 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停止 VM') async_vm_stop=compute_client.virtual_machines.power_off(resource_group_name, vm_name) async_vm_stop.wait()'''