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

Setting up tags on resource creation stage through 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"

这会将标记 "environment" 设置为值 "development"。

如果强制执行订阅策略以在每个资源上都有特定的标签。
创建时必须设置标签。
文档说可以通过 ARM 模板。

Q1(复杂):如何通过 Azure CLI 在任何资源类型创建步骤上设置标签?
Q2:我可以至少为 Azure Function 执行此操作吗,因为 documentation 说我可以?

当我尝试关注文档时 - 在第 2 季度使用此命令链接:

az functionapp create                               \
    --name                "function-name"           \
    --resource-group      "resource-group-name"     \
    --storage-account     "gl0unique0storage0name"  \
    --tags                "environment=development"

我收到这个错误:
az: error: unrecognized arguments: --tags environment=development

Q1 (complex): How can I set tags on any resource type creation step through Azure CLI?

据我所知,您可以使用 Azure Policy to do that, follow this doc: 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")

Q2: Can I do this at least for Azure Function because documentation says that I can?

是的,你可以,命令看起来不错,确保你使用最新版本的 CLI,或者你可以尝试下面的命令 az functionapp create -g joywebapp -p joyplan -n joytest1111 -s joystoragev2 --tags 'environment=development',它在我这边工作正常。