如何动态地将标签应用于资源组? [英] How to dynamically apply tags to resource groups?

查看:92
本文介绍了如何动态地将标签应用于资源组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

正如标题所示,我有{"CostCenter":"12345","Customer":"Joe User","CreatedBy":"Foo User". }作为ResourceGroupA的标签,我想将其复制为ResourceGroupB

As title suggests, I have {"CostCenter":"12345", "Customer":"Joe User", "CreatedBy":"Foo User" } as my tags for ResourceGroupA, and I would like to copy that for my ResourceGroupB

我遵循了以下命令:

jsonrtag=$(az group show -g ResourceGroupA --query tags)

rt=$(echo $jsonrtag | tr -d '"{},' | sed 's/: /=/g')

azure_create_rg="az group create --location eastus2 --name ResourceGroupB --tags $rt"
$azure_create_rg

但是,它返回了以下内容:

  {
    " id" ;:" ..." ;,
    位置":"eastus2",
    "managedBy":null,
    名称":"ResourceGroupB",
    属性":{
      "provisioningState":成功"
    },
    "标签":{
      "CostCenter":"12345",
      客户":"\"乔,
      "User \":"\" CreatedBy",
      " \"Foo":"User \""
    }
  }

  {
    "id": "...",
    "location": "eastus2",
    "managedBy": null,
    "name": "ResourceGroupB",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": {
      "CostCenter": "12345",
      "Customer": "\"Joe",
      "User\"": "\"CreatedBy",
      "\"Foo": "User\""
    }
  }

显然,转义字符"是指解析为标签Json格式时被考虑在内.

Obviously, the "escape character" is being accounted when parsing into tags Json format. 

有人可以提出建议吗?谢谢!

Can anyone please advise? Thanks!

推荐答案

我相信问题出在正则表达式中's/:/=/g'     :捕获了除空格以外的所有内容,因此它不会在"Joe User"(Joe用户)中选择"User"一词.

I believe the problem is in the regular expression 's/: /=/g'    where the : is capturing everything except blank spaces therefore it doesn't pick up the words User in "Joe User".

尝试以下未经测试的表达式:

Try the following untested expression:

rt =


( echo
(echo


jsonrtag | tr - d '" {},' | sed 的/[a-zA-Z] *:([[a-zA-Z] | \ s)*/= /g' )
jsonrtag | tr -d '"{},' | sed 's/[a-zA-Z]*:([a-zA-Z]|\s)* /=/g')

应该在结肠左侧捕获一个单词,在结肠右侧捕获一个或多个单词.

Which should capture one word on the left side of the colon and one or more words on the right side of the colon.

希望获得帮助.

古斯塔沃·洛佩兹(Gustavo Lopez).

Gustavo Lopez.


这篇关于如何动态地将标签应用于资源组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆