天天都在用的自动化运维神器 Ansible,可你会用它批量管理 Windows 服务器吗?
Ansible 是一款开源的轻量级自动化运维工具,支持 Linux 和 Windows(只支持 Client,并且部分模块)。利用 Ansible 可以简单批量的配置系统、安装软件、或者更高级的运维任务(比如:滚动升级)。
Ansible 之类的运维工具对运维工作进行抽象及规范,能够极大的降低运维难度。本文将讲解如何通过 Ansible 对 Windows 进行管理,并通过文件传输、管理账号、执行脚本等例子演示批量自动化管理工作。
实验环境
| 类型 | 系统 | IP |
|---|---|---|
| Server | Ubuntu Server 16.04.5 LTS X64 | 192.168.0.22 |
| Client | Windows Server 2008 R2 SP1 | 192.168.0.23 |
注意:如果是实验目的,建议用 Vmware,并且在关键操作时备份快照(比如:刚装完环境,升级完 PS 和 .Net后)。这样能够及时干净的还原现场,节省每次重装系统导致的时间浪费。
Windows 被控端 (Ansible Client)
Ansible 只支持 Powershell 4.0 及以上,所以系统最低要求 Windows 7 或者 Win dows Server 2008。详见 Ansible Doc host requirements (http://t.cn/EafyAMW)
升级 PowerShell 和 .Net Framework
官方文档要求升级至 PowerShell 3.0 即可。但是实验发现,PowerShell 3.0 会报错。
Win+R -> PowerShell 打开 PowerShell
输入 $PSVersionTable 查看 PSVersion 确保大于等于 4.0 ( PowerShell 4.0 ),以及 CLRVersion 大于等于4.0 ( .NET Framework 4.0 ) 。
如果版本过低,则执行下面代码,直接复制到 PowerShell 执行即可。(建议使用 PowerShell 5.1)
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Upgrade-PowerShell.ps1"$file = "$env:temp\Upgrade-PowerShell.ps1"$username = "管理员用户名"$password = "管理员密码"(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force# PowerShell 版本,只能用 3.0, 4.0 和 5.1&$file -Version 5.1 -Username $username -Password $password -Verbose
重启后,再次打开 PowerShell,输入 $PSVersionTable 查看版本。更多参考可见:
a.) hotfixv4.trafficmanager.net dont work (http://t.cn/Eafk9dz)
b.) 安装和配置 WMF 5.1 (http://t.cn/Eafk8oA)
注意事项:
注意用户名密码改成管理员的用户名密码。
确保能够访问外网。
PowerShell 以管理员模式打开。
PowerShell 不能直升 PowerShell,需要卸载 PowerShell 3或者保存 PSModulePath。
安装 PowerShell 5 需要先打最新 SP 补丁。
PowerShell 5 要求 .NET Framework 不低于 4.5.2。
安装成功后会自动重启服务器,注意别影响其他服务。
安装 WinRM 并且配置监听
## 配置WinRM$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 set winrm/config/service/auth '@{Basic="true"}'winrm set winrm/config/service '@{AllowUnencrypted="true"}'
使用非加密连接有安全隐患,此处不作为实验重点。感兴趣的可以参考 PowerShell 配置 WinRM 使用 HTTPS。(http://t.cn/8Fhd55x)
Linux 主控端 (Ansible Server)
安装 Ansible 和 pywinrm
$ sudo apt-get update$ sudo apt-get install -y software-properties-common$ sudo apt-add-repository --yes --update ppa:ansible/ansible$ sudo apt-get install -y ansible$ ansible --versionansible 2.7.7config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.7/dist-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]$ pip install "pywinrm>=0.3.0"
更多安装方式可参考:Installing the Control Machine(http://t.cn/EafcmzU)。
配置 Inventory
默认是 Inventory /etc/ansible/hosts,此处改为手动指定。Ansible 的Inventory 支持 INI 格式和 YAML 格式,本文采用 YAML 格式。
mkdir ansibletee ansible/test_hosts.yaml <<-'EOF'winserver:hosts::vars:ansible_user: Administratoransible_password: 密码ansible_connection: winrmansible_winrm_transport: basicansible_port: 5985EOF
更多 Inventory 配置说明可参考:Working with Inventory(http://t.cn/EafMh8M)
探测主机是否存活
$ ansible -i ansible/test_hosts.yaml winserver -m win_ping192.168.0.23 | SUCCESS => {"changed": false,"ping": "pong"}
一些简单的使用实例
a.) 在被管理主机上远程执行命令
$ ansible winserver -m raw -a "ipconfig"192.168.0.23 | SUCCESS | rc=0 >>Windows IP ConfigurationEthernet adapter 本地连接:Connection-specific DNS Suffix . :Link-local IPv6 Address . . . . . : fe80::c55d:90f1:8d60:5d97%11IPv4 Address. . . . . . . . . . . : 192.168.0.23Subnet Mask . . . . . . . . . . . : 255.255.255.0Default Gateway . . . . . . . . . : fe80::daae:90ff:fe02:9d81%11192.168.0.1.....省略.....
b.) 传输文件到 Windows 被管理端
下面我们将演示使用 win_copy 模块 把 /etc/issue 文件传送到目标主机 D 盘下。
$ ansible winserver -m win_copy -a "src=/etc/issue dest=D:\issue"192.168.0.23 | SUCCESS => {"attempts": 1,"changed": true,"checksum": "5c76e3b565c91e21bee303f15c728c71e6b39540","dest": "D:\\issue","failed": false,"operation": "file_copy","original_basename": "issue","size": 23,"src": "issue"}
c.) 在被管理主机上增加一个用户
下面我们将演示使用 win_user 模块在被管理主机上创建一个新的用户。
$ ansible winserver -m win_user -a "name=san2 passwd=123.c0m groups=Administrators"192.168.0.23 | SUCCESS => {"account_disabled": false,"account_locked": false,"attempts": 1,"changed": true,"description": "","failed": false,"fullname": "san2","groups": [{"name": "Administrators","path": "WinNT://WORKGROUP/VIRTUAL_SAN/Administrators"}],"name": "san2","password_expired": true,"password_never_expires": false,"path": "WinNT://WORKGROUP/VIRTUAL_SAN/san2","sid": "S-1-5-21-2708087092-4192450616-382865091-1004","state": "present","user_cannot_change_password": false}
可用的 Windows 模块
绝大部分 Module 都是针对 Linux 编写的,大部分在 Windows 下不能正常使用。目前 Windows 的 Module 都是使用 PowerShell 编写的,可用的有:
各模块详细使用方法可参见:Windows Modules(http://t.cn/Eaf66BZ)。
如果你在使用过程中遇到问题,可参考官方的 Windows 常见问题处理(http://t.cn/EafX4ui)。
Ansible Playbook
Ansible 配合 Playbook 食用更佳,上述中的 Ansible 命令是 adhoc 命令模式,类似在 Bash 中手动执行命令。多用于简单,并且不需要复用场景,而 Ansible-Playbook 类似 Bash 脚本,功能更加强大,更适合复杂任务编排场景。
参考资料
官方文档
官方文档-windows指南
官方文档-windows指南-winrm步骤
Ansible批量远程管理Windows主机(部署与配置)
ansible自动化管理windows系统实战
安装和配置 WMF 5.1
hotfixv4.trafficmanager.net dont work
来源:家的博客
原文:http://t.cn/EafuE4h
题图:来自谷歌图片搜索
版权:本文版权归原作者所有
投稿:欢迎投稿,投稿邮箱: [email protected]
今日思想
这个世上,没有永远的朋友,也没有永远的敌人,只有永恒的利益。
—— Dr·大吉「极度分裂」
推荐阅读
