共计 1853 个字符,预计需要花费 5 分钟才能阅读完成。
介绍:
Ansible 是一个开源自动化工具,用于管理各种配置和应用程序部署。它既可以配置类 Unix 系统,也可以配置 Windows 系统。
主服务器要求:
- python3-pip
- pywinrm(python 包)
Windows 要求:
- Powershell 3.0 或更高版本
- 至少安装.NET 4.0
配置过程:
环境介绍:
角色 | 发行版 | IP 地址 |
---|---|---|
Ansible control | RHEL 9.1 | 192.168.0.201 |
Windows Node | Windows10 Enterprise | 192.168.0.203 |
将受控 Windows 计算机以管理员身份打开 Windows 中的“PowerShell”配置 WinRM 服务,配置服务参考 Windows WinRM 服务配置 一文。
注意:
- 生产环境中不推荐使用未加密通信,应配置 HTTPS 并安装适当的证书。
- 确保防火墙允许来自 Ansible 主控端的 WinRM 通信,通常需要开放 TCP 5985(HTTP)或 5986(HTTPS)端口。
在 Ansible inventory 文件中定义 Windows 主机,更新或创建 inventory
正确的配置如下:
[windows]
192.168.0.203 ansible_user="nnkin" ansible_password="Windows1@#" ansible_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
其中 “nnkin” 为 node 计算机用户名,”Windows1@#” 为 node 计算机密码,ansible_connection 设置为 winrm 表明使用 WinRM 进行连接,ansible_winrm_transport 可以设置为 basic(基本身份验证)或其他认证方式,如 credssp。
错误的配置:
网上一些文章五花八门,简直是误人子弟,让你配置成这下面样:
[windows]
192.168.0.203 ansible_ssh_user="nnkin" ansible_ssh_pass="Windows1@#" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
如果按照错误的配置你将会遇到如下几个报错
- 第一种
192.168.0.203 | UNREACHABLE! => {
"changed": false,
"msg": "ssl: auth method ssl requires a username",
"unreachable": true
这个错误的原因是:ansible_ssh_user 应该变成 ansible_user
- 第二种
192.168.0.203 | UNREACHABLE! => {
"changed": false,
"msg": "plaintext: auth method plaintext requires a password",
"unreachable": true
}
这个错误的原因是:ansible_ssh_pass 应该变成 ansible_password
- 第三种
192.168.0.203 | UNREACHABLE! => {
"changed": false,
"msg": "ssl: HTTPSConnectionPool(host='192.168.0.203', port=5986): Max retries exceedused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f3417552d00> new connection: [Errno 111] 拒绝连接 '))",
"unreachable": true
}
这个错误的原因是:ansible_ssh_port 应该变成 ansible_port
验证:
编写一个 Ansible Playbook 来管理 Windows 主机,例如一个简单的 ping 测试任务:
play book 剧本如下:
---
- name: test ping
hosts: windows
gather_facts: yes
tasks:
- name: test connection
win_ping:
执行 Playbook 剧本:
ansible-playbook win_ping.yml -i inventory
或者直接使用 shell 模块来验证:
ansible -i inventory windows -m win_ping