模块中的 AD 身份验证
此集合中使用的模块的一个关键要求是能够在管理资源时对域控制器进行用户身份验证。本指南将介绍此场景可用的不同选项。
注意
本指南介绍在使用 Windows 主机上的模块时如何对域控制器进行身份验证。有关在 Linux 上运行插件时如何进行身份验证的信息,请参阅LDAP 身份验证。
隐式身份验证
第一个也是最简单的选项是在身份验证期间使用连接用户的现有凭据。这避免了在模块参数中指定用户名和密码,但这确实要求 Ansible 使用的连接方法支持凭据委托。例如,将 CredSSP 身份验证与 winrm
和 psrp
连接插件一起使用,或使用 Kerberos 委托。其他身份验证选项(如 NTLM)不支持凭据委托,因此无法与隐式身份验证一起使用。
测试隐式身份验证是否可用的唯一方法是运行模块并查看它是否有效。如果它不起作用,则错误很可能包含消息 Failed to contact the AD server
。
Become
如果隐式身份验证不可用,则可以使用 become
运行模块,该模块指定用于身份验证的用户名和密码。
- name: Use become with connection credentials
microsoft.ad.user:
name: MyUser
state: present
become: true
become_method: runas
become_flags: logon_type=new_credentials logon_flags=netcredentials_only
vars:
ansible_become_user: '{{ ansible_user }}'
ansible_become_pass: '{{ ansible_password }}'
runas
方法在 Windows 上使用,become_flags
将指定凭据仅应用于网络身份验证。ansible_become_user
和 ansible_become_pass
变量指定用于身份验证的用户名和密码。必须将这两个变量都设置为有效的用户名和密码,否则身份验证将失败。
也可以使用 SYSTEM
帐户进行 become。这将使模块在与目标 DC 进行身份验证时使用该主机的 AD 计算机帐户,而不是显式用户名和密码。AD 计算机帐户仍必须具有执行所请求操作所需的权限。
- name: Use machine account for authentication
microsoft.ad.user:
name: MyUser
state: present
become: true
become_method: runas
become_user: SYSTEM
显式凭据
最后一个选项是将用户名和密码指定为模块选项。可以通过两种方式完成此操作:使用 domain_username
和 domain_password
选项,或者使用 domain_credentials
选项。下面显示了这两种方法的示例。
- name: Use domain_username and domain_password
microsoft.ad.user:
name: MyUser
state: present
domain_username: '{{ ansible_user }}'
domain_password: '{{ ansible_password }}'
- name: Use domain_credentials
name: MyUser
state: present
domain_credentials:
- username: '{{ ansible_user }}'
password: '{{ ansible_password }}'
注意
domain_credentials
选项是在此集合的 1.6.0 版本中添加的。
不带 name
键的 domain_credentials
选项(如上面的示例中所示)将是用于与默认域控制器进行身份验证的凭据,就像 domain_username
和 domain_password
一样。不支持将这两个选项一起使用,并且会导致错误。
domain_credentials
选项还可用于指定特定于服务器的凭据。例如,在尝试查找 AD 对象的身份时
- name: Set member with lookup on different server
microsoft.ad.group:
name: MyGroup
state: present
members:
add:
- GroupOnDefaultDC
- name: GroupOnDefaultDC2
- name: GroupOnOtherDC
server: OtherDC
- name: GroupOnThirdDC
server: ThirdDC
domain_credentials:
- username: UserForDefaultDC
password: PasswordForDefaultDC
- name: OtherDC
username: UserForOtherDC
password: PasswordForOtherDC
在上面的情况下,有三个成员被添加到组中
GroupOnDefaultDC
- 将使用UserForDefaultDC
和PasswordForDefaultDC
在默认域控制器上查找GroupOnDefaultDC2
- 与上述相同,只是指定为字典GroupOnOtherDC
- 将使用UserForOtherDC
和PasswordForOtherDC
在OtherDC
上查找GroupOnThirdDC
- 将使用隐式用户身份验证上下文在ThirdDC
上查找
server
的值必须对应于 domain_credentials
中的 name
条目。如果未在 domain_credentials
中指定服务器,则模块将默认使用 domain_username/domain_password
或隐式用户身份验证。
注意
在 domain_credentials
中,默认(没有 name
键)条目仅用于没有显式设置服务器的查找。 domain_username
和 domain_password
凭据将用于所有连接,除非在 domain_credentials
中存在显式的服务器条目。