在混合 Runbook 辅助角色上运行的 Runbook 通常用于管理本地计算机上的资源,或部署了辅助角色的本地环境中的资源。 Azure 自动化中的 runbook 通常管理 Azure 云中的资源。 即使使用方式不同,在 Azure 自动化中运行的 Runbook 和在混合 Runbook 辅助角色上运行的 Runbook 结构上是相同的。
创建 Runbook 以在混合 Runbook 辅助角色上运行时,应当在承载辅助角色的计算机内编辑并测试 Runbook。 主机具有管理本地资源时所需的所有 PowerShell 模块和网络访问权限。 在混合 Runbook 辅助角色计算机上测试 Runbook 后,可以将它上传到 Azure 自动化环境,用于在混合辅助角色中运行。
计划 Runbook 作业行为
Azure 自动化处理混合 Runbook 辅助角色上的作业的方式不同于处理 Azure 沙盒中运行的作业的方式。 对于长时间运行的 runbook,请确保它能在重启后复原。 有关作业行为的详细信息,请参阅混合 Runbook 辅助角色作业。
混合 Runbook 辅助角色的作业在 Windows 上的本地 System 帐户下运行,或者在 Linux 上的 nxautomation 帐户下运行。 对于 Linux,请确保 nxautomation 帐户有权访问 Runbook 模块的存储位置。 使用 Install-Module cmdlet 时,请确保为 Scope 参数指定 AllUsers,以确保 nxautomation 帐户具有访问权限。 有关 Linux 上的 PowerShell 的详细信息,请参阅非 Windows 平台上的 PowerShell 的已知问题。
配置 runbook 权限
通过以下方式定义 Runbook 的权限以在混合 Runbook 辅助角色上运行:
- 让 Runbook 向本地资源提供自己的身份验证。
- 配置使用 Azure 资源托管标识进行身份验证。
- 还可以指定运行方式帐户,为所有 Runbook 提供用户上下文。
对本地资源使用 Runbook 身份验证
如果准备向资源提供其身份验证的 Runbook,请在 Runbook 中使用凭据和证书资产。 可以通过多个 cmdlet 来指定凭据,以便 Runbook 可以对不同资源进行身份验证。 下面的示例显示了用于重新启动计算机的 Runbook 的一部分。 它从凭据资产检索凭据,从变量资产检索计算机的名称,并将这些值用于 Restart-Computer cmdlet。
$Cred = Get-AutomationPSCredential -Name "MyCredential"
$Computer = Get-AutomationVariable -Name "ComputerName"
Restart-Computer -ComputerName $Computer -Credential $Cred
还可以使用 InlineScript 活动。 InlineScript 允许你在具有凭据的另一台计算机上运行代码块。
将 Runbook 身份验证与托管标识结合使用
Azure 虚拟机上的混合 Runbook 辅助角色可以使用托管标识来向 Azure 资源进行身份验证。 使用 Azure 资源的托管标识(而不是运行方式帐户)有一些好处,因为无需执行以下操作:
- 导出运行方式证书,再将其导入到混合 Runbook 辅助角色。
- 续订运行方式帐户使用的证书。
- 在 Runbook 代码中处理运行方式连接对象。
遵循以下步骤,在混合 Runbook 辅助角色上使用 Azure 资源的托管标识:
-
创建 Azure VM。
-
在 VM 上配置 Azure 资源的托管标识。 请参阅使用 Azure 门户在 VM 上配置 Azure 资源托管标识。
-
授予 VM 对资源管理器中资源组的访问权限。 请参见使用 Windows VM 系统分配的托管标识访问资源管理器。
-
在 VM 上安装混合 Runbook 辅助角色。 请参阅部署 Windows 混合 Runbook 辅助角色或部署 Linux 混合 Runbook 辅助角色。
-
更新 Runbook,将 Connect-AzAccount cmdlet 与
Identity参数一起使用,以便对 Azure 资源进行身份验证。 此配置减少了使用运行方式帐户以及执行关联帐户管理的需求。# Connect to Azure using the managed identities for Azure resources identity configured on the Azure VM that is hosting the hybrid runbook worker Connect-AzAccount -Identity # Get all VM names from the subscription Get-AzVM | Select NameNOTE
Connect-AzAccount -Identity适用于使用系统分配的标识和单一用户分配的标识的混合 Runbook 辅助角色。 如果在混合 Runbook 辅助角色上使用多个用户分配的标识,Runbook 必须为Connect-AzAccount指定AccountId参数,以选择特定的用户分配的标识。
将 Runbook 身份验证与运行方式帐户结合使用
不需要让 Runbook 将自身的身份验证提供给本地资源,但可以针对混合 Runbook 辅助角色组指定运行方式帐户。 若要指定运行方式帐户,必须定义有权访问本地资源的凭据资产。 这些资源包括证书存储,所有 Runbook 在组中的混合 Runbook 辅助角色上使用这些凭据运行。
凭据的用户名必须采用以下格式之一:
- 域\用户名
- username@domain
- 用户名(适用于本地计算机的本地帐户)
使用以下过程针对混合 Runbook 辅助角色组指定运行方式帐户:
- 创建具有本地资源访问权限的凭据资产。
- 在 Azure 门户中打开自动化帐户。
- 选择“混合辅助角色组”,并选择特定组。
- 选择“所有设置”,然后选择“混合辅助角色组设置”。
- 将“运行方式”的值从“默认”更改为“自定义”。
- 选择凭据,并单击“保存”。
安装运行方式帐户证书
在 Azure 中部署资源时,可能需要在自动生成过程中访问本地系统以支持部署过程中的某个任务或某组步骤。 若要使用运行方式帐户针对 Azure 进行身份验证,必须安装运行方式帐户证书。
Note: 此 PowerShell runbook 当前不在 Linux 计算机上运行。 它仅在 Windows 计算机上运行。
以下 PowerShell Runbook 称为 Export-RunAsCertificateToHybridWorker,其从 Azure 自动化帐户导出运行方式证书。 Runbook 下载证书并将其导入到连接到同一帐户的混合 Runbook 辅助角色上的本地计算机证书存储中。 完成该步骤后,Runbook 会验证辅助角色能否成功地使用运行方式帐户对 Azure 进行身份验证。
Note: 此 PowerShell Runbook 并非设计为或旨在作为目标计算机上的脚本在自动化帐户之外运行。
<#PSScriptInfo
.VERSION 1.0
.GUID 3a796b9a-623d-499d-86c8-c249f10a6986
.AUTHOR Azure Automation Team
.COMPANYNAME Microsoft
.COPYRIGHT
.TAGS Azure Automation
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>
<#
.SYNOPSIS
Exports the Run As certificate from an Azure Automation account to a hybrid worker in that account.
.DESCRIPTION
This runbook exports the Run As certificate from an Azure Automation account to a hybrid worker in that account. Run this runbook on the hybrid worker where you want the certificate installed. This allows the use of the AzureRunAsConnection to authenticate to Azure and manage Azure resources from runbooks running on the hybrid worker.
.EXAMPLE
.\Export-RunAsCertificateToHybridWorker
.NOTES
LASTEDIT: 2016.10.13
#>
# Generate the password used for this certificate
Add-Type -AssemblyName System.Web -ErrorAction SilentlyContinue | Out-Null
$Password = [System.Web.Security.Membership]::GeneratePassword(25, 10)
# Stop on errors
$ErrorActionPreference = 'stop'
# Get the management certificate that will be used to make calls into Azure Service Management resources
$RunAsCert = Get-AutomationCertificate -Name "AzureRunAsCertificate"
# location to store temporary certificate in the Automation service host
$CertPath = Join-Path $env:temp "AzureRunAsCertificate.pfx"
# Save the certificate
$Cert = $RunAsCert.Export("pfx",$Password)
Set-Content -Value $Cert -Path $CertPath -Force -Encoding Byte | Write-Verbose
Write-Output ("Importing certificate into $env:computername local machine root store from " + $CertPath)
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
Import-PfxCertificate -FilePath $CertPath -CertStoreLocation Cert:\LocalMachine\My -Password $SecurePassword -Exportable | Write-Verbose
# Test to see if authentication to Azure Resource Manager is working
$RunAsConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
Connect-AzAccount `
-ServicePrincipal `
-Tenant $RunAsConnection.TenantId `
-ApplicationId $RunAsConnection.ApplicationId `
-CertificateThumbprint $RunAsConnection.CertificateThumbprint | Write-Verbose
Set-AzContext -Subscription $RunAsConnection.SubscriptionID | Write-Verbose
# List automation accounts to confirm that Azure Resource Manager calls are working
Get-AzAutomationAccount | Select-Object AutomationAccountName
Note: 对于 PowerShell Runbook,
Add-AzAccount和Add-AzureRMAccount是Connect-AzAccount的别名。 搜索库项时,如果未看到Connect-AzAccount,可以使用Add-AzAccount,或者在自动化帐户中更新模块。
若要完成运行方式帐户的准备工作:
- 以 .ps1 扩展名将 Export-RunAsCertificateToHybridWorker Runbook 保存到计算机。
- 将其导入自动化帐户。
- 编辑 Runbook,将
Password变量的值更改为你自己的密码。 - 发布 Runbook。
- 运行 Runbook,以混合 Runbook 辅助角色组为目标,该组使用运行方式帐户运行 Runbook 并对其进行身份验证。
- 检查作业流,以了解其报告某个操作尝试将证书导入本地计算机存储,且后跟多个行。 此行为取决于订阅中定义的自动化帐户数以及身份验证的成功程度。
在 Windows 混合 Runbook 辅助角色上使用已签名 Runbook
你可以将 Windows 混合 Runbook 辅助角色配置为仅运行已签名 Runbook。
Important: 将混合 Runbook 辅助角色配置为仅运行已签名 Runbook 后,未签名的 Runbook 将无法在该辅助角色上执行。
创建签名证书
以下示例创建可用于对 Runbook 签名的自签名证书。 此代码会创建证书并将其导出,使混合 Runbook 辅助角色以后可以导入它。 还会返回指纹,以备以后用于引用证书。
# Create a self-signed certificate that can be used for code signing
$SigningCert = New-SelfSignedCertificate -CertStoreLocation cert:\LocalMachine\my `
-Subject "CN=contoso.com" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" `
-KeyExportPolicy Exportable `
-KeyUsage DigitalSignature `
-Type CodeSigningCert
# Export the certificate so that it can be imported to the hybrid workers
Export-Certificate -Cert $SigningCert -FilePath .\hybridworkersigningcertificate.cer
# Import the certificate into the trusted root store so the certificate chain can be validated
Import-Certificate -FilePath .\hybridworkersigningcertificate.cer -CertStoreLocation Cert:\LocalMachine\Root
# Retrieve the thumbprint for later use
$SigningCert.Thumbprint
导入证书并配置辅助角色以进行签名验证
将创建的证书复制到组中的每个混合 Runbook 辅助角色。 运行以下脚本,以导入证书并将辅助角色配置为在 Runbook 上使用签名验证。
# Install the certificate into a location that will be used for validation.
New-Item -Path Cert:\LocalMachine\AutomationHybridStore
Import-Certificate -FilePath .\hybridworkersigningcertificate.cer -CertStoreLocation Cert:\LocalMachine\AutomationHybridStore
# Import the certificate into the trusted root store so the certificate chain can be validated
Import-Certificate -FilePath .\hybridworkersigningcertificate.cer -CertStoreLocation Cert:\LocalMachine\Root
# Configure the hybrid worker to use signature validation on runbooks.
Set-HybridRunbookWorkerSignatureValidation -Enable $true -TrustedCertStoreLocation "Cert:\LocalMachine\AutomationHybridStore"
使用证书对 Runbook 签名
将混合 Runbook 辅助角色配置为仅使用已签名 Runbook 后,必须对要在混合 Runbook 辅助角色上使用的 Runbook 签名。 使用以下示例 PowerShell 代码对这些 Runbook 签名。
$SigningCert = ( Get-ChildItem -Path cert:\LocalMachine\My\<CertificateThumbprint>)
Set-AuthenticodeSignature .\TestRunbook.ps1 -Certificate $SigningCert
对 Runbook 签名后,必须将其导入自动化帐户并与签名块一起发布。 若要了解如何导入 Runbook,请参阅导入 Runbook。
在 Linux 混合 Runbook 辅助角色上使用已签名 Runbook
为了能够使用签名的 Runbook,Linux 混合 Runbook 辅助角色必须在本地计算机上具有 GPG 可执行文件。
Important: 将混合 Runbook 辅助角色配置为仅运行已签名 Runbook 后,未签名的 Runbook 将无法在该辅助角色上执行。
你将执行以下步骤来完成此配置:
- 创建 GPG keyring 和密钥对
- 使 keyring 可供混合 Runbook 辅助角色使用
- 验证签名验证是否已打开
- 对 runbook 签名
创建 GPG keyring 和密钥对
若要创建 GPG keyring 和密钥对,请使用混合 Runbook 辅助角色 nxautomation account。
-
使用 sudo 应用程序以 nxautomation 帐户登录。
sudo su – nxautomation -
使用 nxautomation 时,将生成 GPG 密钥对。 GPG 将引导你完成每个步骤。 必须提供姓名、电子邮件地址、过期时间和密码。 然后等待,直到计算机上有足够的熵用于生成密钥。
sudo gpg --generate-key -
由于 GPG 目录使用 sudo 生成,需要使用以下命令将其所有者更改为 nxautomation。
sudo chown -R nxautomation ~/.gnupg
使 keyring 可供混合 Runbook 辅助角色使用
创建 keyring 后,需要使其可供混合 Runbook 辅助角色使用。 修改设置文件 home/nxautomation/state/worker.conf,在该文件的 [worker-optional] 节下添加以下示例代码。
gpg_public_keyring_path = /home/nxautomation/run/.gnupg/pubring.kbx
验证签名验证是否已打开
如果已在计算机上禁用签名验证,则必须通过运行以下 sudo 命令将其打开。 使用工作区 ID 替换 <LogAnalyticsworkspaceId>。
sudo python /opt/microsoft/omsconfig/modules/nxOMSAutomationWorker/DSCResources/MSFT_nxOMSAutomationWorkerResource/automationworker/scripts/require_runbook_signature.py --true <LogAnalyticsworkspaceId>
对 runbook 签名
配置签名验证后,即可使用以下 GPG 命令对 Runbook 签名。
gpg –-clear-sign <runbook name>
签名的 Runbook 称为 <runbook name>asc。
已签名 Runbook 现在可上传到 Azure 自动化中,并且可以像常规 Runbook 一样执行。
在混合 Runbook 辅助角色中启动 Runbook
在 Azure 自动化中启动 Runbook 介绍了用于启动 Runbook 的不同方法。 在混合 Runbook 辅助角色上启动 Runbook 的操作将使用“运行位置”选项,该选项允许你指定混合 Runbook 辅助角色组的名称。 指定组时,该组中的其中一个辅助角色检索和运行 Runbook。 如果 Runbook 未指定此选项,Azure 自动化会照常运行 Runbook。
在 Azure 门户中启动 Runbook 时,会看到一个“运行位置”选项,可以在其中选择“Azure”或“混合辅助角色” 。 如果选择“混合辅助角色”,则可以从下拉列表中选择混合 Runbook 辅助角色组。
使用 PowerShell 启动 Runbook 时,请将 RunOn 参数与 Start-AzAutomationRunbook cmdlet 一起使用。 以下示例使用 Windows PowerShell 在名为 MyHybridGroup 的混合 Runbook 辅助角色组中启动名为 Test-Runbook 的 Runbook。
Start-AzAutomationRunbook –AutomationAccountName "MyAutomationAccount" –Name "Test-Runbook" -RunOn "MyHybridGroup"
日志记录
为了帮助解决在混合 runbook 辅助角色上运行的 runbook 的问题,日志存储在本地的以下位置:
-
在 Windows 的
C:\ProgramData\Microsoft\System Center\Orchestrator\<version>\SMA\Sandboxes上获取详细的作业运行时进程日志记录。 概要 runbook 作业状态事件将写入 Application and Services Logs\Microsoft-Automation\Operations 事件日志中。 -
在 Linux 上,可以在
/home/nxautomation/run/worker.log找到用户混合辅助角色日志,可以在/var/opt/microsoft/omsagent/run/automationworker/worker.log找到系统 runbook 辅助角色日志。
后续步骤
- 如果 Runbook 未成功完成,请查看 Runbook 执行失败相关故障排除指南。
- 有关 PowerShell 的详细信息(包括语言参考和学习模块),请参阅 PowerShell 文档。
- 有关 PowerShell cmdlet 参考,请参阅 Az.Automation。
历史资料。此文对应 2022-08-12 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体、发布器占位标签和导航标记。
