Debian Codegraph @

ansible_collections/microsoft/ad/docs/docsite/rst/guide_attributes.rst

raw
1 .. _ansible_collections.microsoft.ad.docsite.guide_attributes:
2
3 ****************
4 Attributes guide
5 ****************
6
7 A common use case for modules in this collection is to manage various Active Directory objects, such as users, groups, computers, and more. Some of these options are exposed as direct module options but other attributes might need to be set through the ``attributes`` option common to most modules in this collection.
8
9 .. contents::
10 :local:
11 :depth: 1
12
13 .. _ansible_collections.microsoft.ad.docsite.guide_attributes.ldap_attributes:
14
15 LDAP Attributes
16 ===============
17
18 One core component of Microsoft's Active Directory (``AD``) is a Lightweight Directory Access Protocol (``LDAP``) database. This database contains all the information relevant to an AD environment such as users, computers, organizational units, and more. Each object contains a dynamic set of attributes to describe the object and conform to a schema. For example users contain attributes like ``firstName``, ``country``, ``sAMAccountName`` to describe the object itself. Microsoft document all the builtin attributes in AD in their `AD Attribute Schema <https://learn.microsoft.com/en-us/windows/win32/adschema/attributes-all>`_. For example the `SAM-Account-Name attribute <https://learn.microsoft.com/en-us/windows/win32/adschema/a-samaccountname>`_ contains the metadata around this attribute. It includes fields like:
19
20 * ``Ldap-Display-Name`` - The LDAP display name
21 * ``Syntax`` - The underlying value type that the attribute stores
22 * ``System-Only`` - Whether the attribute is set by the system, effectively making it read only
23 * ``Is-Single-Value`` - Whether the attribute value is a single value or an array/list of values
24
25 The ``Ldap-Display-Name`` is the attribute name/key that is referenced by the Ansible module. For example to manage the ``SAM-Account-Name`` attribute, it would be referenced by the key ``sAMAccountName``. Each attribute has at least 1 value associated with it, but some attributes can have multiple values. For example ``sAMAccountName`` is a ``Is-Single-Value`` attribute so only has one value but ``userCert`` can contain multiple values. The ``Active Directory Users and Computers`` snap-in (or ``dsa.msc``) can be used to view these LDAP attributes in the advanced mode. This is useful for seeing existing values as well as what attributes can be set on an object.
26
27 The LDAP schema in AD can also be extended to add custom attributes for an organization. These custom attributes are also supported in the modules in this collection. To get the LDAP schema information for attributes, the following can be run in PowerShell:
28
29 .. code-block:: PowerShell
30
31 Function Get-AttributeMetadata {
32 [CmdletBinding()]
33 param ([Parameter(ValueFromPipeline)][string[]]$Name)
34
35 begin {
36 $schema = (Get-ADRootDSE -Properties subschemaSubentry).subschemaSubentry
37 $getParams = @{
38 SearchBase = $schema
39 LDAPFilter = '(objectClass=*)'
40 Properties = 'attributeTypes'
41 }
42 $attributes = (Get-ADObject @getParams).attributeTypes
43 $queried = $false
44 }
45
46 process {
47 foreach ($n in $Name) {
48 $queried = $true
49 $attributes | Where-Object {
50 $_ -like "*Name '$n'*"
51 }
52 }
53 }
54
55 end {
56 if (-not $queried) {
57 $attributes
58 }
59 }
60 }
61
62 # Display all attributes
63 Get-AttributeMetadata
64
65 # Get specific attributes
66 Get-AttributeMetadata -Name sAMAccountName, o, objectGuid
67
68 The output is in the format::
69
70 ( $ATTRIBUTE_OID NAME '$ATTRIBUTE_NAME' SYNTAX '$TYPE_OID' [SINGLE-VALUE|NO-USER-MODIFICATION] )
71
72 The ``$TYPE_OID`` specifies the value type that can be used for this attribute. search the OID online for more information. The ``SINGLE-VALUE`` specifies if the attribute can only store 1 value. The ``NO-USER-MODIFICATION`` specifies if the attribute is read only and cannot be set.
73
74 The last example outputs::
75
76 ( 1.2.840.113556.1.4.221 NAME 'sAMAccountName' SYNTAX '1.3.6.1.4.1.1466.115.121.1.15' SINGLE-VALUE )
77 ( 2.5.4.10 NAME 'o' SYNTAX '1.3.6.1.4.1.1466.115.121.1.15' )
78 ( 1.2.840.113556.1.4.2 NAME 'objectGUID' SYNTAX '1.3.6.1.4.1.1466.115.121.1.40' SINGLE-VALUE NO-USER-MODIFICATION )
79
80 This shows the ``sAMAccountName`` is a string that can only have 1 value. The ``o`` attribute is also a string but can store multiple values. The ``objectGUID`` is a byte array value that can only have 1 value and is also read only.
81
82 .. _ansible_collections.microsoft.ad.docsite.guide_attributes.setting_attributes:
83
84 Setting Attributes
85 ==================
86
87 Each module that manages an Active Directory object will have an ``attributes`` option which is used to configure LDAP attributes directly. The dictionary accepts three keys:
88
89 * ``add`` - Adds the attribute values if not present
90 * ``remove`` - Removes the attribute values if present
91 * ``set`` - Replaces the existing attribute values with the ones specified.
92
93 Each of these keys contain a dictionary value where the keys are the LDAP attribute names by ``ldapDisplayName`` and their values to set. As an LDAP attribute can contain multiple values, the values specified can either be a single value or a list of values, for example:
94
95 .. code-block:: yaml
96
97 - microsoft.ad.user:
98 name: MyUser
99 state: present
100 attributes:
101 set:
102 comment: My Comment
103 extensionName:
104 - Extension Value 1
105 - Extension Value 2
106 - Extension Value 3
107
108 The above example will set the ``comment`` LDAP attribute of the ``MyUser`` object to the value specified. It will also ensure the ``extensionName`` attribute is set to those three values, removing any other value if present.
109
110 The ``add`` key can be used to ensure the LDAP attribute values specified are added to the Attribute value list. The opposite is true for attributes under the ``remove`` key. Any attributes there will have the values specified removed if they are present on the attribute in question. For example:
111
112 .. code-block:: yaml
113
114 - microsoft.ad.user:
115 name: MyUser
116 state: present
117 attributes:
118 add:
119 extensionName:
120 - Extension Value 1
121 - Extension Value 3
122 remove:
123 extensionName:
124 - Extension Value 2
125
126 The above example will ensure the ``extensionName`` has the values ``Extension Value 1``, ``Extension Value 3`` and remove ``Extension Value 2`` if it is set. Because ``set`` was not used, any existing values will not be touched unless they are in the ``remove`` entry.
127
128 .. note::
129 Only use LDAP attributes that can contain multiple values with ``add`` or ``remove``. Using a ``Is-Single-Value`` attribute will result in undefined behaviour.
130
131 To clear an attribute value, define the attribute under ``set`` and set the value to either null (``~``) or an empty list. For example
132
133 .. code-block:: yaml
134
135 - microsoft.ad.user:
136 name: MyUser
137 state: present
138 attributes:
139 set:
140 # Null can either be represented by no value
141 # or with tilde (~)
142 comment: ~
143 company:
144 extensionName: []
145
146 This task will ensure the ``comment``, ``company``, and ``extensionName`` attributes are cleared of any value.
147
148 .. _ansible_collections.microsoft.ad.docsite.guide_attributes.attribute_types:
149
150 Attribute Types
151 ===============
152
153 There are a few different value types that can be stored in an attribute.
154 The common types are:
155
156 * Strings
157 * Integers
158 * Booleans
159 * Byte Arrays
160 * Dates
161 * Security Descriptors
162
163 Setting a string, integer, or boolean value through an Ansible task is simply done through the YAML syntax, for example:
164
165 .. code-block:: yaml
166
167 string: This is a string
168 integer: 1
169 boolean: true
170
171 .. note::
172 Strings are compared in a case sensitive operation, that is ``"String" != "string"``.
173
174 These simple types can also be represented by a dictionary with the keys ``type`` and ``value``. The type key can be set to one of the following:
175
176 * ``bool`` - Value is casted to a boolean
177 * ``bytes`` - Value is decoded as a base64 string
178 * ``date_time`` - Value is decoded as an ISO 8601 datetime string
179 * ``int`` - Value is decoded as an integer
180 * ``security_descriptor`` - Value is decoded as a SDDL string
181 * ``string`` - Value is casted to a string
182 * ``raw`` - Value is used as is - this is the default type used
183
184 This looks like the following:
185
186 .. code-block:: yaml
187
188 - microsoft.ad.user:
189 name: MyUser
190 state: present
191 attributes:
192 set:
193 # comment: A raw value that is a string
194 comment:
195 type: raw
196 value: A string
197
198 # userAccountControl: 1234
199 userAccountControl:
200 type: int
201 value: 1234
202
203 # extensionName: ['Value 1', 'Value 2']
204 extensionName:
205 - type: raw
206 value: Value 1
207 - type: raw
208 value: Value 2
209
210 The complex dictionary value with the ``type`` and ``value`` structure is only really needed for the more complex types listed below. If omitted the value is treated as ``type: raw``.
211
212 Byte Arrays
213 -----------
214
215 As raw bytes cannot be expressed in YAML, to set an attribute with a byte array value the following format is used:
216
217 .. code-block:: yaml
218
219 - microsoft.ad.user:
220 name: MyUser
221 state: present
222 attributes:
223 set:
224 # Attribute with single value
225 dsaSignature:
226 type: bytes
227 value: YmluYXJ5
228 # Attribute with multiple values
229 userCertificate:
230 - type: bytes
231 value: Zm9vYmFy
232 - type: bytes
233 value: YmFyZm9v
234
235 The value specified here is the bytes encoded as a base64 string.
236
237 The :ref:`ansible.builtin.b64encode filter <ansible_collections.ansible.builtin.b64encode_filter>` can be used to encode strings on the fly, and the :ref:`ansible.builtin.file lookup <ansible_collections.ansible.builtin.file_lookup>` could be used to read data from a file.
238
239
240 .. code-block:: yaml
241
242 - vars:
243 sig_data: "{{ lookup('ansible.builtin.file', '/path/to/my/sig') }}"
244 microsoft.ad.user:
245 name: MyUser
246 state: present
247 attributes:
248 set:
249 # Attribute with single value
250 dsaSignature:
251 type: bytes
252 value: "{{ sig_data | ansible.builtin.b64encode }}"
253
254
255 Dates
256 -----
257
258 Attributes with datetime values are technically integer values but represent a point in time. For ease of use, these entries can be represented as an ISO 8601 extended format datetime and will be internally represented by the integer value. To specify an attribute value in the datetime format, use the same dictionary value structure as above but set the ``type`` to ``date_time``. For example:
259
260 .. code-block:: yaml
261
262 - microsoft.ad.user:
263 name: MyUser
264 state: present
265 attributes:
266 set:
267 dateAttributeSingleValue:
268 type: date_time
269 value: '2019-09-07T15:50:00+00:00'
270 dateAttributeMultipleValue:
271 - type: date_time
272 value: '2019-09-07T15:50:00Z'
273 - type: date_time
274 value: '2019-09-07T11:50:00-04:00'
275
276 Internally the datetime is converted to the UTC time and converted to the number of 100 nanosecond increments since 1601-01-01. This PowerShell snippet shows what is happening internally to get the integer value:
277
278 .. code-block:: PowerShell
279
280 $dt = '2019-09-07T15:50:00Z'
281 $dtVal = [DateTimeOffset]::ParseExact(
282 $dt,
283 [string[]]@("yyyy-MM-dd'T'HH:mm:ss.FFFFFFFK"),
284 [System.Globalization.CultureInfo]::InvariantCulture,
285 [System.Globalization.DateTimeStyles]::AssumeUniversal)
286 $dtVal.UtcDateTime.ToFileTimeUtc()
287
288 .. note:: If no timezone is specified, it is assumed to be in UTC.
289
290 Security Descriptors
291 --------------------
292
293 A security descriptor is stored as a byte array in the attribute but the ``security_descriptor`` type can be used to more conveniently represent this value in a playbook. The value specified is the `Security Descriptor Definition Language <https://learn.microsoft.com/en-us/windows/win32/secauthz/security-descriptor-definition-language>`_ (``SDDL``). This string is internally converted to the byte array needed to set the SDDL. An example of setting an attribute of this type is:
294
295 .. code-block:: yaml
296
297 - microsoft.ad.user:
298 name: MyUser
299 state: present
300 attributes:
301 set:
302 nTSecurityDescriptor:
303 type: security_descriptor
304 value: O:DAG:DAD:PAI(A;CI;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD)
305
306 SDDL strings can be quite complex so building them manually is ill-advised. It is recommended to build a test object in the ``Active Directory Users and Computers`` snap-in (or ``dsa.msc``) and set the security as needed in the ``Security`` tab. From there the SDDL string can be retrieved by doing the following:
307
308 .. code-block:: PowerShell
309
310 $dn = 'CN=ObjectName,DC=domain,DC=test'
311 $obj = Get-ADObject -Identity $dn -Properties nTSecurityDescriptor
312 $obj.nTSecurityDescriptor.GetSecurityDescriptorSddlForm('All')
313
314 .. _ansible_collections.microsoft.ad.docsite.guide_attributes.dn_lookup_attributes:
315
316 DN Lookup Attributes
317 ====================
318
319 Some attributes in Active Directory are stored as a Distinguished Name (``DN``) value that references another AD object. Some modules expose a way to lookup the DN using a more human friendly value, such as ``managed_by``. These option values must either be a string or a dictionary with the key ``name`` and optional key ``server``. The string value or the value of ``name`` is the identity to lookup while ``server`` is the domain server to lookup the identity on. The lookup identity value can be specified as a ``distinguishedName``, ``objectGUID``, ``objectSid``, ``sAMAccountName``, or ``userPrincipalName``. The below is an example of how to lookup a DN using the ``sAMAccountName`` using a string value or in the dictionary form:
320
321 .. code-block:: yaml
322
323 - name: Find managed_by using string value
324 microsoft.ad.group:
325 name: My Group
326 scope: global
327 managed_by: Domain Admins
328
329 - name: Find managed_by using dictionary value with a server
330 microsoft.ad.group:
331 name: My Group
332 scope: global
333 managed_by:
334 name: Domain Admins
335 server: OtherDC
336
337 There are also module options that can set a list of DN values for an attribute. The list values for these options are the same as the single value attributes where each DN lookup is set as a string or a dictionary with the ``name`` and optional ``server`` key.
338
339 .. code-block:: yaml
340
341 - name: Specify a list of DNs to set
342 microsoft.ad.computer:
343 identity: TheComputer
344 delegates:
345 set:
346 - FileShare
347 - name: ServerA
348 server: OtherDC
349
350 For list attributes with the ``add/remove/set`` subkey options, the ``lookup_failure_action`` option can also be set to ``fail`` (default), ``ignore``, or ``warn``. The ``fail`` option will fail the task if any of the lookups fail, ``ignore`` will ignore any invalid lookups, and ``warn`` will emit a warning but still continue on a lookup failure.
351
352 .. code-block:: yaml
353
354 - name: Specify a list of DNs to set - ignoring lookup failures
355 microsoft.ad.computer:
356 identity: TheComputer
357 delegates:
358 lookup_failure_action: ignore
359 set:
360 - FileShare
361 - MissingUser
362
363 When a ``server`` key is provided, the lookup will be done using the server value specified. It is possible to also provide explicit credentials just for that server using the ``domain_credentials`` option.
364
365 .. code-block:: yaml
366
367 - name: Set member with lookup on different server
368 microsoft.ad.group:
369 name: MyGroup
370 state: present
371 members:
372 add:
373 - GroupOnDefaultDC
374 - name: GroupOnDefaultDC2
375 - name: GroupOnOtherDC
376 server: OtherDC
377 domain_credentials:
378 - username: UserForDefaultDC
379 password: PasswordForDefaultDC
380 - name: OtherDC
381 username: UserForOtherDC
382 password: PasswordForOtherDC
383
384 In the above, the ``GroupOnOtherDC`` will be done with ``OtherDC`` with the username ``UserForOtherDC``.
385
386 The documentation for the module option will identify if the option supports the lookup behaviour or whether a DN value must be explicitly provided.