一,前期准备
1,控制端(Linux)的要求
- Ansible可以在安装了Python 2(2.7版)或Python 3(3.5及更高版本)的任何机器上运行。
- 控制端计算机不支持Windows。
2,客户端(Windows)的要求
要使Ansible与Windows主机通信并使用Windows模块,Windows主机必须满足以下要求:
- 支持的桌面操作系统包括Windows 7,8.1和10;支持的服务器操作系统包括Windows Server 2008,2008 R2,2012,2012 R2和2016。
- 安装python环境,最好3.9+及以上版本
- Ansible需要PowerShell 3.0或更高版本,并且至少要在Windows主机上安装.NET 4.0。可以使用Upgrade-PowerShell.ps1脚本来更新:
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Upgrade-PowerShell.ps1"
$file = "$env:temp\Upgrade-PowerShell.ps1"
$username = "Administrator"
$password = "Password"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
# version can be 3.0, 4.0 or 5.1
&$file -Version 5.1 -Username $username -Password $password -Verbose
完成后,您将需要删除自动登录并将执行策略设置回默认值Restricted。您可以使用以下PowerShell命令执行此操作:
# this isn't needed but is a good security practice to complete
Set-ExecutionPolicy -ExecutionPolicy Restricted -Force
$reg_winlogon_path = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty -Path $reg_winlogon_path -Name AutoAdminLogon -Value 0
Remove-ItemProperty -Path $reg_winlogon_path -Name DefaultUserName -ErrorAction SilentlyContinue
Remove-ItemProperty -Path $reg_winlogon_path -Name DefaultPassword -ErrorAction SilentlyContinue
【注意】
1,如果在Server 2008上运行,则必须安装SP2。如果在Server 2008 R2或Windows 7上运行,则必须安装SP1。
2,Windows Server 2008只能安装PowerShell 3.0; 指定较新的版本将导致脚本失败。
3,username和password参数都是存储在注册表中的纯文本。确保在脚本完成后运行清理命令,以确保主机上仍未存储凭据。
- WinRM内存补丁
在PowerShell v3.0上运行时,WinRM服务存在一个错误,它限制了WinRM可用的内存量。如果未安装此补丁,Ansible将无法在Windows主机上执行某些命令。这些补丁应作为系统引导或映像过程的一部分安装。Install-WMF3Hotfix.ps1脚本可用于在受影响的主机上安装此修补程序。 以下PowerShell命令将安装此修补程序:
$url = "https://raw.githubusercontent.com/jborean93/ansible-windows/master/scripts/Install-WMF3Hotfix.ps1"
$file = "$env:temp\Install-WMF3Hotfix.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file -Verbose
- WinRM设置
请在PowerShell中运行以下命令:
$url = "https://raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1"
$file = "$env:temp\ConfigureRemotingForAnsible.ps1"
(New-Object -TypeName System.Net.WebClient).DownloadFile($url, $file)
powershell.exe -ExecutionPolicy ByPass -File $file
【注意】
ConfigureRemotingForAnsible.ps1脚本仅用于培训和开发目的,不应在生产环境中使用,因为它启用了 Basic 这本质上不安全(如身份验证)。
- WinRM Listener
WinRM服务侦听一个或多个端口上的请求
# 查看在WinRM服务上运行的当前侦听器
winrm quickconfig
winrm enumerate winrm/config/Listener
# 修改winrm配置,启用远程连接认证
winrm set winrm/config/service/auth '@{Basic="true"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
- windows 防火墙配置
选择入站规则,允许5985-5986端口通过,均为TCP协议。
二,基本配置
添加windows客户端连接信息。编辑 /etc/ansible/hosts
, 添加客户端主机信息(ansible服务端的配置)
[组名]
192.168.2.2 ansible_user="Administrator" ansible_password="Password" ansible_port=5986 ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore ansible_winrm_transport=basic
三,常用模块命令
下面ip:‘192.168.2.2’ 可以改成hosts中的【组名】。
1,ping 远程windows主机
ansible 192.168.2.2 -m win_ping
2,创建目录
ansible 192.168.2.2 -m win_file -a 'path=D:\\test state=directory'
3,复制/下发文件
ansible 192.168.2.2 -m win_copy -a 'src=/etc/hosts dest=D:\\hosts.txt'
4,删除文件
ansible 192.168.2.2 -m win_file -a 'dest=d:\\config_dir\\hosts.txt state=absent'
5,删除文件或目录
ansible 192.168.2.2 -m win_file -a "path=d:\\Ansible\\justin state=absent"
6,执行cmd命令
ansible 192.168.2.2 -m win_shell -a 'ipconfig'
7,重启windows
三种方式来重启
ansible 192.168.2.2 -m win_reboot
# 立即重启系统,不会等待,可能会导致应用程序突然关闭,丢失未保存的数据:
ansible 192.168.2.2 -m win_shell -a 'shutdown -r -t 0'
# 在 1 秒后重启系统,允许系统关闭正在运行的应用程序并提示用户保存工作(推荐):
ansible 192.168.2.2 -m win_shell -a "shutdown -r -t 1"
ansible 192.168.2.2 -m win_service -a "name=spooler state=restarted"
8,创建用户
远程在windows客户端上创建用户
ansible 192.168.2.2 -m win_user -a "name=username passwd=123456 groups=Administrators"
9,windows服务管理
ansible 192.168.2.2 -m win_shell -a "net stop|start zabbix_agent"
10,获取window主机信息
ansible 192.168.2.2 -m setup
11,执行ps脚本
ansible 192.168.2.2 -m script -a "E://test.ps1"
12,获取IP地址:
ansible 192.168.2.2 -m win_command -a "ipconfig"
13,获取身份 :
ansible windows -m win_command -a "whoami"
14,查看文件状态:
ansible 192.168.2.2 -m win_stat -a "path='C://Windows/win.ini'"
更多命令:
移动文件:
ansible windows -m raw -a "cmd /c 'move /y d:\issue c:\issue'"
创建文件夹:
ansible windows -m raw -a "mkdir d:\tst"
结束程序:
ansible windows -m raw -a "taskkill /F /IM QQ.exe /T"
将.zip解压到远程Windows主机,远程主机上必须存在需要解压的源文件
ansible windows -m win_unzip -a "creates=no src=D:\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 -a "name=Tomcat8630 state=restarted"
重启node.js(.bat命令)
ansible windows -m win_command -a "chdir=D:\SupplierPay .\http_restart.bat"
执行win_command模块命令
启动redis
ansible windows -m win_command -a "chdir=D:\Redis server-start.bat "
ansible win -m win_command -a "chdir=C:\ a.bat "
ps:"chdir=C:\ a.bat " 之前有空格
创建一个名叫user1的管理员用户,要求能够远程访问
ansible windows -m win_user -a "name=user1 password=123 groups='Administrators,Remote Desktop Users'"
移动文件
ansible windows -m raw -a "cmd /c 'move /y D:\Ansible\product\DBFPlus.exe D:\Ansible\back\'"
移动文件目标端也需要制定到文件,而不能只制定到所在目录位置
ansible windows -m raw -a "cmd /c 'move /y D:\Ansible\product D:\Ansible\back'"
移动文件夹源端和目标端目录都不能带反斜杠/。且将源的整个目录移到目的端目录里。
创建文件夹
ansible windows -m raw -a "md d:\Ansible\justin"
结束某程序
ansible windows -m raw -a "taskkill /F /IM snmp.exe /T"
常用模块:
win_acl (E) :设置文件/目录属主属组权限;
win_copy :拷贝文件到远程Windows主机;
win_file :创建,删除文件或目录;
win_lineinfile :匹配替换文件内容;
win_package (E) :安装/卸载本地或网络软件包;
win_service :管理Windows Services服务;
win_user :管理Windows本地用户。