microsoft.ad.ldap 库存 – Active Directory 的库存插件

注意

此库存插件是 microsoft.ad 集合(版本 1.7.1)的一部分。

如果您正在使用 ansible 包,您可能已经安装了这个集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install microsoft.ad。您需要其他要求才能使用此库存插件,有关详细信息,请参阅要求

要在 playbook 中使用它,请指定:microsoft.ad.ldap

microsoft.ad 1.1.0 中的新增功能

概要

  • Active Directory 或其他 LDAP 源的库存插件。

  • 使用以 microsoft.ad.ldap.{yml|yaml} 结尾的 YAML 配置文件。

  • 添加的每个主机都将 inventory_hostname 设置为 LDAP 计算机对象的 name,并将 ansible_host 设置为 dNSHostName LDAP 属性的值(如果已设置)。如果计算机对象上未设置 dNSHostName 属性,则不会设置 ansible_host。有关如何设置这些值以及如何调整它们的更多信息,请参阅 LDAP 库存主机名

  • 主机事实 microsoft_ad_distinguished_name 也将设置为用于派生主机条目的主机的专有名称。

  • 所需的任何其他事实都需要在 _attributes_ 选项中定义。

要求

以下要求需要在执行此库存的本地控制器节点上满足。

  • dnspython - 用于选项服务器查找支持

  • pyspnego >= 0.8.0

  • pyspnego[kerberos] - 用于 Kerberos 和服务器查找支持

  • sansldap

  • dpapi-ng - 用于 LAPS 解密支持

参数

参数

注释

attributes

字典

要检索的 LDAP 属性。

指定的键是请求的 LDAP 属性,每个属性的值是一个字典,反映要将其设置为主机变量的内容以及如何设置。

内部字典值的每个键是要设置的主机变量名,值是用于派生值的模板。如果未显式设置值,则将使用从 LDAP 属性返回的强制值。

在 LDAP 架构中表示为单值的属性将作为该单值返回,多值属性将作为值列表返回。

有关更多信息,请参阅 LDAP 库存属性

默认值: {}

auth_protocol

字符串

连接到 LDAP 主机时使用的身份验证协议。

如果使用 LDAPS/StartTLS 并且已指定 _certificate_,则默认为 certificate。否则,默认为 negotiate

simple 是简单的身份验证,其中用户和密码以明文形式发送。它不支持任何加密,因此必须与 LDAPS 或 StartTLS 一起使用。如果通过没有 TLS 的明文 LDAP 连接使用,则必须指定 encrypt=False 以显式选择不加密。

certificate 是 TLS 客户端证书身份验证。它只能与 LDAPS 或 StartTLS 一起使用。有关如何指定用于身份验证的客户端证书的更多信息,请参阅 _certificate_。

negotiate 将尝试协商 Kerberos 身份验证,并回退到 NTLM。如果 Kerberos 可用,则如果未指定用户名或密码,则可以使用 Kerberos 凭据缓存。

kerberos 将使用 Kerberos 身份验证,而没有 NTLM 回退。

ntlm 将使用 NTLM 身份验证,而没有 Kerberos 尝试。

negotiatekerberosntlm 支持通过 LDAP 进行加密。

Kerberos 支持需要安装 pyspnego[kerberos] 额外功能。

有关更多信息,请参阅 LDAP 身份验证

可以使用 Jinja2 模板值设置此选项。

选择

  • "simple"

  • "certificate"

  • "negotiate"

  • "kerberos"

  • "ntlm"

配置

ca_cert

字符串

可以是用于证书验证的 CA 证书 PEM 或 DER 文件、PEM 证书目录或 CA 证书 PEM 字符串的路径。

如果省略,则用于验证的默认 CA 存储取决于当前的 Python 设置。

可以使用 Jinja2 模板值设置此选项。

配置

cert_validation

字符串

使用 TLS 连接时的证书验证行为。

可以设置为 alwaysignoreignore_hostname

always 将执行证书主机名和 CA 验证。

ignore 将忽略任何证书错误。

ignore_hostname 将验证 CA 信任链,但将忽略 TLS 执行的任何主机名检查。

有关更多信息,请参阅 证书验证

可以使用 Jinja2 模板值设置此选项。

选择

  • "always" ← (默认)

  • "ignore"

  • "ignore_hostname"

配置

certificate

字符串

用于证书身份验证的证书或包含密钥的证书捆绑包。

该值可以是包含证书的文件路径,也可以是 PEM 编码的证书字符串。

如果使用证书文件的路径,则该文件可以是 PEM 编码的证书、PEM 编码的证书和密钥捆绑包、DER 编码的证书或 PFX/PKCS12 编码的证书和密钥捆绑包。

如果指定的证书不包含密钥,请使用 *certificate_key*。

如果密钥使用密码加密,请使用 *certificate_password*。

可以使用 Jinja2 模板值设置此选项。

配置

certificate_key

字符串

用于证书身份验证的证书密钥。

该值可以是包含 PEM 或 DER 编码格式的密钥的文件路径,也可以是 PEM 编码的密钥字符串。

如果密钥使用密码加密,请使用 *certificate_password*。

可以使用 Jinja2 模板值设置此选项。

配置

certificate_password

字符串

用于解密由 *certificate* 或 *certificate_key* 指定的证书密钥的密码。

可以使用 Jinja2 模板值设置此选项。

配置

compose

字典

从 jinja2 表达式创建变量。

默认值: {}

connection_timeout

integer

在连接建立失败之前等待的超时时间(以秒为单位)。

可以使用 Jinja2 模板值设置此选项。

默认值: 5

配置

encrypt

boolean

是否需要对连接进行加密。

可以使用身份验证协议或通过 TLS 执行加密。

*auth_protocol* negotiatekerberosntlm 都支持通过 LDAP 进行加密,而 simple 不支持。

如果使用 auth_protocol=simple 通过 LDAP 而不使用 TLS,则必须将其设置为 False。由于未使用加密,因此所有流量都将是明文,应避免使用。

可以使用 Jinja2 模板值设置此选项。

选择

  • false

  • true ← (默认)

配置

filter

字符串

用于查询计算机对象的 LDAP 筛选器字符串。

默认情况下,这将与筛选器“(objectCategory=computer)”组合使用。使用 *filter_without_computer* 来覆盖此行为,并使 *filter* 成为唯一使用的筛选器。

filter_without_computer

boolean

在 microsoft.ad 1.3.0 中添加

不会将 *filter* 值与默认筛选器“(objectCategory=computer)”组合使用。

在大多数情况下,这应为 false,但可以设置为 true 以使指定的 *filter* 值成为唯一使用的筛选器。

选择

  • false ← (默认)

  • true

groups

字典

根据 Jinja2 条件将主机添加到组。

默认值: {}

keyed_groups

list / elements=dictionary

根据变量的值将主机添加到组。

默认值: []

default_value

字符串

在 ansible-core 2.12 中添加

当主机变量的值为空字符串时的默认值。

此选项与 keyed_groups[].trailing_separator 互斥。

key

字符串

用于生成组的输入字典中的键。

parent_group

字符串

键控组的父组。

prefix

字符串

键控组名称将以此前缀开头。

默认值: ""

separator

字符串

用于构建键控组名称的分隔符。

默认值: "_"

trailing_separator

boolean

在 ansible-core 2.12 中添加

将此选项设置为 false,以在值为空字符串时省略主机变量之后的 keyed_groups[].separator

此选项与 keyed_groups[].default_value 互斥。

选择

  • false

  • true ← (默认)

leading_separator

boolean

在 ansible-core 2.11 中添加

keyed_groups 结合使用。

默认情况下,没有提供前缀或分隔符的键控组的名称将以一个下划线开头。

这是因为默认前缀为 "",默认分隔符为 "_"

如果未给出前缀,请将此选项设置为 false 以省略前导下划线(或其他分隔符)。

如果组名是从映射派生的,则分隔符仍用于连接项。

要完全不在组名中使用分隔符,请将键控组的分隔符设置为空字符串。

选择

  • false

  • true ← (默认)

password

字符串

用于进行身份验证的密码。

如果 *auth_protocol* 为 simple 并且未指定密码,则绑定将作为未经验证的绑定执行。

如果 *auth_protocol* 为 negotiatekerberosntlm 并且未指定密码,则它将尝试使用 *username* 指定的本地缓存凭据(如果可用)。

可以使用 Jinja2 模板值设置此选项。

配置

port

integer

用于连接的 LDAP 端口。

端口 389 用于 LDAP,端口 686 用于 LDAPS。

如果 tls_mode=ldaps,则默认为端口 636,否则默认为 389

可以使用 Jinja2 模板值设置此选项。

配置

search_base

字符串

用于查找计算机对象的 LDAP 搜索基本路径。

如果未指定,则默认为 Active Directory 服务器的 defaultNamingContext

如果搜索较大的 Active Directory 数据库,建议缩小搜索基本路径以加快查询速度。

search_scope

字符串

要执行的 LDAP 搜索的范围。

base 将仅搜索由 *search_base* 指定的当前路径或对象。 这通常对清单插件没有用处。

one_level 将仅搜索 *search_base* 中的直接子对象。

subtree 将搜索 *search_base* 中的直接子对象和任何嵌套对象。

选择

  • "base"

  • "one_level"

  • "subtree" ← (默认)

server

字符串

要连接的域控制器/服务器。

如果未指定,则将从当前的 krb5.conf default_realm 设置以及 SRV DNS 查找中派生服务器。

有关更多信息,请参阅 服务器查找

可以使用 Jinja2 模板值设置此选项。

配置

strict

boolean

如果 yes,则使无效条目成为致命错误,否则跳过并继续。

由于可以在表达式中使用事实,因此它们可能并不总是可用,默认情况下我们会忽略这些错误。

选择

  • false ← (默认)

  • true

tls_mode

字符串

要使用的 TLS 操作。

如果将显式 *port* 设置为 636,则此项默认为 ldaps

ldaps 将通过 LDAPS(端口 636)进行连接。

start_tls 将通过 LDAP(端口 389)进行连接,并在身份验证绑定之前执行 StartTLS 操作。

如果将要使用 TLS,建议使用 ldaps 而不是 start_tls

可以使用 Jinja2 模板值设置此选项。

选择

  • "ldaps"

  • "start_tls"

配置

use_extra_vars

boolean

在 ansible-core 2.11 中添加

将额外的变量合并到可用于组合的变量中(优先级最高)。

选择

  • false ← (默认)

  • true

配置

username

字符串

用于身份验证的用户名。

如果 auth_protocolsimple 且未指定用户名,则使用匿名身份验证。

如果 auth_protocolnegotiatekerberosntlm 且未指定用户名,则会尝试使用本地缓存的凭据(如果可用),例如由 kinit 检索的凭据。

可以使用 Jinja2 模板值设置此选项。

配置

备注

注意

  • 请参阅 LDAP 清单,了解有关如何使用此清单插件的更多详细信息。

  • 请参阅 LAPS,了解有关此插件如何检索 LAPS 密码信息的更多详细信息。

  • 此插件为技术预览版,模块选项可能会根据收到的反馈进行更改。

  • 除非在选项描述中另有说明,否则配置文件中指定的值将按原样使用。只有 LDAP 连接选项允许使用 Jinja2 模板。

  • 请参阅 LDAP 连接帮助,了解有关 LDAP 连接的更多信息。

示例

# Set in the file ending with microsoft.ad.ldap.yml or microsoft.ad.ldap.yaml
plugin: microsoft.ad.ldap


####################################################################
#                        Connection Options                        #
#                                                                  #
# These options control how the plugin connects to the LDAP server #
####################################################################

# Connects to ldap://dc01.domain.com:389
server: dc01.domain.com
port: 389

# Connects to ldaps://dc01.domain.com:636
server: dc01.domain.com
tls_mode: ldaps

# Connects to the global catalog
# ldap://dc01.domain.com:3268
server: dc01.domain.com
port: 3268

# Provides explicit user, will use the current Kerberos ticket if no credential
# is provided.
username: [email protected]
password: Password123!

# Only allow Kerberos authentication.
auth_protocol: kerberos

# Verify LDAPS CA chain with custom CA chain.
tls_mode: ldaps
ca_cert: /home/user/certs/ldap.pem

# The username and password can be retrieved using a template with a lookup.
# Other connection options can also be set this way, the option description
# tells you whether it can be set to a template.
username: '{{ lookup("ansible.builtin.env", "LDAP_USERNAME") }}'
password: '{{ lookup("ansible.builtin.env", "LDAP_PASSWORD") }}'


##############################################
#               Search Options               #
#                                            #
# These options control the searching rules  #
##############################################

# Search for computer accounts in the Workshop OU.
search_base: OU=Workshop A,DC=domain,DC=com

# Filter the computer accounts returned for only ones with the dNSDomainName
# attribute set.
filter: (dNSDomainName=*)

# Filter computer accounts returned for ones starting with PROD and with the
# LAPS password set.
filter: (&(sAMAccountName=PROD*)(ms-Mcs-AdmPwd=*))

# See documentation for more details
attributes:
  sAMAccountName:
    sam_account_name:
  objectSid:
    computer_sid:
  pwdLastSet:
    password_last_set: this | microsoft.ad.as_datetime
  comment:
    host_comment
  memberOf:
    # Gets the value (1) of the first RDN (0) of each memberOf instance (this).
    # For example 'CN=Domain Admins,CN=Users,DC=domain,DC=test'
    # will be returned as just 'Domain Admins'
    computer_membership: this | microsoft.ad.parse_dn | map(attribute="0.1")
  location:


############################################################################
#                             LAPS Integration                             #
#                                                                          #
# Examples on how to use the new Windows LAPS values as connection options #
############################################################################

attributes:
  # msLAPS-Password is used if no encryption has been configured.
  # Currently an encrypted LAPS password is not supported.
  msLAPS-Password:
    ansible_user: (this | from_json).n
    ansible_password: (this | from_json).p

  # msLAPS-EncryptedPassword is used if encryption has been configured.
  # If the Python dpapi-ng library is installed the `this`` value will
  # contain the entry `value` which is the decrypted value. The ``info``
  # entry will contain the reason why the value could not be decrypted.
  msLAPS-EncryptedPassword:
    ansible_user: (this.value | from_json).n
    ansible_password: (this.value | from_json).p

  # ms-Mcs-AdmPwd is used for Legacy LAPS and stores just the password.
  # The username needs to be hardcoded as a string value for this template.
  ms-Mcs-AdmPwd:
    ansible_user: '"Administrator"'
    ansible_password: this


#####################################################################
#                        Constructed Options                        #
#                                                                   #
# These options control the constructed values like vars and groups #
#####################################################################

# Build composed host variables. Requires attributes to be set in the
# attributes option to be referenced here.
compose:
  host_var: computer_sid

# Conditionals that adds found hosts to the groups specified.
groups:
  # Adds all hosts to the windows group
  windows: true

  # Uses the memberOf fact documented above to place the host in the production
  # group if it's a member of that group
  production: '"Production Group" in computer_membership'

# Adds the host to a group site_{{ location }} with the default group of
# site_unknown if the location isn't defined
keyed_groups:
  - key: location | default(omit)
    prefix: site
    default_value: unknown

作者

  • Jordan Borean (@jborean93)

提示

每个条目类型的配置条目都具有从低到高的优先级顺序。例如,列表中较低的变量将覆盖列表中较高的变量。