Ansible之管理windows主机

编程入门 行业动态 更新时间:2024-10-21 20:36:31

一、需求说明及powershell简介

1、需求说明

  信息系统中运行的服务器除了linux服务器,免不了还会有windows服务器,我们系统通过Ansible主控机同时管理所有的服务器。Ansible既然号称无agent的轻量级自动化运维管理工具,管理windows服务器也不需要安装agent客户端,而是利用windows自带的powershell管理windows服务器,Ansible要求windows客户机安装的powershell版本V3以上。因为powershell 3存在一些严重的bug,对Ansible的支持并不是很好,建议安装powershell 4。本博文将介绍如何配置Ansible管理windows主机

2、powershell简介

  Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能。PowerShell v3采用新的cmdlet让管理员能够更深入到系统进程中,这些进程可以制作成可执行的文件或脚本(script)。一条cmdlet是一条轻量命令,Windows PowerShell运行时间在自动化脚本的环境里调用它。PowerShell v3将在PowerShell上打造管理的大部分,也提供GUI管理选项以及命令行自动化。v3引入了一些相当重要的新功能,比如更好的远程处理。PowerShell远程已经逐渐成为在网络上进行管理通信的主要渠道。越来越多的GUI管理控制台将依赖远程,因此加强PowerShell远程对微软很重要。现在能够断开远程会话,稍后能从同个或不同的计算机重新连接到相同的会话。客户端计算机崩溃的话,v3的社区技术预览版不能断开会话。相反,会话会永久关闭。所以这与远程桌面完全不同,远程桌面会话能在客户端崩溃时配置并打开会话。

3、示例环境说明

  • 主控机操作系统:centos 7.6
  • 客户机操作系统:win7-x64
  • Ansible版本:ansible 2.9.18
  • powershell版本:5.1.14409.1005

二、Ansible管理windows主机-windows客户机配置步骤

1、检查windows客户机powershell版本

输入powershell命令进入powershell模式
输入get-host或者$PSVersionTable命令查看powershell版本
其中win7、window server 2008的默认powershell版本为powershell 4,windows server2012默认版本为 4,window 10默认版本为5.1。

2、下载并安装Microsoft .NET Framework 4.5和powershell5.1

  • Microsoft .NET Framework 4.5下载地址:
    https://download.microsoft/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe
  • powershell5.1下载地址:
    powershell最新版本为7.1.3(发文时间),powershell5.1以后版本不区分操作系统。
    https://www.filehorse/download-windows-powershell-64/37429/

升级powershell后操作系统需重启。

3、升级完成后检查powershell版本

4、查看powershell执行策略

使用get-executionpolicy查看powershell执行策略

5、更改powershell执行策略为remotesigned

使用set-executionpolicy remotesigned命令修改策略

6、配置winrm service并启动服务

使用winrm quickconfig命令启动winrm服务

7、查看winrm service启动监听状态

使用winrm enumerate winrm/config/listener命令启动监听服务

8、修改winrm配置,启用远程连接认证

命令如下:
winrm set winrm/config/service/auth ‘@{Basic=“true”}’

winrm set winrm/config/service ‘@{AllowUnencrypted=“true”}’

9、添加防火墙信任规则,允许5985端口通过

三、Ansible主控机配置及验证步骤

1、inventory文件配置

[root@test1 ansible]# cat win_hosts
[test]
192.168.0.9 ansible_ssh_user=“Administrator” ansible_ssh_pass=“password123” ansible_ssh_port=5985 ansible_connection=“winrm” ansible_winrm_server_cert_validation=ignore

2、测试ping探测windows客户主机是否存活

3、查看跟windows相关的模块

4、测试在windows远程客户机创建目录

[root@test1 ansible]# ansible -i win_hosts -m win_file -a ‘dest=d:\config_dir state=directory’ test
192.168.0.9 | CHANGED => {
“changed”: true
}

5、测试将ansible主机上的/etc/hosts文件同步到windows主机的指定目录下

[root@test1 ansible]# ansible -i win_hosts -m win_copy -a ‘src=/etc/hosts dest=d:\config_dir\hosts.txt’ test
192.168.0.9 | CHANGED => {
“changed”: true,
“checksum”: “7335999eb54c15c67566186bdfc46f64e0d5a1aa”,
“dest”: “d:\config_dir\hosts.txt”,
“operation”: “file_copy”,
“original_basename”: “hosts”,
“size”: 158,
“src”: “/etc/hosts”
}

6、其他测试

#删除hosts文件
[root@test1 ansible]# ansible -i win_hosts -m win_file -a ‘dest=d:\config_dir\hosts.txt state=absent’ test
192.168.0.9 | CHANGED => {
“changed”: true
}
#删除目录
[root@test1 ansible]# ansible -i win_hosts -m win_file -a ‘dest=d:\config_dir state=absent’ test
192.168.0.9 | CHANGED => {
“changed”: true
}
#远程执行执行windows shell命令
[root@test1 ansible]# ansible -i win_hosts -m win_shell -a “ipconfig” 192.168.0.9
192.168.0.9 | CHANGED | rc=0 >>

Windows IP Configuration


Ethernet adapter ��������:

Connection-specific DNS Suffix . :
IPv4 Address. . . . . . . . . . . : 192.168.0.9
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
#执行远程重启服务器
[root@test1 ansible]# ansible -i win_hosts -m win_reboot
[root@test1 ansible]# ansible -i win_hosts -m win_shell -a ‘shutdown -r -t 0’
#远程创建用户
[root@test1 ansible]# ansible -i win_hosts -m win_user -a “name=test1 passwd=123456” test
192.168.0.9 | CHANGED => {
“account_disabled”: false,
“account_locked”: false,
“changed”: true,
“description”: “”,
“fullname”: “test1”,
“groups”: [],
“name”: “test1”,
“password_expired”: true,
“password_never_expires”: false,
“path”: “WinNT://WorkGroup/XTZJ-2020DWDIHQ/test1”,
“sid”: “S-1-5-21-1672353480-595772364-1259071024-1007”,
“state”: “present”,
“user_cannot_change_password”: false
}
#远程删除用户
[root@test1 ansible]# ansible -i win_hosts -m win_user -a “name=test1 state=absent” test
192.168.0.9 | CHANGED => {
“changed”: true,
“msg”: “User ‘test1’ deleted successfully”,
“name”: “test1”,
“state”: “absent”
}
#远程启停服务
[root@test1 ansible]# ansible -i win_hosts -m win_shell -a “net stop spooler” test
192.168.0.9 | CHANGED | rc=0 >>
The Print Spooler service is stopping.
The Print Spooler service was stopped successfully.

[root@test1 ansible]# ansible -i win_hosts -m win_shell -a “net start spooler” test
192.168.0.9 | CHANGED | rc=0 >>
The Print Spooler service is starting.
The Print Spooler service was started successfully.

四、FAQ

1、执行win_ping时报错No module named winrm

  • 报错信息:

  • 报错原因:

未安装pywinrm模块

  • 解决方案:

[root@test1 ansible]# pip install pywinrm --upgrade

Installing collected packages: ntlm-auth, requests-ntlm, pywinrm
Running setup.py install for pywinrm … done
Successfully installed ntlm-auth-1.5.0 pywinrm-0.4.1 requests-ntlm-1.1.0

更多推荐

Ansible之管理windows主机

本文发布于:2023-06-14 08:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主机   Ansible   windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!