简体   繁体   English

查询 azure 服务连接资源名称

[英]Querying an azure service connection for resource names

I have a service connection in ADO to connect to azure to update resources, but I require the names of some of the resources (eg, the storage account name eg: "st-base-storage-dev"我在 ADO 中有一个服务连接来连接到 azure 以更新资源,但我需要一些资源的名称(例如,存储帐户名称,例如:“st-base-storage-dev”

Currently, I'm grabbing this name from the portal and making a variable at the top of the stage, but was wondering if there's a way to get the value from azure without needing this type of manual intervention for future stages?目前,我正在从门户中获取此名称并在阶段顶部创建一个变量,但想知道是否有一种方法可以从 azure 获取值,而无需在未来阶段进行此类手动干预?

You can add an Azure CLI task in your pipeline to run the following command to get the specific resources properties and set the value of the name to a variable.您可以在管道中添加Azure CLI 任务以运行以下命令以获取特定资源属性并将名称的值设置为变量。 Thus you can use the variable in subsequent tasks.因此您可以在后续任务中使用该变量。 See az resource list查看az 资源列表

az resource list --resource-group ResourceGroup --resource-type Microsoft.Storage/storageAccounts

Below YAML for your reference:下面YAML供大家参考:

pool:
  vmImage: Windows-latest

steps:
- task: AzureCLI@2
  inputs:
    azureSubscription: 'YourServiceConnection'
    scriptType: 'ps'
    scriptLocation: 'inlineScript'
    inlineScript: |
      $resources = (az resource list --resource-group ResourceGroup  --resource-type Microsoft.Storage/storageAccounts) | ConvertFrom-Json
      $staccount = $resources.name
      Write-Host "staccount:" $staccount
      Write-Host "##vso[task.setvariable variable=storageaccount]$staccount"
  displayName: Get Resource name and set variable

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Write-Host "storageaccount" : $(storageaccount)'
  displayName: Print the variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM