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. 程序员也想改 Lottie 动画?是的!

    一.前言 Hi,大家好,我是承香墨影! Lottie 是 Airbnb 开源的一套跨平台的完整的动画效果解决方案,用过都说好.完全解耦开发人员和设计师,让设计师设计的动画,在程序中无缝还原,真是一旦拿 ...

  2. 利用Bootstrap Paginator插件和KnockoutJS完成分页功能

    在最近一个项目中,需要结合一堆条件查询并对查询的结果数据完成一个简单分页功能,可是做着做着,自己的思路越来越模糊,做到心态崩溃!!! 哈哈,特此花点时间重新总结,并从最简单的分页,然后向多条件查询分页 ...

  3. springboot~jpa个性化数据操作接口

    jap是个全能仓储 jap把很多数据库访问都封装了,并且提交了默认的一切数据方法签名的约定,大家按着约定走,可以不写SQL语句,而如果比较复杂的情况,也需要写SQL,这里我们介绍一下查询和修改的实例方 ...

  4. Python捕捉系统信号

    #!/usr/bin/env python # -*- coding: utf-8 -*- import signal import sys """ Python捕捉中断 ...

  5. [SpringBoot guides系列翻译]调度任务

    原文 调度任务 用spring实现一个任务调度. 你将做的 你将做一个应用每5秒钟打印当前时间,用@Scheduled注解. 你需要啥 15分钟 文本编辑器或者IDE JDK1.8+ Gradle4+ ...

  6. web.xml的<url-parttern>的匹配规则

    <url-parttern>顾名思义是对url(统一资源定位符)的一种匹配,是对“http:........."地址的匹配,但是会减去应用上下文,就是你的web程序的名字,如果你 ...

  7. defer 和 async 区别

    defer saync 共同点: script 标签属性, 控制脚本加载时间,解决script下载阻塞的问题. 区别: defer:推推推荐! 异步加载,所有元素解析完执行. async: 异步加载, ...

  8. Snapde电子表格编写Exprtk脚本进行数据运算

    Snapde,一个专门为编辑超大型数据量CSV文件而设计的单机版电子表格软件:它运行的速度非常快,反应非常灵敏. 一.打开文件:用Snapde打开需要运算的CSV文件 二.添加行列:在编辑菜单找到设置 ...

  9. C# 通用单例窗体类

    /// <summary> /// 通用的单例制作器 /// </summary> /// <typeparam name="T"></t ...

  10. vue 外部字体图标使用,无须绝对路径引入办法

    通常外部字体图标都在使用 iconfont ,这种图标在网上搜到一大把都是由于路径问题显示不出来,或者是显示个方块. 最近的项目中也碰到这个坑爸的问题,总结一下解决办法: 和 webpack.conf ...