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

相关推荐
风清再凯14 小时前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
IT乌鸦坐飞机14 小时前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
遇见火星14 天前
如何使用Ansible一键部署MinIO集群?
ansible
粥周粥14 天前
ANSIBLE
ansible
码农101号14 天前
Linux中ansible模块补充和playbook讲解
linux·运维·ansible
码农101号14 天前
Linux的Ansible软件基础使用讲解和ssh远程连接
ansible
烟雨书信15 天前
ANSIBLE运维自动化管理端部署
运维·自动化·ansible
碎碎-li15 天前
ANSIBLE(运维自动化)
运维·自动化·ansible
@donshu@18 天前
Linux运维-ansible-python开发-获取inventroy信息
linux·运维·ansible
Kendra91921 天前
Ansible
ansible