【问题标题】:Azure resource tag bulk update using Azuredevops pipeline使用 Azuredevops 管道的 Azure 资源标签批量更新
【发布时间】:2023-01-05 11:21:03
【问题描述】:

我按照here 的回答创建了一个管道,我的要求是用我在管道中的输入覆盖每个资源的所有当前标签。因此,我决定将 az 命令与“替换”操作一起使用,这样标签将始终被我们提供的任何输入值替换,这将帮助我始终从该管道中确认哪些标签应用于每个资源。

但是正如下面的 yaml 中所给出的,“foreach”is 循环并不像我预期的那样为我工作。下面是顾虑

trigger:
- none

pool:
  name: mypool
parameters:
- name: myEnvironments
  type: object
  default:
  - development
- name: tagList
  type: object
  default:
  - resourcename:  resource1
    todeploy: yes
    allure_envtest_1: allure_envtest_1_value
    allure_envtest_2: allure envtest value

  - resourcename: resource 2
    todeploy: yes
    trip_envtest_1: trip_envtest_1_value
    trip_envtest_2: trip_envtest_2_value

stages:
- ${{ each environment in parameters.myEnvironments }}:
  - stage: 
    displayName: 'Create Tag in ${{ environment }}'
    pool:
      name: mypool
    jobs:
      - ${{ each tag in parameters.tagList }}:
        - ${{ each tagcontent in tag }}:
          - ${{ if and(ne(tagcontent.Key, 'resourcename'),ne(tagcontent.Key, 'todeploy')) }}:
            - job:
              displayName: 'Tag the reource ${{ tag.resourcename }}'
              steps:
              - task: AzureCLI@2
                displayName: "Tag the resource"
                inputs:
                  azureSubscription: ""
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: 'az tag update --resource-id ${{ tag.resourcename }} --operation replace --tags ${{ tagcontent.Key }}=${{ tagcontent.value }}'
            

  
              
  • 由于我在 az 命令中使用了“替换”操作,它总是替换为 tagList 中的最后一个值,因为所有以前的值都被这个最后一个值替换了。

  • 当我向标签值添加一些空格时,这会创建意外的标签。如何在这些标签值中使用空格

  • 上面的每个任务都为每个标签创建了多个任务,我将有许多资源作为输入,每个资源超过 10 个标签,因此这将导致非常大的管道并且难以管理。那么我们是否可以更好地整合每个任务中的步骤。

  • 在应用标记之前验证或验证标记结果的任何方法。

【问题讨论】:

  • 您的用法不正确,请参阅我的答案。 :)

标签: azure-devops azure-pipelines azure-resource-manager azure-cli azure-pipelines-yaml


【解决方案1】:

由于我在 az 命令中使用了“替换”操作,它总是替换 使用 tagList 中的最后一个值,因为所有先前的值都是 替换为最后一个值。

当我在标签值中添加一些空格时,这正在创建 意想不到的标签。如何在这些标签值中使用空格

这些情况来自于您使用的用法不正确。

从你的描述来看,看起来你想要添加标签,我创建了两个资源组来测试,写了一个 YAML 文件,它工作正常。

trigger:
- none

pool:
  vmImage: ubuntu-latest
variables:
 YAML_Path: azure-pipelines.yml
parameters:
- name: myEnvironments
  type: object
  default:
  - development
- name: tagList
  type: object
  default:
  - resourcename:  /subscriptions/xxx/resourcegroups/testgroup1
    todeploy: yes
    allure_envtest_1: allure_envtest_1_value
    allure_envtest_2: allure envtest value

  - resourcename: /subscriptions/xxx/resourcegroups/testgroup2
    todeploy: yes
    trip_envtest_1: trip_envtest_1_value
    trip_envtest_2: trip_envtest_2_value
                    
stages:
- ${{ each environment in parameters.myEnvironments }}:
  - stage: 
    displayName: 'Create Tag in ${{ environment }}'
    pool:
      vmImage: ubuntu-latest
    jobs:
      - ${{ each tag in parameters.tagList }}:
        - job: 
          displayName: 'Remove tags from resource' #This will achieve the same feature as replace operation, remove tags first.
          steps:
          - task: AzureCLI@2
            displayName: "Remove tags from the resource"
            inputs:
              azureSubscription: 'xxx'
              scriptType: 'bash'
              scriptLocation: 'inlineScript'
              inlineScript: | # Add "" in this place and use merge operation.
                  az tag delete --resource-id ${{ tag.resourcename }} --yes -y
        - ${{ each tagcontent in tag }}:
          - ${{ if and(ne(tagcontent.Key, 'resourcename'),ne(tagcontent.Key, 'todeploy')) }}:
            - job:
              displayName: 'Tag the reource ${{ tag.resourcename }}'
              steps:
              - task: AzureCLI@2
                displayName: "Tag the resource"
                inputs:
                  azureSubscription: 'testbowman_in_AAD'
                  scriptType: 'bash'
                  scriptLocation: 'inlineScript'
                  inlineScript: | # Add "" in this place and use merge operation.
                      az tag update --resource-id ${{ tag.resourcename }} --operation merge --tags "${{ tagcontent.Key }}"="${{ tagcontent.value }}"

上面的 YAML 就可以实现 'add tags' 的要求(它可以在添加标签之前删除标签,就像替换操作一样。):

上面的每个任务都为每个标签创建了多个任务,并且 我将有许多资源作为输入,每个资源超过 10 个标签,所以 这将导致非常大的管道并且难以管理。所以 我们是否可以更好地整合每个任务中的步骤。

只是强调一下,您在 DevOps YAML 管道中使用的循环的用法与通常的开发语言中的循环不同。它被命名为Conditional Insertion,此功能不能弹出一个值或在相同的值上增加。

我一开始分享的YAML已经可以实现你需要的功能了。在你的情况下,如果有很多标签,一种可行的方法是在管道中使用脚本来解析 YAML 内容并获取与标签相关的所有键值对。之后,您可以根据所有键值对生成一个字符串变量。并通过logging command,暴露给YAML管道运行概念下的其他任务。您可以在 Azure CLI 任务中使用公开的字符串变量。

我为您编写了一个 Python 脚本来获取参数中的标签:

import yaml

def get_yaml_tagList_parameters(yaml_file):
    with open(yaml_file, 'r') as stream:
        try:
            yaml_parameters = yaml.load(stream, Loader=yaml.FullLoader)
            #get parameters from yaml file
            tagList = yaml_parameters['parameters'][1]['default']
            return tagList
        except yaml.YAMLError as exc:
            print(exc)

#define the YAML file path
yaml_file = "YAML_Files/xxx.yml"
tagList = get_yaml_tagList_parameters(yaml_file)
#get the length of the list
list_length = len(tagList)
#create a string list with the length of the list
string_list = [str] * list_length
# print(list_length)
for tag in tagList:
    for key, value in tag.items():
        if key != 'resourcename' and key != 'todeploy':
            #get the array index of the tagList
            index = tagList.index(tag)
            #increase the key and value to the string list based on the index
            
            string_list[index] = str(string_list[index]) + key + "=" + """ + value + """+" "

for string in string_list:
    #remove the space at the end of the string
    print(string.replace("<class 'str'>", ""))

结果应该是这样的(确保值部分用双引号引起来):

这样的设计会很困难,但这是不可避免的,因为 Condition Insertion 不是那么灵活。

您可以设计自己的代码,以上只是示例。

在其之前验证或验证标记结果的任何方法 得到应用。

答案是不。

【讨论】:

    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 2020-01-17
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多