Active Directory 和 Ansible Tower
Active Directory & Ansible Tower
欢迎来到我们以 Windows 为中心的“入门”系列的第二期!
上次我们向您介绍了 Ansible 如何连接到 Windows 主机。我们之前也探讨了在对 LDAP 目录进行身份验证时登录 Ansible Tower 的方法。在这篇文章中,我们将介绍几种使用 Ansible 管理 Microsoft Active Directory 的方法。由于 AD 在许多 Windows 环境中发挥着作用,因此使用 Ansible 管理 Windows 可能包括对 Active Directory 域执行命令。
首先,设置协议
我们将使用 WinRM 连接到 Windows 主机,这意味着要确保 Ansible 或 Tower 知道这一点。Ansible Tower 中的机器凭据可以与变量一起创建和使用,但在终端中使用 Ansible 时,剧本应使用变量明确说明这一点。
--- - name: Your Windows Playbook hosts: win vars: ansible_ssh_user: administrator ansible_ssh_pass: ThisIsWhereStrongPassesGo ansible_connection: winrm ansible_winrm_server_cert_validation: ignore - tasks:
除了使用本地管理员帐户/密码外,还专门指定了 WinRM 连接方法。忽略证书验证的变量用于独立的非域主机,因为已加入域的实例应在域上验证证书。
域在哪里?
说到域,Ansible 可以创建新的域(如果不存在)。
在下面的示例中,Ansible(使用先前的设置)从服务器管理 win_feature
安装 AD 域服务功能,如果不存在域,则使用提供的 AD 安全模式密码 win_domain
创建新的 Active Directory 域。
- name: Install AD Services feature win_feature: name: AD-Domain-Services include_management_tools: yes include_sub_features: yes state: present register: result - name: Create new forest win_domain: dns_domain_name: tycho.local safe_mode_password: RememberTheCant! register: result - name: Reboot after creation win_reboot: msg: "Server config in progress; rebooting..." when: result.reboot_required
创建域后,服务器会向任何已登录的用户发送一条消息,通知服务器将重新启动,然后开始重新启动。虽然这不是一个生产级别的剧本,但它很好地展示了如何在几行代码中快速配置。
如果已经存在用于测试的域,则无需创建新的域,但可能需要将测试机器加入现有域。Ansible 同样可以通过几行代码来简化此任务。
- name: Configure DNS win_dns_client: adapter_names: "Ethernet 2" ipv4_addresses: - 10.0.0.1 - name: Promote to member win_domain_membership: dns_domain_name: tycho.local domain_admin_user: drummer@tycho.local domain_admin_password: WeNeed2Hydrate! state: domain register: domain_state - name: Reboot after joining win_reboot: msg: "Joining domain. Rebooting..." when: domain_state.reboot_required
步骤不言自明,确保机器能够与目录服务器通信(win_dns_client
),然后加入域(win_domain_membership
)。目标机器将重新启动以完成加入目录的操作。快速简便。
它能做什么?
使用 win_feature
管理角色类似于 Install-WindowsFeature
和 Add-WindowsFeature
Powershell cmdlet 的组合。如果您不熟悉要安装的功能的名称,请使用 Get-WindowsFeature
cmdlet 列出可安装的功能。
Windows 域模块(win_domain
、win_domain_controller
、win_domain_group
、win_domain_membership
、win_domain_user
)涵盖了针对 Active Directory 执行的常见任务。对于大多数 Windows 模块,应将具有适当权限的域帐户设置为机器凭据(使用 DOMAIN/User 或 User@domain.tld),就像您对本地帐户所做的那样。
总结
在这篇文章中,我们使用 WinRM 连接到 Windows 主机,让 Ansible 使用 win_feature
模块从服务器管理安装 AD 域服务功能(或者使用 win_domain
模块创建新的 Active Directory 域,如果不存在),确保机器能够使用 win_dns_client
与目录服务器通信,然后使用 win_domain_membership
将其加入域。
不要忘记确保您的 Windows 节点剧本通过明确声明 ansible_connection: winrm
(必需)以及 ansible_winrm_server_cert_validation: ignore
(如果您尚未将本地 CA 添加为受信任的 CA)来设置连接变量。如本文开头所示,这两个变量与 Ansible 剧本中 vars:
后的连接帐户变量一起使用。在 Ansible Tower 中,这些变量位于作业模板中。
现在您已经了解了如何使用 Ansible 与 Microsoft Active Directory!在下一篇文章中,我们将深入探讨 Ansible 和 Windows 提供的软件包管理功能!