Skip to content

Commit 368ef37

Browse files
committed
Fixes MicrosoftDocs/azure-docs#55115 - Correct link and add tag pattern
1 parent ee6f8fb commit 368ef37

File tree

5 files changed

+94
-6
lines changed

5 files changed

+94
-6
lines changed

articles/governance/policy/concepts/definition-structure.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ support backwards compatibility.
9191

9292
`indexed` should be used when creating policies that enforce tags or locations. While not required,
9393
it prevents resources that don't support tags and locations from showing up as non-compliant in the
94-
compliance results. The exception is **resource groups** and **subscriptions**. Policies that
95-
enforce location or tags on a resource group or subscription should set **mode** to `all` and
94+
compliance results. The exception is **resource groups** and **subscriptions**. Policy definitions
95+
that enforce location or tags on a resource group or subscription should set **mode** to `all` and
9696
specifically target the `Microsoft.Resources/subscriptions/resourceGroups` or
9797
`Microsoft.Resources/subscriptions` type. For an example, see
98-
[Enforce resource group tags](../samples/enforce-tag-rg.md). For a list of resources that support
99-
tags, see
98+
[Pattern: Tags - Sample #1](../samples/pattern-tags.md). For a list of resources that support tags, see
10099
[Tag support for Azure resources](../../../azure-resource-manager/management/tag-support.md).
101100

102101
### <a name="resource-provider-modes" />Resource Provider modes (preview)

articles/governance/policy/samples/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following are examples of different patterns using the language and operator
2121
- [Fields](./pattern-fields.md)
2222
- [Parameters](./pattern-parameters.md)
2323
- [Effect details](./pattern-effect-details.md)
24+
- [Using tags](./pattern-effect-details.md)
2425
- [Value operator](./pattern-value-operator.md)
2526
- [Count operator](./pattern-count-operator.md)
2627
- [Grouping policy definitions in an initiative](./pattern-group-with-initiative.md)

articles/governance/policy/samples/pattern-parameters.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ parameter and how it's used.
1414
## Sample 1: String parameters
1515

1616
This policy definition uses two parameters, **tagName** and **tagValue** to set what the policy
17-
assignment is looking for on resources. This format allows the policy to be used for any number of
18-
tag name and tag value combinations, but only maintain a single policy definition.
17+
assignment is looking for on resources. This format allows the policy definition to be used for any
18+
number of tag name and tag value combinations, but only maintain a single policy definition.
19+
20+
> [!NOTE]
21+
> For a tag sample that uses **mode** _All_ and works with a resource group, see
22+
> [Pattern: Tags - Sample #1](./pattern-tags.md#sample-1-parameterize-tags).
1923
2024
:::code language="json" source="~/policy-templates/patterns/pattern-parameters-1.json":::
2125

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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-parameterized-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).

articles/governance/policy/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
href: ./samples/pattern-parameters.md
5151
- name: Effect details
5252
href: ./samples/pattern-effect-details.md
53+
- name: Tags
54+
href: ./samples/pattern-tags.md
5355
- name: Value operator
5456
href: ./samples/pattern-value-operator.md
5557
- name: Count operator

0 commit comments

Comments
 (0)