![](/img/trans.png)
[英]Running GitActions Pipeline to create resources in Azure with Ansible Could not retrieve credential from local cache for service principal
[英]Create an automation credential from a yaml pipeline
我正在嘗試從 Azure DevOps 中的 YAML 管道創建自動化憑據。 我正在使用 AzurePowerShell@5 任務在 YAML 文件中編寫(內聯)PowerShell 腳本。 並且要創建的憑據的用戶名和密碼存儲在 Azure DevOps 中的一個變量組中。
在 Microsoft 文檔中,cmdlet New-AzAutomationCredential包含如何使用 cmdlet 的示例:
$User = "Contoso\PFuller"
$Password = ConvertTo-SecureString "Password" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
New-AzAutomationCredential -AutomationAccountName "Contoso17" -Name "ContosoCredential" -Value $Credential -ResourceGroupName "ResourceGroup01"
但是如果我嘗試用變量組中的兩個變量替換純文本用戶名和密碼,則部署失敗:
$User = $UserVariable
$Password = ConvertTo-SecureString $PasswordVariable -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
New-AzAutomationCredential -AutomationAccountName "Contoso17" -Name "ContosoCredential" -Value $Credential -ResourceGroupName "ResourceGroup01"
是否可以使用管道變量創建憑據並執行 New-AzAutomationCredential cmdlet?
編輯:添加有關我在哪個級別設置變量組的信息:
- stage: devstage
displayName: 'Dev Environment Deployment'
dependsOn: build
condition: succeeded()
variables:
- group: dev-vg
如果它們在變量中,那么您應該從環境中引用它們或將它們作為參數顯式傳遞給腳本。 除非明確配置,否則不會在環境中設置機密。
AzurePowerShell@v5
- inputs:
script: |
$User = $env:username
$Password = ConvertTo-SecureString $env:password -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
New-AzAutomationCredential -AutomationAccountName "Contoso17" -Name "ContosoCredential" -Value $Credential -ResourceGroupName "ResourceGroup01"
- env:
myuser: $(username)
mypassword: $(password)
或者,將變量直接傳遞到腳本內容中(警告,腳本,包括密碼將通過這種方式寫入磁盤):
AzurePowerShell@v5
- inputs:
script: |
$User = $(username)
$Password = ConvertTo-SecureString $(password) -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Password
New-AzAutomationCredential -AutomationAccountName "Contoso17" -Name "ContosoCredential" -Value $Credential -ResourceGroupName "ResourceGroup01"
在 YAML 管道中,您可以在根、階段和作業級別設置變量。 您還可以在 UI 中指定 YAML 管道之外的變量。 當您在 UI 中設置變量時,該變量可以被加密並設置為機密。 秘密變量不會在 YAML 管道中自動解密,需要使用 env: 或根級別的變量傳遞到您的 YAML 文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.