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 服务名"

相关推荐
Hy行者勇哥5 天前
用Cursor智能编写Ansible/Terraform脚本,打通CI/CD链路
ci/cd·ansible·terraform
芷栀夏7 天前
飞牛NAS怎么部署Immich?家庭照片自动备份与远程访问教程
服务器·nginx·ansible
悠然南风17 天前
Ansible常见模块总结及LDAP Role 编写与调试
ansible
祺风挽楠1 个月前
ansible编辑
网络·ansible
芳心粽伙饭1 个月前
Ansible课后作业
ansible
烁3471 个月前
Ansible初识
ansible
烁3471 个月前
Ansible安装部署调试
ansible
烁3471 个月前
Ansible命令
ansible
小义_1 个月前
【Ansible】(三)基础配置与连接设置
云原生·ansible
炸炸鱼.1 个月前
Ansible 企业级实战:Playbook 与 Roles 完全指南
网络·ansible