1、说明
日常系统自动化运维过程中难免会有windows系列服务器,就开源软件来说目前大多的对windows批量管理兼容性不太好;不像Linux系统便捷,但现实中确实有些业务需要跑在windows上;搜索查找折腾一番后,发现python开发的ansible(已经被redhat收购)有比较好的解决方案,通过一番折腾,整理出来,以备忘交流;
2、实验环境
服务器端:
CentOS7.4_x64 自带python 2.7.5 ip:172.16.3.167
源码安装ansible
被管理windows端:
win7sp1_x32 需要powershell 3.0+ ip:172.16.3.188 并开启winrm服务 开启防火墙规则
3、实验目标
能通过ansible 的各模块对windows进行传输文件,管理账号,执行脚本等批量自动化管理工作;
二、ansible配置
1、简介
Ansible 从1.7+版本开始支持Windows,但管理机必须为Linux系统,远程主机的通信方式也由Linux下的SSH变为PowerShell,管理机需要安装Python的pywinrm模块,但PowerShell需3.0+版本且Management Framework 3.0+版本,实测Windows 7 SP1和Windows Server 2008 R2及以上版本系统经简单配置可正常与Ansible通信。
2、环境准备
以下配置在CentOS7.4_x64下
安装pip及相关依赖
下载pip
#wget https://bootstrap.pypa.io/get-pip.py
#python get-pip.py
安装依赖
#pip install pywinrm paramiko PyYAML Jinja2 httplib2 six
3、源码安装ansible
# git clone git://github.com/ansible/ansible.git --recursive
#cd ./ansible
#source ./hacking/env-setup
运行了env-setup脚本,就意味着Ansible基于源码运行起来了.默认的inventory文件是 /etc/ansible/hosts
cat /etc/ansible/hosts
注:可以把这步添加到开机自启中;
[win7]
172.16.3.188 ansible_ssh_user="virtual" ansible_ssh_pass="myself." ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
注意上信息在一行;以空格隔开,[win7] 是这台主机的标题;下面的是ip和连接信息等;
以上ansible管理端已经配置好,被管理端win7还没有配置,相对来说稍稍麻烦点
三、被管理端win7配置
1、环境简介
和Linux稍有区别,被管理端系统如果是Windows系列时;需预先有以下配置:
安装Framework 3.0+ (有可能需要下载)
配置powershell策略为remotesigned (需要修改)
升级PowerShell至3.0+(win7默认是2.0)
设置Windows远端管理,英文全称WS-Management(WinRM)
2、环境配置
a、升级或安装Framework 4.5
如果Framework版不满足请至微软官方下载
b、修改powershell策略为remotesigned
如图:
c、升级PowerShell至3.0
保存以下脚本为upgrade_to_ps3.ps1
# Powershell script to upgrade a PowerShell 2.0 system to PowerShell 3.0
# based on
# some Ansible modules that may use Powershell 3 features, so systems may need
# to be upgraded. This may be used by a sample playbook. Refer to the windows
# documentation on docs.ansible.com for details.
# - hosts: windows
# tasks:
#
- script: upgrade_to_ps3.ps1
# Get version of OS
# 6.0 is 2008
# 6.1 is 2008 R2
# 6.2 is 2012
# 6.3 is 2012 R2
if ($PSVersionTable.psversion.Major -ge 3)
{
write-host "Powershell 3 Installed already; You don't need this"
Exit
}
$powershellpath = "C:\powershell"
function download-file
{
param ([string]$path, [string]$local)
$client = new-object system.net.WebClient
$client.Headers.Add("user-agent", "PowerShell")
$client.downloadfile($path, $local)
}
if (!(test-path $powershellpath))
{
New-Item -ItemType directory -Path $powershellpath
}
# .NET Framework 4.0 is necessary.
#if (($PSVersionTable.CLRVersion.Major) -lt 2)
#{
# $DownloadUrl = "http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe"
# $FileName = $DownLoadUrl.Split('/')[-1]
# download-file $downloadurl "$powershellpath\$filename"
# ."$powershellpath\$filename" /quiet /norestart
#}
#You may need to reboot after the .NET install if so just run the script again.
# If the Operating System is above 6.2, then you already have PowerShell Version > 3
if ([Environment]::OSVersion.Version.Major -gt 6)
{
write-host "OS is new; upgrade not needed."
Exit
}
$osminor = [environment]::OSVersion.Version.Minor
$architecture = $ENV:PROCESSOR_ARCHITECTURE
if ($architecture -eq "AMD64")
{
$architecture = "x64"
}
else
{
$architecture = "x86"
}
if ($osminor -eq 1)
{
$DownloadUrl = "http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.1-KB2506143-" + $architecture + ".msu"
}
elseif ($osminor -eq 0)
{
$DownloadUrl = "http://download.microsoft.com/download/E/7/6/E76850B8-DA6E-4FF5-8CCE-A24FC513FD16/Windows6.0-KB2506146-" + $architecture + ".msu"
}
else
{
# Nothing to do; In theory this point will never be reached.
Exit
}
$FileName = $DownLoadUrl.Split('/')[-1]
download-file $downloadurl "$powershellpath\$filename"
Start-Process -FilePath "$powershellpath\$filename" -ArgumentList /quiet
脚本来源于github upgrade_to_ps3.ps1
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。