ansible管理windows实践
一、前言
近期打算搞搞自动部署,因为是windows服务器,一些工具和系统支持都不是太好。最后发现ansible比较火,最重要的是他支持windows。本文主要就ansible 在windows使用环境搭建过程分享。
二、Ansible简介
(1)、连接插件connection plugins:负责和被监控端实现通信;
(2)、host inventory:指定操作的主机,是一个配置文件里面定义监控的主机;
(3)、各种模块核心模块、command模块、自定义模块;
(4)、借助于插件完成记录日志邮件等功能;
(5)、playbook:剧本执行多个任务时,非必需可以让节点一次性运行多个任务。
三、Windows下Ansible工作模式
Ansible 从1.7+版本开始支持Windows,但前提是管理机必须为Linux系统,远程主机的通信方式也由SSH变更为PowerShell,同时管理机必须预安装Python的Winrm模块,方可和远程Windows主机正常通信,但PowerShell需3.0+版本且Management Framework 3.0+版本,实测Windows 7 SP1和Windows Server 2008 R2及以上版本系统经简单配置可正常与Ansible通信。简单总结如下:
(1) 管理机必须为Linux系统且需预安装Python Winrm模块
(2) 底层通信基于PowerShell,版本为3.0+,Management Framework版本为3.0+
(3) 远程主机开启Winrm服务
四、Ansible管理机部署安装
(1). 对管理主机的要求
目前,只要机器上安装了 Python 2.6 或 Python 2.7 (windows系统不可以做控制主机),都可以运行Ansible.
主机的系统可以是 Red Hat, Debian, CentOS, OS X, BSD的各种版本,等等.
(2) 从源码运行
从项目的checkout中可以很容易运行Ansible,Ansible的运行不要求root权限,也不依赖于其他软件,不要求运行后台进程,也不需要设置数据库.因此我们社区的许多用户一直使用Ansible的开发版本,这样可以利用最新的功能特性,也方便对项目做贡献.因为不需要安装任何东西,跟进Ansible的开发版相对于其他开源项目要容易很多.
从源码安装的步骤
$ git clone git://github.com/ansible/ansible.git --recursive
$ cd ./ansible
使用 Bash:
$ source ./hacking/env-setup
如果没有安装pip, 请先安装对应于你的Python版本的pip:
$ sudo easy_install pip
以下的Python模块也需要安装:
$ sudo pip install paramiko PyYAML Jinja2 httplib2 six
一旦运行env-setup脚本,就意味着Ansible从源码中运行起来了.默认的inventory文件是 /etc/ansible/hosts。
配置hosts文件:
$ vim /etc/ansible/hosts
[windows]
192.168.1.105 ansible_ssh_user="Administrator" ansible_ssh_pass="" ansible_ssh_port= ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore
192.168.1.105是windows服务器的IP。
至此,服务端配置完毕。
和Linux发版版稍有区别,远程主机系统如为Windows需预先如下配置:
- 安装Framework 3.0+
- 更改powershell策略为remotesigned
- 升级PowerShell至3.0+
- 设置Windows远端管理,英文全称WS-Management(WinRM)
(1)安装Framework 3.0+
下载链接为:http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_x86_x64.exe。 下载至本地后双击左键安装即可,期间可能会多次重启,电脑需正常连接Internet。
(2)更改powershell策略为remotesigned

(3)升级PowerShell至3.0+
Window 7和Windows Server 2008 R2默认安装的有PowerShell,但版本号一般为2.0版本,所以我们需升级至3.0+,如下图中数字1部分表示PowerShell版本过低需3.0+版本,数字2部分表示当前PowerShell版本为2.0。

# 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
upgrade_to_ps3.ps1


(4)设置Windows远端管理(WS-Management,WinRM)
winrm service 默认都是未启用的状态,先查看状态;如无返回信息,则是没有启动;
winrm enumerate winrm/config/listener
针对winrm service 进行基础配置:
winrm quickconfig
查看winrm service listener:
winrm e winrm/config/listener
为winrm service 配置auth:
winrm set winrm/config/service/auth @{Basic="true"}
为winrm service 配置加密方式为允许非加密:
winrm set winrm/config/service @{AllowUnencrypted="true"}
好了,远程Windows主机配置到此结束,我们验证配置的是否有问题。
六、Windows下可用模块测试
ansible windows -m win_ping
传输/etc/passwd文件至远程F:\file\目录下
执行命令:
ansible windows -m win_copy -a 'src=/etc/passwd dest=F:\file\passwd'
返回结果:
192.168.1.105 | success >> {
"changed": true,
"checksum": "896d4c79f49b42ff24f93abc25c38bc1aa20afa0",
"operation": "file_copy",
"original_basename": "passwd",
"size": 2563
}
部分返回结果诠释:
- “operation”: “file_copy”—执行的操作为 file_copy;
- “original_basename”: “passwd”—文件名为 passwd;
- “size”: 2563—文件大小为 2563 bytes。
Playbook写法如下:
---
- name: windows module example
hosts: windows
tasks:
- name: Move file on remote Windows Server from one location to another
win_file: src=/etc/passwd dest=F:\file\passwd
删除F:\file\passwd
执行命令:
ansible windows -m win_file -a "path=F:\file\passwd state=absent"
返回结果:
192.168.1.105 | success >> {
"changed": true
}
七、总结
ansible管理windows实践的更多相关文章
- [转帖]Ansible管理windows集群
Ansible管理windows集群 http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+win ...
- 厉害—Ansible管理windows集群
最近公司新项目需要安装400+windows server 2012系统的工作站,想着怎么能像linux下运用ansible批量管理,linux就很简单了有ssh服务 但是下却没这么简单,但还是有办法 ...
- ansible管理windows (发送文件)
https://github.com/ansible/ansible/raw/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 环境: 服务 ...
- ansible管理windows主机
1. 在windows开启winrm winrm service 默认都是未启用的状态,先查看状态:如无返回信息,则是没有启动: winrm enumerate winrm/config/listen ...
- Ansible 管理Windows 受控端
- Ansible学习笔记(一):部署管理Windows机器遇到的一些坑
在给国盛通上海测试环境做Ansible管理Windows服务器的时候,遇到了一些坑,Google解决掉了,特此记录,坑用红色标记. 一.环境说明 1.Ansible管理主机 操作系统:CentOS 7 ...
- [转帖]Ansible批量远程管理Windows主机(部署与配置)
2018-09-12 12:04:42 https://blog.51cto.com/7424593/2174156 一.测试环境介绍 Ansible管理主机: 系统: CentOS6.8 IP ...
- Ansible 批量管理Windows Server服务器
Ansible批量管理Windows Server Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具, 它用Python写成,类似于saltstack和Puppe ...
- Ansible批量远程管理Windows主机(部署与配置)
一.测试环境介绍 Ansible管理主机: 系统: CentOS6.8 IP Addr: 172.16.10.22 Linux管理服务器需安装pip.pywinrm插件 Windows客户端主机: ...
随机推荐
- cef 下载地址
最新的CEF3源代码在:http://cefbuilds.com/CEF3的论坛:http://www.magpcss.org/ceforum/viewforum.php?f=5CEF3 C++开发环 ...
- Android动画中Interpolator 详解和演示
遇到一个项目需求,想让动画变得更活泼一点,于是想到了动画属性中的Interpolator,写了基本例子测试一下Android提供给我们现成的加速器的效果: 效果 代码中方法 xml中属性 越来越快 A ...
- Java Unsafe类
参考了这篇文章:http://blog.csdn.net/aesop_wubo/article/details/7537278 <JAVA并发编程学习笔记之Unsafe类> Unsafe开 ...
- 记事本中写c/c++程序在Windows下运行
1.在桌面新建一个1.c,内容例如以下: 2.打开cmd命令行窗体,进入CodeBlock安装文件夹下的:E:\Installed\CodeBlocks\MinGW.然后运行mingwvars. ...
- java中Volatile修饰符的含义
在java语言中:为了获得最佳速度,同意线程保存共享成员变量的私有拷贝.并且仅仅当线程进入或者离开同步代码块时才与共享成员变量的原始值进行对照. volatilekeyword的作用就是提示vm:对于 ...
- 前言(CSDN也有Markdown了,好开森)
实战出精华 在具体的C++网络编程中提升你的逼格 John Torjo Boost.Asio C++ 网络编程 Copyright © 2013 Packt Publishing 关于作者 做为一名权 ...
- C语言-回溯例2
组合问题 组合:从n个不同元素中取r个不重复的元素组成一个子集,而不考虑其元素的顺序,称为从n个中取r个的无重组合,例如OR = {1,2,3,4}, n = 4, r = 3则无重组合为: {1,2 ...
- 使用JDBC连接SQL Server
源文:http://bbs.bc-cn.net/dispbbs.asp?boardid=12&id=140292 整理:秋忆 接触Java或者JSP,难免会使用到数据库SQL Server 2 ...
- angular input file 上传文件
<body > <div ng-controller="fileCtrl"> <form ng-submit="submit(obj)&qu ...
- 记录-外挂recovery的制作(魅蓝note)
安卓的开源使其具有很强的可定制性,对于用户来说很具有可玩性.玩机一般来说就是解锁BootLoader刷入第三方recovery,利用第三方recovery刷第三方ROM,刷supersu获取root权 ...