ansible自动化管理windows系统实战

一、简述

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

c、升级PowerShell至3.0
保存以下脚本为upgrade_to_ps3.ps1

# Powershell script to upgrade a PowerShell 2.0 system to PowerShell 3.0
# based on http://occasionalutility.blogspot.com/2013/11/everyday-powershell-part-7-powershell.html
# 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

右击-->以管理员运行 稍等几分钟(具体时间看下载的速度,只要任务管理器中有powershell就说明还在下载安装),系统会自动重启升级安装powershell到3.0
如图:

重启后查看powershell信息

d、设置Windows远端管理(WS-Management,WinRM)服务
winrm 服务默认都是未启用的状态;注意以下操作在cmd中执行,而非powershell中
对winrm服务进行基础配置:

winrm quickconfig
C:\Users\san02>winrm quickconfig
已在此计算机上运行 WinRM 服务。
WinRM 没有设置成为了管理此计算机而允许对其进行远程访问。
必须进行以下更改:
在 HTTP://* 上创建 WinRM 侦听程序接受 WS-Man 对此机器上任意 IP 的请求。
启用 WinRM 防火墙异常。
执行这些更改吗[y/n]? y
WinRM 已经进行了更新,以用于远程管理。
在 HTTP://* 上创建 WinRM 侦听程序接受 WS-Man 对此机器上任意 IP 的请求。
WinRM 防火墙异常已启用。

查看winrm service listener
winrm e winrm/config/listener
C:\Users\san02>winrm e winrm/config/listener
Listener
    Address = *
    Transport = HTTP
    Port = 5985
    Hostname
    Enabled = true
    URLPrefix = wsman
    CertificateThumbprint
    ListeningOn = 127.0.0.1, 172.16.3.137, ::1, fe80::100:7f:fffe%13, fe80::5efe
:172.16.3.137%12, fe80::4865:97de:bb1f:877%11

配置auth 为true(默认为false)
winrm set winrm/config/service/auth @{Basic="true"}C:\Users\san02>winrm set winrm/config/service/auth @{Basic="true"}
Auth
    Basic = true
    Kerberos = true
    Negotiate = true
    Certificate = false
    CredSSP = false
    CbtHardeningLevel = Relaxed

配置允许非加密方式
winrm set winrm/config/service @{AllowUnencrypted="true"}
C:\Users\san02>winrm set winrm/config/service @{AllowUnencrypted="true"}
Service
    RootSDDL = O:NSG:BAD:P(A;;GA;;;BA)(A;;GR;;;IU)S:P(AU;FA;GA;;;WD)(AU;SA;GXGW;
;;WD)
    MaxConcurrentOperations = 4294967295
    MaxConcurrentOperationsPerUser = 1500
    EnumerationTimeoutms = 240000
    MaxConnections = 300
    MaxPacketRetrievalTimeSeconds = 120
    AllowUnencrypted = true
    Auth
        Basic = true
        ......以下省略.......

至此被管理端win7的环境配置完成!

四、测试Ansible管理windows

1、查看连接状态

[[email protected] ~]# ansible win7 -m win_ping
172.16.3.188 | SUCCESS => {
    "attempts": 1,
    "changed": false,
    "failed": false,
    "ping": "pong"
}

2、获取Windows Facts

[[email protected] ~]# ansible win7 -m   setup
172.16.3.188 | SUCCESS => {
    "ansible_facts": {
        "ansible_architecture": "32-bit",
        "ansible_bios_date": "12/01/2006",
        "ansible_bios_version": "VirtualBox",
        "ansible_date_time": {
            "date": "2018-01-24",
            "day": "24",
            "epoch": "1516816620.86637",
            "hour": "17",
            "iso8601": "2018-01-24T09:57:00Z",
            "iso8601_basic": "20180124T175700861308",
            "iso8601_basic_short": "20180124T175700",
            "iso8601_micro": "2018-01-24T09:57:00.861308Z",
            "minute": "57",
            "month": "01",
            "second": "00",
    ......以下省略.......

3、远程执行命令
默认是乱码,需要修改winrm模块文件

sed -i "s#tdout_buffer.append(stdout)#tdout_buffer.append(stdout.decode(‘gbk‘).encode(‘utf-8‘))#g" /usr/lib/python2.7/site-packages/winrm/protocol.py
sed -i "s#stderr_buffer.append(stderr)#stderr_buffer.append(stderr.decode(‘gbk‘).encode(‘utf-8‘))#g" /usr/lib/python2.7/site-packages/winrm/protocol.py
获取ip地址
[[email protected] ~]# ansible win7 -m raw -a "ipconfig"
172.16.3.188 | SUCCESS | rc=0 >>
Windows IP Configuration
Ethernet adapter 本地连接:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::c55d:90f1:8d60:5d97%11
   IPv4 Address. . . . . . . . . . . : 172.16.3.188
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::daae:90ff:fe02:9d81%11
                                       172.16.3.1

4、文件传输到win7被管理端
把/etc/issue文件复制到当前目录(也可以直接/etc/issue)再传送到目标主机D盘下(可以修改文件名)

[[email protected] ~]# ansible win7 -m  win_copy -a "src=issue dest=D:\issue"
172.16.3.188 | SUCCESS => {
    "attempts": 1,
    "changed": true,
    "checksum": "5c76e3b565c91e21bee303f15c728c71e6b39540",
    "dest": "D:\\issue",
    "failed": false,
    "operation": "file_copy",
    "original_basename": "issue",
    "size": 23,
    "src": "issue"
}

更多好用的模块请参考官方windows可用模块,包括自动配置等;

原文地址:http://blog.51cto.com/dyc2005/2064746

时间: 2024-12-10 10:13:56

ansible自动化管理windows系统实战的相关文章

【Absible学习】ansible管理windows系统

Ansible 从1.7+版本开始支持Windows,实测Windows 7 SP1和Windows Server 2008 R2及以上版本系统经简单配置可正常与Ansible通信.但需要满足以下几点:1.管理机必须是linux系统,且原装Python Winrm模块2.底层通信基于PowerShell,版本为3.0+,Management Framework版本为3.0+3.远程windows主机开启Winrm服务 被控制主机windows 安装Framework 3.0+下载链接为:http

《Ansible自动化运维:技术与最佳实践》图书已上架,欢迎大家阅读

本书由资深运维程师联手打造,通过大量实例,详细讲解Ansible这个自动化运维工具的基础原理和使用技巧:从基础的架构解析.安装配置,到典型应用案例分析,作者分享了自己在工作中的实战经验,为各类运维操作.运维开发人员提供了翔实的指南.本书主要内容包括:Ansible架构及安装,Ansible 组件.组件扩展.API,playbook详解,最佳实践案例分析,用ansible-vault保护敏感数据,Ansible与云计算的结合,部署Zabbix组件.Haproxy + LAMP架构,以及Ansibl

Ansible自动化运维工具-上

[Ansible特点] 1)Ansible与saltstack均是基于Python语言开发的 2)安装使用简单,基于不同插件和模块实现各种软件,平台,版本的管理以及支持虚拟容器多层级的部署 3)不需要安装客户端,ansible基于SSH远程管理,不需要为配置工作添加额外的支持: PS:很多认为Ansible工具执行效率慢,其原因是SSH服务慢,我们可以选择优化SSH连接速度以及Ansible加速模块 [Ansible自动化管理工具特点] #轻量级,更新时,需要在操作机上进行一次更新即可 #采用S

使用Chef管理windows集群 | 运维自动化工具

但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半天时间.虽然你编写了大量的shell(或python,perl)脚本来实现各种自动化场景,但最后会发现你又陷入了脚本的汪洋大海之中,管理和维护这么多的脚本的成本也不小.你需要一款基础设施自动化工具,希望它能具有以下功能. 1批量执行 这个不多说了吧,试想要为每一台机器打补丁的情形吧. 2任务编排 现

Ansible管理windows

Ansible管理windows 安装步骤 一.系统要求1,管理机必须是Linux系统且需预安装Python Winrm 模块(本次使用Linux版本是CentOS Linux release 7.3.1611 (Core)).2,底层通信认证一些基于Kerberos ,Windows使用的连接工具为PowerShell而非SSH,我这里测试的Windows版本是win7-32bit专业版.3,远程主机PowerShell版本为3.0+,Management Framework版本为3.0+.4

Python+Django+Ansible Playbook自动化运维项目实战

Python+Django+Ansible Playbook自动化运维项目实战网盘地址:https://pan.baidu.com/s/1bZ1Ju0mld3KLZawdxZ7m6Q 密码: 5k9x备用地址(腾讯微云):https://share.weiyun.com/5E7aUWv 密码:wzfdrn 本课程将带你从项目实践角度出发,围绕自动化资产扫描和发现.Ansible自动化任务执行的内容展开,让运维更简单.更高效,Hold住高薪! 适合人群:如果你是一位运维党,对Python运维自动化

Ansible自动化配置实战

Ansible自动化配置实战 1.Ansible基本概述 ? Ansible是一个配置管理系统configuration management system你只需要可以使用ssh访问你的服务器或设备就行. 2. Ansible能做什么 ? ? ansible可以帮助我们完成一些批量任务,或者完成一些需要经常重复的工作. ? 比如:同时在100台服务器上安装nginx服务,并在安装后启动服务. ? 比如:将某个文件一次性拷贝到100台服务器上. ? 比如:每当有新服务器加入工作环境时,你都要为新服

[转帖]Ansible批量远程管理Windows主机(部署与配置)

2018-09-12 12:04:42 https://blog.51cto.com/7424593/2174156 一.测试环境介绍 Ansible管理主机: 系统:   CentOS6.8 IP Addr: 172.16.10.22 Linux管理服务器需安装pip.pywinrm插件 Windows客户端主机: 系统:   Server2008R2 SP1 IP Addr: 172.16.10.23 Windows机器需要安装或升级powershell4.0以上版本,Server2008R

[转帖]Ansible管理windows集群

http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+windows server 2012系统的工作站,想着怎么能像linux下运用ansible批量管理,linux就很简单了有ssh服务 但是下却没这么简单,但还是有办法那就是Powershell. Ansible可用于管理Windows集群,不过管理节点需要部署在Linux机器上,而且需要预装python winrm模块. 同时,Windo