我一直在尝试创建一个脚本,以便在工作日启动我的postgresSQL灵活服务器,并在周六和周日停止它以优化成本。
引用了Microsoft官方博客的链接:在此处输入链接说明
测试脚本给出一个错误:
错误”:{“code”:“NoRegisteredProviderFound”,“message”:“No registered resource provider found for location 'eastus..注意:我已经输入了正确的订阅ID、资源组和服务器位置。
Azure CLI命令的工作原理:az postgres flexible-server start --resource-group [ResourceGroupName] --name [ServerName]
有人能帮我解决自动化脚本错误吗?我们需要先与PostgreSQL服务器建立连接吗?或者需要导入一些额外的资源依赖项?
谢谢你的时间。
脚本:
import azure.mgmt.resource
import requests
import automationassets
from msrestazure.azure_cloud import AZURE_PUBLIC_CLOUD
from datetime import datetime
def get_token(runas_connection, resource_url, authority_url):
""" Returns credentials to authenticate against Azure resoruce manager """
from OpenSSL import crypto
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
authority_full_url = (authority_url + '/' + tenant_id)
context = adal.AuthenticationContext(authority_full_url)
return context.acquire_token_with_client_certificate(
resource_url,
application_id,
pem_pkey,
thumbprint)['accessToken']
action = ''
day_of_week = datetime.today().strftime('%A')
if day_of_week == 'Saturday':
action = 'stop'
elif day_of_week == 'Monday':
action = 'start'
subscription_id = '<SUBSCRIPTION_ID>'
resource_group = '<RESOURCE_GROUP>'
server_name = '<SERVER_NAME>'
if action:
print 'Today is ' + day_of_week + '. Executing ' + action + ' server'
runas_connection = automationassets.get_automation_connection("AzureRunAsConnection")
resource_url = AZURE_PUBLIC_CLOUD.endpoints.active_directory_resource_id
authority_url = AZURE_PUBLIC_CLOUD.endpoints.active_directory
resourceManager_url = AZURE_PUBLIC_CLOUD.endpoints.resource_manager
auth_token=get_token(runas_connection, resource_url, authority_url)
url = 'https://management.azure.com/subscriptions/' + subscription_id + '/resourceGroups/' + resource_group + '/providers/Microsoft.DBforPostgreSQL/flexibleServers/' + server_name + '/' + action + '?api-version=2020-02-14-preview'
response = requests.post(url, json={}, headers={'Authorization': 'Bearer ' + auth_token})
print(response.json())
else:
print 'Today is ' + day_of_week + '. No action taken'
错误信息:
追溯(最近一次调用):runas_connection = automationassets.get_automation_connection(“AzureRunAsConnection”)File“C:\Python27\lib\automationassets.py”,line 234,in get_automation_connection connection = _client.get_connection_asset(name)File“C:\Python27\lib\automationassets.py”,line 189,in get_connection_asset return self._issue_request(url,method=self._GET)File,in _issue_request raise AutomationAssetNotFound()automationassets.AutomationAssetNotFound.
消息:未找到accountId [Number]控制器名称[ConnectionsV 2Controller]和类型[AzureRunAsConnection]的资产
1条答案
按热度按时间kuarbcqp1#
自动启动停止使用Azure自动化的灵活服务器Python RunBook自动启动停止使用Azure自动化的灵活服务器Python RunBook
消息:未找到accountId [Number]控制器名称[ConnectionsV 2Controller]和类型[AzureRunAsConnection]的资产
根据错误消息,Runbook python脚本无法从连接中获取资产。
根据MS Document:
微软从4月1日起停止提供RunAs帐户。如果您在此之后创建了自动化帐户,则您的连接不包含此AzureRunAsConnection连接。
如果您在它之前创建了Account,解决方法是您可以手动创建AzureRunAsConnection:
如果您在4月1日之后创建帐户,则解决方法
您需要更改代码中的身份验证方法以使用托管标识
首先,您需要打开托管标识,如下所示:
将适当的角色分配给托管身份以访问Azure资源。
更改Runbook代码以使用托管身份进行身份验证。
有关更多信息,请参阅此MS文档