Debian Codegraph @

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

raw
1 .. _ansible_collections.microsoft.ad.docsite.guide_ldap_inventory:
2
3 ********************
4 LDAP Inventory guide
5 ********************
6
7 This guide covers information about the LDAP inventory plugin included in this collection. This inventory plugin can be used to build an inventory from an LDAP server source, like Microsoft Active Directory.
8
9 .. contents::
10 :local:
11 :depth: 1
12
13 .. _ansible_collections.microsoft.ad.docsite.guide_ldap_inventory.connection_info:
14
15 Connection info
16 ===============
17
18 Details on how to configure an LDAP connection can be found under :ref:`the LDAP connection guide <ansible_collections.microsoft.ad.docsite.guide_ldap_connection>`. Each of the connection options described by the plugin documentation are specified in the inventory yml configuration file like the below.
19
20 .. code-block:: yaml
21
22 plugin: microsoft.ad.ldap
23
24 # LDAP connection options can be defined in the yaml config.
25 auth_protocol: simple
26 username: UserName
27 password: MyPassword123
28 tls_mode: ldaps
29
30
31 .. _ansible_collections.microsoft.ad.docsite.guide_ldap_inventory.attributes:
32
33 Attributes
34 ==========
35
36 The LDAP inventory plugin can be used to set custom facts for each host it retrieves based on the computer object's LDAP attributes. Retrieving custom attributes is done through the ``attributes`` option in the inventory plugin definition. The value is set to one of the three following types:
37
38 * Empty string or null
39 * A template string
40 * A dictionary
41
42 .. note::
43 While an individual attribute can only be set to one of these types, it is possible to use the different value types for different attributes.
44
45 It is also possible to use the ``compose`` inventory option to use the builtin compose templating provided by inventory plugins but the LDAP attributes must first be requested through the ``attributes`` option and referenced in the ``compose`` template through the host fact the ``attributes`` set it on.
46
47 Empty string or null
48 --------------------
49
50 .. code-block:: yaml
51
52 attributes:
53 comment:
54 objectSid: ''
55 ms-Mcs-AdmPwd:
56
57 In this case each of the attribute values will be set as a host fact as they are coerced by the LDAP schema, see :ref:`value types and templating <ansible_collections.microsoft.ad.docsite.guide_ldap_inventory.value_types>`. The name of each fact will be based on the attribute name with ``-`` being replaced by ``_``. In the above example the host facts ``comment``, ``objectSid``, and ``ms_Mcs_AdmPwd`` will be set to the coerced values.
58
59 .. code-block:: yaml
60
61 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
62
63 ansible_host: MYHOST.domain.com
64 comment: test comment
65 ms_Mcs_AdmPwd: Password123!
66 objectSid: S-1-5-21-1234-1108
67 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
68
69
70 Template string
71 ---------------
72
73 .. code-block:: yaml
74
75 attributes:
76 comment: this
77 objectSid: raw | microsoft.ad.as_sid
78 ms-Mcs-AdmPwd: raw | first
79
80 This format will set the host fact based on the template value specified. Each template is implicitly wrapped with ``{{ ... }}`` and processed through Jinja2 to produce a result. This means the template string can contain filters provided by Ansible and other collections to convert the raw LDAP value into something more useful. The ``this`` variable refers to the coerced LDAP attribute value and ``raw`` refers to a list of base64 encoded byte strings of the raw LDAP attribute value. See :ref:`value types and templating <ansible_collections.microsoft.ad.docsite.guide_ldap_inventory.value_types>` for more information around what can be done inside the templates. Each host fact will be named after the attribute name with ``-`` being replaced by ``_``. In the above example the host facts ``command``, ``objectSid``, and ``ms_Mcs_AdmPwd`` will be set to the template results.
81
82 .. code-block:: yaml
83
84 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
85
86 ansible_host: MYHOST.domain.com
87 comment: test comment
88 ms_Mcs_AdmPwd: UGFzc3dvcmQxMjMh
89 objectSid:
90 - S-1-5-21-1234-1108
91 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
92
93
94 Dictionary
95 ----------
96
97 .. code-block:: yaml
98
99 attributes:
100 comment:
101 # Jinja2 native types will automatically convert this to a dict as
102 # the value is a json string.
103 my_comment:
104 other_var: this | from_json
105 objectSid:
106 sid: raw | microsoft.ad.as_sid | first
107 ms-Mcs-AdmPwd:
108 ansible_password: this
109
110 The final value that can be set on each attribute values is a dictionary where the keys are the host facts to set and the value is the template used to dervice the final value. It can be null or an empty string to refer to the LDAP coerced value of that attribute (``this``) or a template string to template a new value based on the requirements at hand. See the above two formats for more info on null/empty string vs a string template value. In the above example there are 4 host facts set:
111
112 * ``my_command`` - the coerced value for the ``comment`` attribute
113 * ``other_var`` - a dictionary created from the coerced value of ``comment`` if it was a json string
114 * ``sid`` - the computer SID value as a string derived from ``objectSid``
115 * ``ansible_password`` - the LAPS password coerced value derived from ``ms-Mcs-AdmPwd``
116
117 .. code-block:: yaml
118
119 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
120
121 ansible_host: MYHOST.domain.com
122 ansible_password: Password123!
123 my_comment:
124 foo: bar
125 other_var:
126 foo: bar
127 sid: S-1-5-21-1234-1108
128 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
129
130 .. note::
131 Fact names under each attribute key should follow Python conventions for variable names, for example ``_`` should be used instead of ``-``. Some Ansible versions will fail if using a name that is not valid.
132
133
134 .. _ansible_collections.microsoft.ad.docsite.guide_ldap_inventory.inventory_hostname:
135
136 Inventory hostname
137 ==================
138
139 By default the ``inventory_hostname`` for a found host will be based on the ``name`` LDAP attribute value. If the ``dNSHostName`` attribute is set for the computer account found, it will be set as the ``ansible_host`` fact. To define a custom ``inventory_hostname`` or ``ansible_host`` either set it in the ``attributes`` or ``compose`` plugin option under that key. For example this will set the ``inventory_hostname`` to the value of ``sAMAccountName`` without the ending ``$`` rather than the computer account LDAP ``name`` attribute.
140
141 .. code-block:: yaml
142
143 attributes:
144 sAMAccountName:
145 inventory_hostname: sAMAccountName[:-1]
146
147 .. code-block:: yaml
148
149 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
150
151 ansible_host: MYHOST.domain.com
152 microsoft_ad_distinguished_name: CN=OtherName,CN=Computers,DC=domain,DC=com
153 sAMAccountName: MYHOST$
154
155
156 It is also possible to set ``inventory_hostname`` under the ``compose`` key. The following will produce the same output as the above.
157
158 .. code-block:: yaml
159
160 attributes:
161 sAMAccountName:
162
163 compose:
164 inventory_hostname: sAMAccountName[:-1]
165
166
167 An example of setting a custom ``ansible_host`` fact that is used as the connection host but leaving the default ``inventory_hostname`` of the computer account name is:
168
169 .. code-block:: yaml
170
171 attributes:
172 sAMAccountName:
173 ansible_host: sAMAccountName[:-1]
174
175
176 .. _ansible_collections.microsoft.ad.docsite.guide_ldap_inventory.value_types:
177
178 Value types and templating
179 ==========================
180
181 Each LDAP attribute value is stored as a list of bytes but the schema supplied in the LDAP database can describe how those raw list of bytes are represented as a proper type, like a string, integer, boolean, etc. Currently only these four types are used when coercing LDAP attribute values
182
183 * Booleans
184 * Integers
185 * Bytes
186 * Strings
187
188 Booleans, integers, and strings are coerced into those specific Python types but bytes are coerced into a base64 string encoding of those bytes.
189
190 .. note::
191 The ``objectGuid`` and ``objectSid`` attributes are always coerced into strings representing the security identifier and guid respectively. These are the only attributes that have special coercion rules outside of the LDAP schema syntax.
192
193 LDAP attribute values may also be marked as a a single or multi valued attribute. A single value contains just the coerced value, or ``None/null`` if it has not been set while a multi valued attribute will be set as a list of coerced values. For example the ``comment`` is a single valued string while ``servicePrincipalName`` is a multi valued string. Using this inventory configuration that requests ``comment``, and ``servicePrincipalName`` we get the following inventory host definition:
194
195 .. code-block:: yaml
196
197 plugin: microsoft.ad.ldap
198
199 attributes:
200 comment:
201 servicePrincipalName:
202
203
204 .. code-block:: yaml
205
206 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
207
208 ansible_host: MYHOST.domain.com
209 comment: test comment
210 servicePrincipalName:
211 - WSMAN/MYHOST
212 - WSMAN/MYHOST.domain.com
213 - TERMSRV/MYHOST
214 - TERMSRV/MYHOST.domain.com
215 - RestrictedKrbHost/MYHOST
216 - HOST/MYHOST
217 - RestrictedKrbHost/MYHOST.domain.com
218 - HOST/MYHOST.domain.com
219 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
220
221 Some attributes like ``pwdLastSet`` are typically represented as a datetime value but internally are stored as integers. As there is no metadata in the LDAP schema to denote these integer values as datetime objects they will only be coerced into integer values by default.
222
223 The following filters can be used as an easy way to further convert the coerced values into something more readable:
224
225 * :ref:`microsoft.ad.as_datetime <ansible_collections.microsoft.ad.as_datetime_filter>`
226 * :ref:`microsoft.ad.as_guid <ansible_collections.microsoft.ad.as_guid_filter>`
227 * :ref:`microsoft.ad.as_sid <ansible_collections.microsoft.ad.as_sid_filter>`
228 * :ref:`microsoft.ad.parse_dn <ansible_collections.microsoft.ad.parse_dn_filter>`
229 * :ref:`microsoft.ad.split_dn <ansible_collections.microsoft.ad.split_dn_filter>`
230
231 An example of these filters being used in the ``attributes`` option can be seen below:
232
233 .. code-block:: yaml
234
235 plugin: microsoft.ad.ldap
236
237 attributes:
238 pwdLastSet:
239 password_last_set_int: this
240 password_last_set_datetime: this | microsoft.ad.as_datetime
241
242 .. code-block:: yaml
243
244 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
245
246 ansible_host: MYHOST.domain.com
247 password_last_set_datetime: 2023-02-06T07:39:09.195321+0000
248 password_last_set_int: 133201427491953218
249 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
250
251 The templates can also reference other filters that exist outside the collection, like the Ansible builtin ``from_json`` and more. The value is simply what would be placed inside ``{{ ... }}`` during a normal template operation.
252
253 .. note::
254 Lookups cannot be used in the attribute value templates, only filters.
255
256 Each template used in the ``attributes`` inventory option can reference the following variables:
257
258 * ``this``
259 * ``raw``
260 * Any previously defined attributes
261
262 The ``this`` variable refers to the coerced LDAP attribute value while ``raw`` refers to the list of base64 encoded strings representing the raw LDAP value that hasn't been coerced. As each attribute host fact is processed, it is also available in the subsequent templates under that host fact name. Here is an example of a more complex set of attributes:
263
264 .. code-block:: yaml
265
266 plugin: microsoft.ad.ldap
267
268 attributes:
269 objectSid:
270 sid: this
271 sid_raw: raw
272 sid_raw_filtered: raw | microsoft.ad.as_sid | first
273 objectGuid:
274 sAMAccountName:
275 computer_name:
276 comment:
277 comment: this
278 # Can refer to previously set attributes above
279 description: computer_name ~ " - " ~ sid ~ " - " ~ objectGuid ~ " - " ~ this
280
281 # Can also be used as a template and refer to the vars retrieved above
282 compose:
283 comment2: comment
284
285 .. code-block:: yaml
286
287 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
288
289 ansible_host: MYHOST.domain.com
290 comment: test comment
291 comment2: test comment
292 computer_name: MYHOST$
293 description: MYHOST$ - S-1-5-21-1234-1108 - 51cc490f-1de0-41ae-98ad-dc065d5b33e2 - test comment
294 objectGuid: 51cc490f-1de0-41ae-98ad-dc065d5b33e2
295 sid: S-1-5-21-1234-1108
296 sid_raw:
297 - AQMAAAAAAAUVAAAA0gQAAFQEAAA=
298 sid_raw_filtered: S-1-5-21-1234-1108
299 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
300
301
302 .. _ansible_collections.microsoft.ad.docsite.guide_ldap_inventory.laps:
303
304 LAPS
305 ====
306
307 Local Administrator Administrator Password Solution (LAPS) can be used to automatically change the password of the local administrator account on domain joined hosts. The LDAP connection plugin can retrieve the LAPS-managed value and assign it as the connection password for the target host.
308
309 There are three different attributes that can be used by LAPS to store the password information:
310
311 * ``ms-Mcs-AdmPwd`` - The legacy LAPS attribute containing the password
312 * ``msLAPS-Password`` - The Windows LAPS attribute containing the username and password
313 * ``msLAPS-EncryptedPassword`` - The Windows LAPS attribute containing the encrypted username and password
314
315 If using the legacy LAPS setup, the following will retrieve and assign the connection username and password to the LAPS-managed value:
316
317 .. code-block:: yaml
318
319 plugin: microsoft.ad.ldap
320
321 attributes:
322 ms-Mcs-AdmPwd:
323 ansible_user: '"Administrator"'
324 ansible_password: this
325
326 .. code-block:: yaml
327
328 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
329
330 ansible_host: MYHOST.domain.com
331 ansible_password: aR$lmrqK1l622H
332 ansible_user: Administrator
333 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
334
335 .. note::
336 Legacy LAPS does not store the username, the above example hardcodes the user name ``Administrator``.
337
338 If using Windows LAPS without encryption, the following will assign the connection username and password to the LAPS-managed values:
339
340 .. code-block:: yaml
341
342 plugin: microsoft.ad.ldap
343
344 attributes:
345 msLAPS-Password:
346 ansible_user: (this | from_json).n
347 ansible_password: (this | from_json).p
348 raw_example: raw
349 this_example: this
350
351 .. code-block:: yaml
352
353 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
354
355 ansible_host: MYHOST.domain.com
356 ansible_password: AWznso@ZJ+J6p9
357 ansible_user: Administrator
358 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
359 raw_example:
360 - eyJuIjoiQWRtaW5pc3RyYXRvciIsInQiOiIxZDk4MmI0MzdiN2E1YzYiLCJwIjoiQVd6bnNvQFpKK0o2cDkifQ==
361 this_example:
362 n: Administrator
363 p: AWznso@ZJ+J6p9
364 t: 1d982b437b7a5c6
365
366 Unlike Legacy LAPS, the attribute value is a json string that contains the keys:
367
368 * ``n`` - The account name the password was encrypted for
369 * ``p`` - The password for the account
370 * ``t`` - The time the password was set encoded as a FILETIME in base16
371
372 .. note::
373 It is recommended to use the ``from_json`` filter (as shown in the example above) on the ``this`` value to ensure consistent behavior in the presence or absence of Jinja2 native type support.
374
375 Getting an encrypted Windows LAPS value requires the ``dpapi-ng`` Python library to be installed. See :ref:`the LDAP connection requirements <ansible_collections.microsoft.ad.docsite.guide_ldap_connection.requirements>` for more information on this optional package and how to debug whether it's installed or not.
376
377 .. note::
378 Using Windows LAPS encrypted password is currently an experimental feature.
379
380 With the ``dpapi-ng`` package installed, an authorized LDAP user can decrypt and assign the LAPS-managed username and password to the target host connection as follows:
381
382 .. code-block:: yaml
383
384 plugin: microsoft.ad.ldap
385
386 attributes:
387 msLAPS-EncryptedPassword:
388 ansible_user: (this.value | from_json).n
389 ansible_password: (this.value | from_json).p
390 raw_example: raw
391 this_example: this
392
393 .. code-block:: yaml
394
395 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
396
397 ansible_host: MYHOST.domain.com
398 ansible_password: 6jr&}yK++{0Q}&
399 ansible_user: Administrator
400 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
401 raw_example:
402 - toLZAWR7rgfk...
403 this_example:
404 encrypted_value: MIIETgYJKoZI...
405 flags: 0
406 info: ''
407 update_timestamp: 133281382308674404
408 value: '{"n":"Administrator","t":"1d982b607ae7b64","p":"6jr&}yK++{0Q}&"}'
409
410 The ``raw`` value contains the raw base64 encoded value as stored in AD. The ``this`` value contains a dictionary with the following keys:
411
412 * ``encrypted_value``: The encrypted password blob as a base64 string
413 * ``flags``: The flags set as a bitwise int value, currently these are undocumented by Microsoft
414 * ``update_timestamp``: The FILETIME value of when the
415 * ``value``: The decrypted value containing the username and password as a JSON string
416 * ``debug``: Debug information that indicates why it failed to decrypt the value
417
418 The ``value`` key will only be present if the decryption process was successful. If it failed, the ``debug`` key will be present and contain the reason why it failed to be decrypted.
419
420 If the ``dpapi-ng`` library is not installed this is what the output would look like:
421
422 .. code-block:: yaml
423
424 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
425
426 ansible_host: MYHOST.domain.com
427 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
428 raw_example:
429 - toLZAWR7rgfk...
430 this_example:
431 debug: Cannot decrypt value as the Python library dpapi-ng is not installed
432 encrypted_value: MIIETgYJKoZI...
433 flags: 0
434 update_timestamp: 133281382308674404
435
436 The ``value`` key is no longer present and ``debug`` contains the message that ``dpapi-ng`` is not installed.
437
438 If ``dpapi-ng`` library was installed but the connection user is not authorized to decrypt the value this is what the output would look like:
439
440 .. code-block:: yaml
441
442 # ansible-inventory -i microsoft.ad.ldap.yml --host MYHOST --vars --yaml
443
444 ansible_host: MYHOST.domain.com
445 microsoft_ad_distinguished_name: CN=MYHOST,CN=Computers,DC=domain,DC=com
446 raw_example:
447 - toLZAWR7rgfk...
448 this_example:
449 debug: Failed to decrypt value due to error - ValueError GetKey failed 0x80070005
450 encrypted_value: MIIETgYJKoZI...
451 flags: 0
452 update_timestamp: 133281382308674404
453
454 A simple way to test that the connection user is able to decrypt the password is to run ``Get-LapsADPassword -Identity MYHOST`` on a Windows host as that user.