ansible管理windows测试

一、环境介绍

Ansible管理主机:

系统: redhat7.6

Linux管理服务器需安装pywinrm插件

Windows客户端主机:

系统: Server2012R2

Windows机器需要安装或升级powershell4.0以上版本,Server2008R2默认的版本是2.0,因此必须升级至4.0版本。对于Server2012及以上的版本默认是4.0版本,不需要升级。

(经测试安装powershell3.0版本并不能正常支持Ansible,因此选择升级至4.0版本)

注意:升级powershell需要重新服务器才能生效。不支持windows2003

二、Windows Server 2008R2客户端升级至powershell4.0

配置winrm之前检查系统版本,以及powershell版本,如果是Server2008R2版本,则需要升级powershell至4.0版本。Server2012R2以上的版本不需要升级powershell

升级powershell4.0步骤:

1. 检查powershell版本

未升级前显示的是2.0版本

2. 下载并安装Microsoft .NET Framework 4.5

下载地址:

https://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe

3. 下载并安装powershell4.0(Windows Management Framework 4.0

下载地址:

https://download.microsoft.com/download/3/D/6/3D61D262-8549-4769-A660-230B67E15B25/Windows6.1-KB2819745-x64-MultiPkg.msu

注意: 先安装.NET Framework 4.5 ,然后安装powershell4.0,安装完成之后重启windows服务器

4. 升级完powershell4.0后检查

查看powershell版本

打开运行--->输入powershell启动powershell

在powershell终端上执行get-host命令可以查看powershell版本

三、Windows客户端配置winrm,启用powershell远程管理

打开powershell终端,按以下步骤执行命令(正常情况不会报错,如果有报错,请检查输入的命令是否正确,或者手动输入命令进行配置)

1. 查看powershell执行策略

get-executionpolicy

2. 更改powershell执行策略为remotesigned

set-executionpolicy remotesigne

3. 配置winrm service并启动服务

winrm quickconfig

4. 查看winrm service启动监听状态

winrm enumerate winrm/config/listener

5. 修改winrm配置,启用远程连接认证

winrm set winrm/config/service/auth '@{Basic="true"}'

winrm set winrm/config/service '@{AllowUnencrypted="true"}'

四、Ansible服务端配置和测试管理Windows服务器(服务端操作)

1. 添加windows客户端连接信息

编辑/etc/ansible/hosts,添加客户端主机信息(ansible服务端的配置)

windows

192.168.1.1 ansible_ssh_user="Administrator" ansible_ssh_pass="XXXXX" ansible_ssh_port=5985 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore

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

执行命令

ansible windows -m win_ping

3. 测试文件管理

测试在windows主机执行远程创建目录

ansible windows -m win_file -a 'dest=c:\config_dir state=directory'

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

ansible windows -m win_copy -a 'src=/etc/passwd dest=C:\Users\passwd'

删除文件

ansible windows -m win_file -a 'dest=c:\config_dir\hosts.txt state=absent'

删除目录

ansible windows -m win_file -a 'dest=c:\config_dir2 state=absent'

4. 测试远程执行cmd命令

ansible windows -m win_shell -a 'ipconfig'

5. 远程重启windows服务器

ansible windows -m win_reboot

ansible windows -m win_shell -a 'shutdown -r -t 0'

6. 测试创建用户(远程在windows客户端上创建用户)

ansible windows -m win_user -a "name=testuser1 passwd=123456"

7. Windows服务管理

Ansible命令格式:

ansible [远程主机IP地址] -m win_shell -a "net stop|start 服务名"

相关推荐
信创天地11 天前
自动化运维利器赋能信创:Ansible与SaltStack在国产系统的部署与批量管理实战
运维·自动化·ansible
tritone11 天前
使用阿贝云免费云服务器学习Ansible的实践与感受
服务器·学习·ansible
~黄夫人~13 天前
Ansible自动化运维:快速入门,从 “批量化执行” 开始
运维·自动化·ansible
~黄夫人~14 天前
Ansible 自动化运维:从 “手动输密码” 到 “一键免密管理”
linux·运维·自动化·ansible
王九思14 天前
Ansible 自动化运维介绍
运维·自动化·ansible
shawnyz15 天前
RHCSE--ansible1-入门和模块
linux·运维·ansible
AOwhisky15 天前
Ansible管理变量和事实(管理变量部分) & 部署文件到受管主机
前端·chrome·ansible
shawnyz15 天前
RHCSE--ansible2--剧本
linux·运维·服务器·ansible
何以不说话17 天前
记录一下学习日常⑨(ansible、Open-V、zabbix)
学习·ansible·zabbix
_叶小格_18 天前
ansible自动化入门基础
运维·笔记·学习·自动化·ansible