ansible自动化管理windows系统实战

简介:

2000元阿里云代金券免费领取,2核4G云服务器仅664元/3年,新老用户都有优惠,立即抢购>>>


阿里云采购季(云主机223元/3年)活动入口:请点击进入>>>,


阿里云学生服务器(9.5元/月)购买入口:请点击进入>>>,

一、简述

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 
如图:
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 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 
如图:
ansible自动化管理windows系统实战
重启后查看powershell信息
ansible自动化管理windows系统实战

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、查看连接状态

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

2、获取Windows Facts

[root@localhost ~]# 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、远程执行命令
远程执行命令分为远程执行windows 原生自有命令通过raw 模块,如:"ipconfig "
远程执行ansible的win_command模块也可以执行命令,即ansible的扩展命令如"whoami"
默认是乱码,需要修改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
a、获取ip地址
[root@localhost ~]# 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
         .....省略.....

b、win_command模块远程获取身份
[root@localhost ansible]# ansible win7 -m win_command -a "whoami"
172.16.3.188 | SUCCESS | rc=0 >>
virtual_san\virtual

c、移动文件
[root@localhost ansible]# ansible win7 -m raw -a "cmd /c 'move /y d:\issue c:\issue'"
172.16.3.188 | SUCCESS | rc=0 >>
        1 file(s) moved

d、创建文件夹
[root@localhost ansible]# ansible win7 -m raw -a "mkdir d:\\tst"
172.16.3.188 | SUCCESS | rc=0 >>
    Directory: D:\
Mode                LastWriteTime     Length Name                              
----                -------------     ------ ----                              
d----         2018/1/25     16:44            tst   

e、删除文件或目录
[root@localhost ansible]# ansible win7 -m win_file -a "path=D:\1.txt state=absent"
172.16.3.188 | SUCCESS => {
    "attempts": 1, 
    "changed": true, 
    "failed": false
}

f、结束某程序
先通过 tasklist获取运行程序信息
[root@localhost ansible]# ansible win7 -m raw -a "taskkill /F /IM QQ.exe /T" 
172.16.3.188 | SUCCESS | rc=0 >>
SUCCESS: The process with PID 3504 (child process of PID 2328) has been terminated

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

[root@localhost ~]# 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"
}

5、添加用户

[root@localhost ansible]# ansible win7 -m win_user -a "name=san2 passwd=123.c0m groups=Administrators"
172.16.3.188 | 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
}

通过以上的实践我得知,要想通过ansible批量管理windows,前提是windows上要基于powershell配置好winrm服务;然后ansible通过模块和winrm服务远程指管理;这里只是简单的列举了向个常用管理模块;更多好用的模块请参考官方windows可用模块,包括自动配置等;










本文转自 dyc2005 51CTO博客,原文链接:http://blog.51cto.com/dyc2005/2064746,如需转载请自行联系原作者
目录
相关文章
|
10天前
|
运维 监控 安全
构建高效自动化运维系统:策略与实践
【4月更文挑战第29天】 在信息技术日新月异的今天,高效的运维管理已成为企业保持竞争力的关键因素。本文将探讨如何构建一个能够适应快速变化需求的自动化运维系统。通过深入分析自动化工具的选择、配置管理的最佳实践以及持续集成和部署的策略,我们旨在为读者提供一个清晰的框架来优化他们的运维流程。文章的核心在于提出一种结合了最新技术和思维模式的综合解决方案,以实现运维工作的最优化。
|
2天前
|
运维 负载均衡 持续交付
构建高效自动化运维体系:Ansible与Docker的协同实践
【5月更文挑战第7天】 在当今快速迭代的软件开发环境中,自动化运维成为确保部署效率和一致性的关键。本文将探讨如何通过结合Ansible和Docker技术,构建一个高效的自动化运维体系,旨在提升运维效率,减少人为错误,并实现持续集成与持续部署(CI/CD)的流程自动化。文章详细阐述了Ansible的配置管理机制、Docker容器化的优势,以及二者在实际运维场景中的结合应用,为读者提供一套可行的自动化运维解决方案。
|
2天前
|
运维 关系型数据库 MySQL
Ansible自动化运维工具主机清单配置
Ansible自动化运维工具主机清单配置
|
7天前
|
存储 运维 Kubernetes
构建高效自动化运维体系:Ansible与Kubernetes的协同实践
【5月更文挑战第2天】随着云计算和微服务架构的兴起,自动化运维成为保障系统稳定性与效率的关键。本文将深入探讨如何利用Ansible作为配置管理工具,结合Kubernetes容器编排能力,共同打造一个高效、可靠的自动化运维体系。通过剖析二者的整合策略及具体操作步骤,为读者提供一套提升运维效率、降低人为错误的实用解决方案。
|
7天前
|
网络协议 数据安全/隐私保护 iOS开发
苹果MacOS电脑使用内网穿透轻松远程桌面本地Windows系统电脑
苹果MacOS电脑使用内网穿透轻松远程桌面本地Windows系统电脑
|
8天前
|
机器学习/深度学习 运维 持续交付
构建高效自动化运维体系:Ansible与Docker的完美结合构建高效机器学习模型的五大技巧
【4月更文挑战第30天】 在当今快速发展的云计算和微服务架构时代,自动化运维已成为维持系统稳定性和提高效率的关键。本文将探讨如何通过结合Ansible和Docker技术构建一个高效的自动化运维体系。文章不仅介绍了Ansible与Docker的基本原理和优势,还详细阐述了如何整合这两种技术以简化部署流程、加强版本控制,并提高整体运维效率。通过案例分析,我们将展示这一组合在实际环境中的应用效果,以及它如何帮助企业实现持续集成和持续部署(CI/CD)的目标。 【4月更文挑战第30天】 在数据驱动的时代,构建一个高效的机器学习模型是获取洞察力和预测未来趋势的关键步骤。本文将分享五种实用的技巧,帮助数
|
9天前
|
运维 Kubernetes 持续交付
构建高效自动化运维系统:基于容器技术的持续集成与持续部署实践
【4月更文挑战第30天】 在快速发展的云计算时代,传统的运维模式已无法满足敏捷开发和快速迭代的需求。本文将介绍如何利用容器技术搭建一套高效自动化运维系统,实现软件的持续集成(CI)与持续部署(CD)。文章首先探讨了现代运维面临的挑战,接着详细阐述了容器技术的核心组件和工作原理,最后通过实际案例展示了如何整合这些组件来构建一个可靠、可扩展的自动化运维平台。
|
9天前
|
Windows
Windows系统下安装分布式事务组件Seata
Windows系统下安装分布式事务组件Seata
|
9天前
|
运维 监控 安全
构建高效自动化运维系统:策略与实践
【4月更文挑战第30天】 在现代IT基础设施管理中,自动化运维不再是可选项而是必需品。随着复杂性的增加和变更的频繁性,自动化可以提高效率、减少错误并释放人员专注于更有价值的任务。本文将探讨构建一个高效的自动化运维系统的关键环节,包括工具选择、流程设计以及监控和优化策略。通过案例分析和最佳实践分享,读者可以获得实施自动化运维的实用指导和启发。
|
9天前
|
存储 运维 Kubernetes
构建高效自动化运维体系:Ansible与Kubernetes的协同策略
【4月更文挑战第29天】 在当今快速迭代的软件开发环境中,自动化运维成为了确保部署效率和稳定性的关键。本文深入探讨了如何通过Ansible和Kubernetes的集成来实现高效的自动化配置管理与容器编排。文章首先介绍了Ansible与Kubernetes各自的特点及优势,随后详细阐述了它们在自动化运维中的互补作用,并提供了一个实用的集成方案。通过案例分析,验证了该策略在提高部署速度、降低人为错误以及增强系统可靠性方面的有效性。最后,讨论了实施过程中可能遇到的挑战和解决思路。

热门文章

最新文章

http://www.vxiaotou.com