Ansible管理windows集群

http://www.cnblogs.com/Dev0ps/p/10026908.html

写的挺好的 我关注点还是不够好呢

最近公司新项目需要安装400+windows server 2012系统的工作站,想着怎么能像linux下运用ansible批量管理,linux就很简单了有ssh服务

但是下却没这么简单,但还是有办法那就是Powershell。

Ansible可用于管理Windows集群,不过管理节点需要部署在Linux机器上,而且需要预装python winrm模块。

同时,Windows机器上的powershell版本需要满足3.0+,且Management Framework也需要满足3.0+版本。

一、首先在Powershell窗口执行get-host命令查看版本

二、配置winrm服务

Windows上winrm服务默认是未启用的,使用如下命令可以查看状态。

1
winrm enumerate winrm/config/listener

使用如下命令配置winrm服务

1
2
3
winrm quickconfig
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'

至此,windows主机的配置就完成了,接下来我们配置linux管理节点进行验证。
三、Linux管理节点配置

1.然后需要使用pip安装pywinrm模块。如果没有安装pip,则先参考python分类博文安装并配置pip。

1
[root@cmdb-server ansible]# pip install "pywinrm>=0.2.2"

2.配置hosts文件

1
2
[wind]
192.168.247.151 ansible_ssh_user="Administrator" ansible_ssh_pass="NPS#2018" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

3.然后使用如下命令进行验证,我们看到,此时可以使用win_ping模块连到之前配置的windows主机上了。

1
2
3
4
5
[root@cmdb-server ansible]# ansible wind -m win_ping
192.168.247.151 | SUCCESS => {
    "changed": false,
    "ping""pong"
}

4.远程推送文件

1
2
3
4
5
6
7
8
9
10
[root@cmdb-server ansible]# ansible wind -m win_copy -a 'src=/etc/ansible/PreventCopy.jar dest=C:\'
192.168.247.151 | SUCCESS => {
    "changed": true,
    "checksum""c3da689273ec80f8072573b73dd87d3bc68e0395",
    "dest""'C:\\PreventCopy.jar'",
    "operation""file_copy",
    "original_basename""PreventCopy.jar",
    "size"62463090,
    "src""/etc/ansible/PreventCopy.jar"
}

5.删除远程文件

1
2
3
4
[root@cmdb-server ansible]# ansible wind -m win_file -a "path=C:\PreventCopy.jar state=absent"
192.168.247.151 | SUCCESS => {
    "changed": true
}

更多模块及详细功能介绍:https://docs.ansible.com/ansible/latest/modules/list_of_windows_modules.html除win开头的模块外,scripts,raw,slurp,setup模块在Windows 下也可正常使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Windows下可用模块虽不及Linux丰富,但基础功能均包括在内,以下几个模块为常用模块:
win_acl (E) —设置文件/目录属主属组权限;
win_copy—拷贝文件到远程Windows主机;
win_file —创建,删除文件或目录;
win_lineinfile—匹配替换文件内容;
win_package (E) —安装/卸载本地或网络软件包;
win_ping —Windows系统下的ping模块,常用来测试主机是否存活;
win_service—管理Windows Services服务;
win_user —管理Windows本地用户。
 
window的通信检测为:ansible testwin -m win_ping
 
复制文件到window:
ansible windows -m win_copy -"src=/etc/passwd dest=E:filepasswd"
 
删除文件:
ansible windows -m win_file -"path=E:filepasswd state=absent"
 
新增用户:
ansible windows -m win_user -"name=stanley password=magedu@123 groups=Administrators"
 
重启服务:
ansible windows -m win_service -"name=spooler state=restarted"
 
获取window主机信息:
ansible windows -m setup
 
执行ps脚本:
ansible windows -m script -"E://test.ps1"
 
获取IP地址:
ansible windows -m win_command -"ipconfig"
 
查看文件状态:
ansible windows -m win_stat -"path='C://Windows/win.ini'"
 
移动文件:
ansible windows -m raw -"cmd /c 'move /y d:\issue c:\issue'"
 
创建文件夹:
ansible windows -m raw -"mkdir d:\tst"
 
重启:
ansible windows -m win_reboot
 
结束程序:
ansible windows-m raw -"taskkill /F /IM QQ.exe /T"
 
如果window主机传回来的中文是乱码,则修改ansible控制机上的python编码:
sed -"s#tdout_buffer.append(stdout)#tdout_buffer.append(stdout.decode('gbk').encode('utf-8'))#g" /usr/lib/python2.6/site-packages/winrm/protocol.py
sed -"s#stderr_buffer.append(stderr)#stderr_buffer.append(stderr.decode('gbk').encode('utf-8'))#g" /usr/lib/python2.6/site-packages/winrm/protocol.py
 
指定配置文件获取win网卡信息
ansible -i win_hosts windows -m raw -"ipconfig"
  
使用默认的配置文件获取网卡信息
ansible windows -m raw -"ipconfig"
  
拷贝文件到远程Windows主机
ansible windows -m win_copy -'src=/etc/passwd dest=F:\file\passwd'
  
ansible windows -m win_copy -"src=/usr/local/src/PayChannels20.35.zip dest=D:\Tomcat8630\webapps\PayChannels20.35.zip"
  
ansible windows -m win_copy -a "src=/usr/local/src/SupplierPay.zip dest=D:\SupplierPay.zip
  
将.zip解压到远程Windows主机,远程主机上必须存在需要解压的源文件
ansible windows -m win_unzip -a"creates=no src=D:\Tomcat8620\webapps\PayChannels-8630.zip dest=D:\Tomcat8620\webapps"
解压到D盘:ansible windows -m win_unzip -a"creates=no src=D:\SupplierPay.zip dest=D:"
  
  
重启远程windows主机的服务
ansible windows -m win_service -"name=Tomcat8630 state=restarted"
重启node.js(.bat命令)
ansible windows -m win_command -"chdir=D:\SupplierPay .\http_restart.bat"
 
执行win_command模块命令
启动redis
ansible windows -m win_command -"chdir=D:\Redis server-start.bat "
 
ansible win -m win_command -"chdir=C:\ a.bat "
ps:"chdir=C:\ a.bat " 之前有空格
 
删除文件或者目录
ansible windows -m win_file -"dest=D:\Tomcat8630\log\ state=absent"
ansible windows -m win_file -"dest=D:\Tomcat8630\logs\ state=absent"
  
创建用户
ansible windows -m win_user -"name=aa passwd=123456"
  
创建一个名叫user1的管理员用户,要求能够远程访问
ansible windows -m win_user -"name=user1 password=123 groups='Administrators,Remote Desktop Users'"
  
重启的第一种方式
ansible windows -m win_shell -"shutdown -r -t 1"
 
重启的第二种方式
ansible windows -m win_reboot
获取ip地址
 
ansible windows -m raw -"ipconfig"
 
获取身份
 
ansible windows -m win_command -"whoami"
 
移动文件
 
ansible windows -m raw -"cmd /c 'move /y D:\Ansible\product\DBFPlus.exe D:\Ansible\back\'"
 
移动文件目标端也需要制定到文件,而不能只制定到所在目录位置
 
ansible windows -m raw -"cmd /c 'move /y D:\Ansible\product D:\Ansible\back'"
 
移动文件夹源端和目标端目录都不能带反斜杠/。且将源的整个目录移到目的端目录里。
 
创建文件夹
 
ansible windows -m raw -"md d:\Ansible\justin"
 
删除文件或目录
 
ansible windows -m win_file -"path=d:\Ansible\justin state=absent"
 
结束某程序
 
ansible windows -m raw -"taskkill /F /IM snmp.exe /T"
 
文件传输
 
ansible windows -m win_copy -'src=/app/svn/127_Client/118919/zjcfg.zip dest=D:\soft\'
 
目标路径不能含关键词ansible,否则提示无效路径,源使用反斜杠结果将递归传输目录下所有文件,源不一反斜杠结尾将整个目录传输到目标目录下。

[转帖]Ansible管理windows集群的更多相关文章

  1. 厉害—Ansible管理windows集群

    最近公司新项目需要安装400+windows server 2012系统的工作站,想着怎么能像linux下运用ansible批量管理,linux就很简单了有ssh服务 但是下却没这么简单,但还是有办法 ...

  2. 使用Chef管理windows集群

    但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半 ...

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

    但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半 ...

  4. 如何在本地数据中心安装Service Fabric for Windows集群

    概述 首先本文只是对官方文档(中文,英文)的一个提炼,详细的安装说明还请仔细阅读官方文档. 虽然Service Fabric的官方名称往往被加上Azure,但是实际上(估计很多人不知道)Service ...

  5. windows集群简单介绍

    windows集群简单介绍仔细看过以前网友发表的一些文章,总觉得对windows集群没有详细介绍,我也是借花献佛,引用了一些技术性文档.目前应用最为广泛的集群计算技术可以分为三大类:高可用性集群技术. ...

  6. 将自己写的windows服务加入到windows集群中

    最近发现windows集群能进行很多自定义,比如在集群中加入自己编写的服务. 能自定义的可不少,截个图: 本次演示中,只想用“通用服务”这个类型. 先列下步骤 编写一个记录时间的Windows服务,这 ...

  7. 使用Cloudera部署,管理Hadoop集群

    Hadoop系列之(三):使用Cloudera部署,管理Hadoop集群 http://www.cnblogs.com/ee900222/p/hadoop_3.html Hadoop系列之(一):Ha ...

  8. VLAN 模式下的 OpenStack 管理 vSphere 集群方案

    本文不合适转载,只用于自我学习. 关于为什么要用OpenStack 管理 vSphere 集群,原因可以有很多,特别是一些传统企业,VMware 的使用还是很普遍的,用 OpenStack 纳管至少会 ...

  9. 《Apache kafka实战》读书笔记-管理Kafka集群安全之ACL篇

    <Apache kafka实战>读书笔记-管理Kafka集群安全之ACL篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想必大家能看到这篇博客的小伙伴,估计你对kaf ...

随机推荐

  1. [小技巧]C#中如何为枚举类型添加描述方法

    背景 在我们的日常开发中,我们会经常使用枚举类型.有时我们只需要显示枚举的值或者枚举值对应名称, 但是在某些场景下,我们可能需要将枚举值显示为不同的字符串. 例: 当前我们有如下枚举Level pub ...

  2. 【Android Studio安装部署系列】十四、Android studio移除工程和删除项目

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 Android Studio删除工程.项目的操作步骤. 移除工程 主要用于从最近打开的项目列表中移除.硬盘中还是存在这个项目的. F ...

  3. 1.6部署到CentOS「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 安装.NET Core 官方安装地址: https://www.microsoft.com/net/learn/d ...

  4. 校园生活app结对开发第一天

    今天刚开始开发,要安装android studio及熟悉软件操作

  5. 分享几个常见的CMD命令,可能会用的上

    win7快捷命令.CMD命令secpol.msc(设置开机启动提示信息)services.msc(打开服务)dxdiag(检查DirectX信息)winver(检查Windows版本)regedit( ...

  6. 【转载】Win10系统如何设置所有程序默认以管理员身份运行?

    在win10系统中有些用户发现一些程序只有使用管理员身份运行能才打开,这样的话就感觉会麻烦很多,那么有没有办法设置所有程序都默认以管理员身份运行,而无需我们一个一个右键点击再打开呢? 目前最好的方法就 ...

  7. 使用C# 操作存储过程,执行sql语句通用类

    如何使用C# 操作存储过程,执行sql语句? 闲话不多说,直接上代码:     /// <summary>    /// Sql通用类    /// </summary>    ...

  8. C++系列总结——继承

    前言 前面讲了封装,但封装只是隐藏了类内部实现.如果使用多态隐藏类本身的话,只有封装是不够的,还需要继承. 继承 通过封装.我们把一些相关的函数和变量包裹在了一起,这些函数和变量就叫做类的成员函数和成 ...

  9. SSM框架使用

    现在的主流框架有SSH和SSM. SSM:SpringMVC(controller)+Spring(管理各组件)+Mybaits SSH:Struct2(controller)+Spring(管理各组 ...

  10. flex页面布局练习--知乎

    采用flexbox弹性容器 在手机端进行页面布局 样本地址: http://tpl.zhuamimi.cn/%E6%89%8B%E6%9C%BA%E7%AB%AF%E9%A1%B5%E9%9D%A2- ...