Ansible是一款轻量级的开源的自动化运维工具,支持linux和windows(只支持client,而且部分模块),利用Ansible能够简单批量的配置系统,安装软件,或者更高级的运维任务(好比滚动升级)。php
Ansible之类的运维工具对运维工做进行抽象及规范,可以极大的下降运维难度。本文只是为了演示如何经过ansible 的各模块对windows进行传输文件,管理帐号,执行脚本等批量自动化管理工做html
类型 | 系统 | ip |
---|---|---|
Server | Ubuntu Server 16.04.5 LTS X64 | 192.168.0.22 |
Client | Windows Server 2008 R2 SP1 | 192.168.0.23 |
注意:python
若是是实验目的,建议用Vmware,而且在关键操做时备份快照(好比,刚装完环境,升级完PS和.Net后),这样可以及时,干净的还原现场,节省每次重装系统致使的时间浪费linux
Ansible 只支持Powershell 4.0及以上(用3.0会报 Process is terminated due to StackOverflowException.),因此要求最低要求Win7,或者Win server 2008,详见 Ansible Doc host requirementsgit
官方文档要求升级至ps3.0便可,可是实验发现,3.0会报错程序员
Win+R
-> PowerShell
打开PowerShellgithub
输入 $PSVersionTable
查看 PSVersion
确保大于等于4.0(PowerShell 4.0),以及 CLRVersion
大于等于4.0(.NET Framework 4.0) ,若是版本太低,则执行下面代码,直接复制到PowerShell 执行便可,建议使用5.1(参考 hotfixv4.trafficmanager.net dont work 和 安装和配置 WMF 5.1)shell
注意:windows
注意用户名密码改为管理员的用户名密码bash
确保可以访问外网
PowerShell以管理员模式打开
PS3不能直升PS5,须要卸载PS3或者保存 PSModulePath
安装PS5须要先打最新SP补丁
PS5要求 .NET Framework 不低于 4.5.2
安装成功后会自动重启服务器,注意别影响其余服务
$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
复制代码
重启后,再次打开PS ,输入 $PSVersionTable
查看版本
## 配置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
复制代码
参考 Installing the Control Machine
$ 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 --version
ansible 2.7.7
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
$ pip install "pywinrm>=0.3.0"
复制代码
默认是 Inventory /etc/ansible/hosts
,此处改成手动指定,Ansible的Inventory支持ini格式和yaml格式,本文采用yaml格式
$ mkdir ansible
$ tee ansible/test_hosts.yaml <<-'EOF'
winserver:
hosts:
192.168.0.23:
vars:
ansible_user: Administrator
ansible_password: 密码
ansible_connection: winrm
ansible_port: 5986
ansible_winrm_server_cert_validation: ignore
EOF
复制代码
$ ansible -i ansible/test_hosts.yaml winserver -m win_ping
192.168.0.23 | SUCCESS => {
"changed": false,
"ping": "pong"
}
复制代码
若是报错
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
fatal: [192.168.0.23]: UNREACHABLE! => {"changed": false, "msg": "ssl: HTTPSConnectionPool(host='192.168.0.23', port=5986): Max retries exceeded with url: /wsman (Caused by SSLError(SSLError(\"bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)\",),))", "unreachable": true}
复制代码
降级一下 pip install "pywinrm==0.2.2"
更多实验,参见参考资料中的两篇51cto中的博文
根据 What modules are available? 绝大部分Module都是针对Linux编写的,大部分在windows下不能正常使用,有一些专用的windows module 使用ps编写的,可用在windows下使用,详细列表参见 Windows modules
官方文档中列举了Ansible windows常见问题,建议仔细阅读
Ansible配合playbook食用更佳,上述中的 ansible
命令是adhoc命令模式,相似在bash中手动执行命令,多用于简单,而且不须要复用场景,而 ansible-playbook
相似 bash脚本,可是更强大,适合复杂任务编排场景。
考虑后期出一个playbook的文章
推荐配合vscode的ansible 插件(github repo 地址 github.com/VSChina/vsc…)
效果以下所示,比idea的ansible强大不少。
山东济南的小伙伴欢迎投简历啊 加入咱们 , 一块儿搞事情。
长期招聘,Java程序员,大数据工程师,运维工程师。