gpt4 book ai didi

json - 基于 Azure ARM JSON 的变量标签

转载 作者:行者123 更新时间:2023-12-03 04:46:21 25 4
gpt4 key购买 nike

我们使用以下模板通过 ARM 模板部署多个资源组:

"parameters": {
"ResourceGroups": {
"type": "array",
"defaultValue": [
"RG1",
"RG2",
"RG3"
]
}
"resources": [
{
"type": "Microsoft.Resources/resourceGroups",
"apiVersion": "2018-05-01",
"location": "[parameters('rgLocation')]",
"name": "[parameters('ResourceGroups')[copyIndex()])]",
"copy": {
"name": "resourcegroupcopy",
"count": "[length(parameters('ResourceGroups'))]",
"mode": "serial"
},
"properties": {},
"tags": {}

我们还想在这些资源组上编写 Azure 标记脚本。然而问题是,并非我们创建的所有资源组都需要相同的标签。它们因资源组而异。

例如:RG1需要Tag1,RG2需要Tag2等。

如何将其放入我的脚本中?

有人能指出我正确的方向吗?

谢谢!

最佳答案

答案是:这取决于你的具体要求,但通常有两种方法:if()函数、对象映射。其中任何一个都可以与 union() 函数组合。为您需要的每个标签创建一个变量:

"tag1": {
"something": "bla-bla"
},
"tag2": {
"somethingElse": "bla-bla-bla"
}

然后,您可以在资源代码中执行以下操作:

"tags": "[if(condition(something goes here, depending on your needs), variable('tag1'), variables('tag2'))]"

您可以将多个 if 语句组合在一起,也可以使用 union() 函数来合并标签(尽管不实用)。联合(变量('tag1'),变量('tag2'))。

另一种(在规模上更易于管理的方式)是使用映射器来“计算”标签属性。您希望 rg1 上有 tag1,rg2 上有 tag2,rg3 上有 tag3。简而言之,会发生什么:您正在检索一个变量,该变量的名称等于属性的值,而属性的值又等于对象的名称。令人困惑?这是一个例子。创建一个新变量:

"mapper": {
"rg1": "tag1",
"rg2": "tag2",
"rg3": "tag3",
}

然后,在您的资源中您可以执行以下操作:

"tags": "[variables(variables('mapper')[variables(parameters('ResourceGroups')[copyIndex()]))])]"
^ ^ ^ ^ name of the property would be RG1\RG2\RG3 depending on where you are in the loop. this would return value of the property, so tag1 or tag2 or tag3
^ ^ ^ access properties of the object you get from the previous function (variables('mapper'))
^ ^ get variable called 'mapper'. you will get an object
^ get variable value called tag1 or tag2 or tag3

关于json - 基于 Azure ARM JSON 的变量标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53559186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com