通过Azure CLI在资源创建阶段设置标签

时间:2018-11-22 08:28:13

标签: azure azure-devops azure-cli

一个人可以通过Azure CLI命令在Azure中创建资源:
az functionapp create \ --name "function-name" \ --resource-group "resource-group-name" \ --storage-account "gl0unique0storage0name" 稍后-下一步-在separate command中设置标签:
az resource tag --tags "environment=development" \ --name "function-name" \ --resource-group "resource-group-name" 这会将标记“环境”设置为值“开发”。

如果强制实施一项订阅政策,则在每个资源上都有一个特定标签。
必须在创建时设置标签。
文档说可以通过ARM模板实现。

第1季度(复杂):如何通过Azure CLI在任何资源类型创建步骤上设置标签?
问题2:我至少可以对Azure Function这样做,因为documentation表示可以吗?

当我尝试关注文档-在第二季度链接时,使用以下命令:
az functionapp create \ --name "function-name" \ --resource-group "resource-group-name" \ --storage-account "gl0unique0storage0name" \ --tags "environment=development" 我收到此错误:
az: error: unrecognized arguments: --tags environment=development

1 个答案:

答案 0 :(得分:2)

  

问题1(复杂):如何通过Azure CLI在任何资源类型创建步骤上设置标签?

AFAIK,您可以使用Azure Policy来做到这一点,请遵循以下文档:Apply tag and its default value

Sample for CLI

# Create the Policy Definition (Subscription scope)
definition=$(az policy definition create --name 'apply-default-tag-value' --display-name 'Apply tag and its default value' --description 'Applies a required tag and its default value if it is not specified by the user' --rules 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/apply-default-tag-value/azurepolicy.rules.json' --params 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/apply-default-tag-value/azurepolicy.parameters.json' --mode All)

# Set the scope to a resource group; may also be a subscription or management group
scope=$(az group show --name 'YourResourceGroup')

# Set the Policy Parameter (JSON format)
policyparam='{ "tagName": { "value": "costCenter" }, "tagValue": { "value": "headquarter" } }'

# Create the Policy Assignment
assignment=$(az policy assignment create --name 'apply-default-tag-value' --display-name 'Apply tag and its default value Assignment' --scope `echo $scope | jq '.id' -r` --policy `echo $definition | jq '.name' -r` --params "$policyparam")
  

第二季度:我能至少对Azure Function做到这一点,因为文档说我可以做到吗?

是的,命令看起来不错,请确保使用最新版本的CLI,也可以尝试在az functionapp create -g joywebapp -p joyplan -n joytest1111 -s joystoragev2 --tags 'environment=development'下面使用该命令,对我而言这很好。