[转帖]Ansible管理windows集群

时间:2023-02-22 09:08:11

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命令查看版本

[转帖]Ansible管理windows集群

二、配置winrm服务

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

1
winrm enumerate winrm/config/listener

[转帖]Ansible管理windows集群

使用如下命令配置winrm服务

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

[转帖]Ansible管理windows集群

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

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

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

[转帖]Ansible管理windows集群

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. 常用 Git 命令清单 转

    我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下 ...

  2. JAVA&&num;160&semi;SSH&&num;160&semi;框架介绍

    SSH 为 struts+spring+hibernate 的一个集成框架,是目前较流行的一种JAVA Web应用程序开源框架. Struts Struts是一个基于Sun J2EE平台的MVC框架, ...

  3. 【BZOJ】【4034】【HAOI2015】T2

    树链剖分/dfs序 树上单点修改+子树修改+链查询 其实用dfs序做也可以…… 其实树链剖分就是一个特殊的dfs序嘛= =所以树链剖分也可以搞子树-(Orz ZYF) 至于为什么……你看在做剖分的时候 ...

  4. mongo 初始配置

    连接mongo 时  在window的可视化工具 有时会出现这种无法找到表的情况 那么我们所需要的是什么?? 用客户端的命令行 查看是否能够真正连接成功 下载mongo window 并安装  这个网 ...

  5. Win32 GDI基础(笔记)

    1.GDI名字的意义 GDI Graphic Device Interface,我说不清和GUI有什么区别.可能一种针对设备,一种针对用户而言吧,反正以后都说GDI,也就是Windows的图形编程. ...

  6. 计算机视觉与模式识别代码合集第二版two

    Topic Name Reference code Image Segmentation Segmentation by Minimum Code Length AY Yang, J. Wright, ...

  7. 离线安装expect

    expect依赖tcl库 安装包: https://files.cnblogs.com/files/tozh/tcl8.4.11-src.tar.gz https://files.cnblogs.co ...

  8. RMAN命令DELETE操作总结

    本篇总结一下RMAN命令中的DELETE操作,DELETE命令用于删除RMAN备份记录以及相应的物理文件. To delete physical backups and copies as well ...

  9. bootsrtap带表格面板内容居中

    css中,添加 .table th, .table td { text-align: center; vertical-align: middle!important;}

  10. jQuery升级踩坑之路

    1.使用了被废弃的jQuery.browser属性 jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support . 在更 ...