我们正在寻找一位拥护者,一位愿意冒险进入 Ansible 自动化世界的 Windows 管理员。不,您不需要了解 Bash 脚本或如何在 Linux 终端中导航。您只需要所有管理员都渴望的:尽可能快地完成日常任务。
虽然 Windows 和 Ansible 的集成已经取得了长足的进步,但请注意,在目前以及最新的 Ansible 版本中,仍然需要 Linux 才能运行 Ansible 并管理您的远程 Windows 节点。但是,您不必担心,因为目前 Windows 有一个名为 WSL 或 Windows Subsystem for Linux 的 Linux 子系统。这里有帮助您安装 WSL 的说明。
如果您是一位没有 Linux 和 Ansible 经验的 Windows 管理员,那么入门并运行您的第一个 Ansible playbook 既不困难也不耗时。您可以通过使用 Ansible playbook 自动化部署服务或更改用户帐户等日常任务,对您组织的基础架构产生积极影响,提高其投资回报率 (ROI),并获得更多创新时间。
Ansible 可以为混合操作系统环境带来自动化,并提供了一种有效的方式来实现基础架构即代码 (IaC) 状态,而不会给您的管理员带来负担。Ansible 不是 System Center Configuration Manager (SCCM) 或 Chocolatey 的替代品;它是一个补充工具,允许您自动化您的软件提供的服务。
开始使用

从 Ansible 控制机远程连接到 Windows 服务器或客户端需要正确配置 Windows Remote Manager (WinRM)。幸运的是,Ansible 团队编写了一个 PowerShell 脚本 ConfigureRemotingForAnsible,它可以让您轻松地在您的开发或测试环境中开始使用 Ansible for Windows。该脚本在任何受支持的 Windows 服务器或客户端目标上配置 WinRM。
在 Windows PowerShell 上运行以下命令
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file
winrm enumerate winrm/config/Listener
Ansible 可以使用多种身份验证传输方案,包括 NTLM、Kerberos 和基本身份验证。在本教程中,我们将使用 Kerberos 身份验证方法(假设 Windows 服务器已注册到域)。
ConfigureRemotingForAnsible 脚本默认启用基本身份验证;要禁用基本身份验证并启用 Kerberos,请在命令提示符下运行以下命令
winrm set winrm/config/service/auth @{Basic="false"; Kerberos="true"}
设置您的控制机
我们的控制机运行的是 CentOS Linux release 7.5.1804(core);您也可以使用 RHEL 7 或 Fedora 19 或更高版本。要安装和配置控制机以管理 Windows 目标主机
- 在控制机上,使用以下命令安装 ansible 和 python2-winrm 软件包
yum install -y ansible python2-winrm
- 在控制机上,安装 Python Kerberos
yum install -y python-requests-kerberos
- 打开 /etc/krb5.conf 文件并编辑以下设置(使用您自己的域信息)
[realms] EXAMPLE.COM = { kdc = ad.example.com } [domain_realm] .example.com = EXAMPLE.COM
设置您的清单
Ansible 的 清单 包含所有可以由 Ansible 主机(也称为 Ansible 控制器)管理的终端节点或目标主机。您可以将清单配置为静态或动态;在本教程中,我们将配置静态清单。
虽然您可以在 /etc/ansible/hosts 中配置静态清单,但最佳实践是创建一个不同的清单文件,可以根据需要进行编辑;例如,如果您需要将静态清单更改为动态清单以适应基础架构更改。清单将按组配置。组以方括号 ([ ]) 开头,服务器集合代表该组(例如,[group])。
Ansible 清单可以采用多种形式:静态文件格式,例如 .ini、.yaml 和 .toml,以及来自脚本或插件的动态生成的清单。.ini 格式适用于小型、简单的清单,它可以像针对其运行的主机名列表一样简单。
Ansible 允许您通过在方括号内插入 <服务器组名称>:vars* 的名称,为 Ansible 主机文件中的每个组设置变量。下面的代码在 Ansible 控制机上设置 WinRM 的变量。
[linux-server]
linux-web.example.com
[linux-server:vars]
ansible_user=root
ansible_connection=ssh
[win-server]
windows-web
[windows:vars]
ansible_user=Administrator@example.com
ansible_connection=winrm
ansible_port=5986
ansible_winrm_transport=kerberos
ansible_winrm_server_cert_validation=ignore
Ansible 可以通过运行临时命令来检查属于 linux-server 或 win-server 组的所有服务器的 ping 状态,例如
ansible linux-server -i (some local path)/(inventory file) -m ping
或
ansible win-server -m win_ping --ask-pass
配置 IIS 和 Apache Web 应用程序
创建 Ansible playbook
当您需要执行比 ping 目标主机或获取主机正常运行时间信息更复杂的任务时,使用 Ansible 临时命令可能很乏味且耗时。Ansible playbook 是 YAML 格式的文件,其中包含一组配置和任务,这些配置和任务在 Ansible Windows 或 Linux 目标主机上实现最终状态。上面的临时命令可以变成
- hosts: win-server
gather_facts: no
tasks:
- name: Checking connection to {{ inventory_hostname }}
win_ping:
要运行 Ansible playbook,请使用 ansible-playbook 命令。结果显示所有成功和失败的任务。

要更改 Ansible 目标主机名,首先显示当前的 Ansible 目标主机名
ansible win-server -m setup --ask-pass | grep ansible_hostname

向 playbook 添加两个任务,以将主机名更改为清单文件中设置的主机名,并重新启动 Ansible 目标主机
- hosts: win-server
gather_facts: yes
tasks:
- name: Change current hostname to {{ inventory_hostname }}
win_hostname:
name: "{{ inventory_hostname }}"
register: winhostname
- name: Reboot {{ inventory_hostname }}
win_reboot:
when: winhostname.reboot_required
Windows:配置 IIS 网站
要创建 Internet Information Services (IIS) 网站
- 必须安装并运行 IIS Windows 功能
- IIS 网页文档必须可供 IIS 服务使用
- 必须配置 Windows 防火墙以允许来自端口 80 的入站流量
下面的 Ansible playbook 使用 Ansible Windows 模块在 Ansible 目标主机上生成正在运行且已配置的 IIS Web 服务器。它处理以下配置和任务
- hosts: 命名 playbook 将针对其运行的 Ansible 目标主机组
- gather_facts: 查询目标系统的设置列表,例如主机名、IP 地址、MAC 地址、完全限定域名 (FQDN) 等。
- tasks: 按顺序执行 Ansible 任务列表
- 检查 Ansible 控制机和目标主机之间的连接,以确保网络连接
- 将目标主机的当前主机名更改为 Ansible 清单主机名
- 在 Ansible 目标主机上安装 IIS 功能
- 将 Web 索引文件从 Ansible 控制机复制到 Ansible 目标主机上的 IIS Web 主目录
- 在目标主机防火墙上打开端口 80 以允许入站流量
- 测试目标主机上的 IIS 服务器是否正在运行且配置正确
- hosts: win-server
gather_facts: yes
tasks:
- name: Install IIS feature on {{ inventory_hostname }}
win_feature:
name: Web-Server
state: present
restart: no
include_sub_features: yes
include_management_tools: no
- name: Move local web index file to {{ inventory_hostname }}
win_copy:
src: files/index.html
dest: 'C:\\inetpub\\wwwroot\\index.html'
- name: Open firewall port 80 for the IIS web server on {{ inventory_hostname }}
win_firewall_rule:
name: "{{ inventory_hostname }}_80"
enable: yes
state: present
localport: 80
action: Allow
direction: In
protocol: Tcp
- name: Testing IIS is properly configured and running on {{ inventory_hostname }}
win_uri:
url: "http://{{ ansible_fqdn }}"
return_content: yes
register: result
failed_when: "'Yay! Simple Ansible deployed IIS server ...' not in result.content"
Linux:配置 Apache 网页
如果您被要求成为混合管理员,您可能会认为您只需负责处理 Windows 环境。但是,随着管理员角色转向 DevOps,您可能会被要求接触一两台 Linux 服务器来支持运行 Apache 的主机。以下是一些帮助您的说明。
要创建 Apache 网页
- 必须安装 Apache
- 必须启用并启动 Apache;这可以在 RHEL 中使用 systemd 完成
- systemctl enable httpd
- systemctl start httpd
- 进行 firewalld 更改以确保 HTTP/HTTPS 端口以及端口添加到防火墙规则中
- 命令:firewalld-cmd --permanent -add-service={http,https}
- 命令:firewall-cmd --permanent -add-port={80,443}
- 运行以下 Ansible playbook 以安装和启用 Apache,而无需执行单个命令。它提供了并行部署 Apache 网页所需的配置,这意味着任务将根据它们的编写方式按顺序运行。任务顺序如下
- 使用 yum 模块安装 httpd 服务
- 使用 copy 模块创建 Web 内容
- 确保防火墙已启用并正在运行
- 确保防火墙允许使用 httpd 服务
- 确保 httpd 服务已启用并正在运行
- 测试连接网页配置
此 Ansible playbook 将部署 Apache Web 服务器
- hosts: webserver
become: true
gather_facts: no
tasks:
- name: install latest version of Apache
yum:
name: "(( item }}"
state: latest
with_items:
- httpd
- httpd-tools
- name: create web content file
copy:
content: "Yay! Simply deployed Apache webserver …"
dest: /var/www/html/index.html
- name: firewall enabled and running
service:
enabled: true
name: firewalld
state: started
- name: firewalld persists httpd service
firewalld:
immediate: yes
permanent: true
service: http
state: enabled
- name: httpd enabled and running
service:
name: httpd
state: started
enabled: true
加入派对
已经有大约 90 个模块可用,并且还在开发更多模块。Windows 模块有助于 Chocolatey,并且现在可以使用 Ansible 管理大部分 Windows 基础架构。这允许 Windows 管理员使用 Linux 管理员在历史悠久的 Linux 文化中执行的相同技术和实践。
Windows 领域正在不断发展,通过加入 Ansible 社区,您可以加入最大的自动化派对!
1 条评论