|
| 1 | +--- |
| 2 | +title: "Pattern: Using tags in a policy definition" |
| 3 | +description: This Azure Policy pattern provides examples of how to add parameterized tags or inherit tags from a resource group in a policy definition. |
| 4 | +ms.date: 05/20/2020 |
| 5 | +ms.topic: sample |
| 6 | +--- |
| 7 | +# Azure Policy pattern: tags |
| 8 | + |
| 9 | +[Tags](../../..//azure-resource-manager/management/tag-resources.md) are an important part of |
| 10 | +managing, organizing, and governing your Azure resources. Azure Policy makes it possible to |
| 11 | +configure tags on your new and existing resources at scale with the |
| 12 | +[modify](../concepts/effects.md#modify) effect and |
| 13 | +[remediation tasks](../how-to/remediate-resources.md). |
| 14 | + |
| 15 | +## Sample 1: Parameterize tags |
| 16 | + |
| 17 | +This policy definition uses two parameters, **tagName** and **tagValue** to set what the policy |
| 18 | +assignment is looking for on resource groups. This format allows the policy definition to be used |
| 19 | +for any number of tag name and tag value combinations, but only maintain a single policy definition. |
| 20 | + |
| 21 | +> [!NOTE] |
| 22 | +> While this policy definition pattern is similar to the one in |
| 23 | +> [Pattern: Parameters - Sample #1](./pattern-parameters.md#sample-1-string-parameters), this sample |
| 24 | +> uses **mode** _All_ and targets resource groups. |
| 25 | +
|
| 26 | +:::code language="json" source="~/policy-templates/patterns/pattern-tags-1.json"::: |
| 27 | + |
| 28 | +### Sample 1: Explanation |
| 29 | + |
| 30 | +:::code language="json" source="~/policy-templates/patterns/pattern-tags-1.json" range="2-8" highlight="3"::: |
| 31 | + |
| 32 | +In this sample, **mode** is set to _All_ since it targets a resource group. In most cases, **mode** |
| 33 | +should be set to _Indexed_ when working with tags. For more information, see |
| 34 | +[modes](../concepts/definition-structure.md#resource-manager-modes). |
| 35 | + |
| 36 | +:::code language="json" source="~/policy-templates/patterns/pattern-tags-1.json" range="26-36" highlight="7-8"::: |
| 37 | + |
| 38 | +In this portion of the policy definition, `concat` combines the parameterized **tagName** parameter |
| 39 | +and the `tags['name']` format to tell **field** to evaluate that tag for the parameter **tagValue**. |
| 40 | +As **notEquals** is used, if **tags\[tagName\]** doesn't equal **tagValue**, the **modify** effect |
| 41 | +is triggered. |
| 42 | + |
| 43 | +:::code language="json" source="~/policy-templates/patterns/pattern-tags-1.json" range="43-47" highlight="3-4"::: |
| 44 | + |
| 45 | +Here, the same format for using the parameterized tag values is used by the **addOrReplace** |
| 46 | +operation to create or update the tag to the desired value on the evaluated resource group. |
| 47 | + |
| 48 | +## Sample 2: Inherit tag value from resource group |
| 49 | + |
| 50 | +This policy definition uses the parameter **tagName** to determine which tag's value to inherit from |
| 51 | +the parent resource group. |
| 52 | + |
| 53 | +:::code language="json" source="~/policy-templates/patterns/pattern-tags-2.json"::: |
| 54 | + |
| 55 | +### Sample 2: Explanation |
| 56 | + |
| 57 | +:::code language="json" source="~/policy-templates/patterns/pattern-tags-2.json" range="2-8" highlight="3"::: |
| 58 | + |
| 59 | +In this sample, **mode** is set to _Indexed_ since it doesn't target a resource group or |
| 60 | +subscription even though it gets the value from a resource group. For more information, see |
| 61 | +[modes](../concepts/definition-structure.md#resource-manager-modes). |
| 62 | + |
| 63 | +:::code language="json" source="~/policy-templates/patterns/pattern-tags-2.json" range="19-29" highlight="3-4,7-8"::: |
| 64 | + |
| 65 | +The **policyRule.if** uses `concat` like [Sample #1](#sample-1-parameterize-tags) to evaluate the |
| 66 | +**tagName**'s value, but uses the `resourceGroup()` function to compare it to the value of the same |
| 67 | +tag on the parent resource group. The second clause here checks that the tag on the resource group |
| 68 | +has a value and isn't null. |
| 69 | + |
| 70 | +:::code language="json" source="~/policy-templates/patterns/pattern-tags-2.json" range="36-40" highlight="3-4"::: |
| 71 | + |
| 72 | +Here, the value being assigned to the **tagName** tag on the resource also uses the |
| 73 | +`resourceGroup()` function to get the value from the parent resource group. In this way, you can |
| 74 | +inherit tags from parent resource groups. If you already created the resource but didn't add the |
| 75 | +tag, this same policy definition and a [remediation task](../how-to/remediate-resources.md) can |
| 76 | +update existing resources. |
| 77 | + |
| 78 | +## Next steps |
| 79 | + |
| 80 | +- Review other [patterns and built-in definitions](./index.md). |
| 81 | +- Review the [Azure Policy definition structure](../concepts/definition-structure.md). |
| 82 | +- Review [Understanding policy effects](../concepts/effects.md). |
0 commit comments