Imp*_*ert 0 json azure azure-resource-manager azure-rm-template
我们使用以下模板通过 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": {}
Run Code Online (Sandbox Code Playgroud)
我们还希望在这些资源组上编写 Azure 标记脚本。然而,问题是,并非我们创建的所有资源组都需要相同的标签。它们因资源组而异。
例如:RG1 需要 Tag1,RG2 需要 Tag2,等等。
我如何将其放入我的脚本中?
任何人都可以指出我正确的方向吗?
谢谢!
答案是:这取决于您的确切要求,但通常有两种方法:if() 函数、对象映射。其中任何一个都可以与 union() 函数结合使用。为您需要的每个标签创建一个变量:
"tag1": {
"something": "bla-bla"
},
"tag2": {
"somethingElse": "bla-bla-bla"
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在资源代码中执行以下操作:
"tags": "[if(condition(something goes here, depending on your needs), variable('tag1'), variables('tag2'))]"
Run Code Online (Sandbox Code Playgroud)
你可以有更多的 if 语句组合在一起,你也可以使用 union() 函数来合并标签(虽然,不实用)。联合(变量('tag1'),变量('tag2'))。
另一种(按比例更易于管理的方式)是使用映射器来“计算”标签属性。你想要 rg1 上的 tag1,rg2 上的 tag2,rg3 上的 tag3。简而言之会发生什么:您正在检索一个变量,该变量的名称等于属性的值,而该属性的值又等于对象的名称。令人困惑?这是一个例子。创建一个新变量:
"mapper": {
"rg1": "tag1",
"rg2": "tag2",
"rg3": "tag3",
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的资源中,您可以执行以下操作:
"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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
764 次 |
| 最近记录: |