ansible自动化打点windows系统实战

时间:2022-06-13 06:45:15

标签:

一、简述

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 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
如图:

ansible自动化打点windows系统实战

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