搭载OpenSSH的Windows主机无法执行Ansible playbook问题求助
问题根因
该报错属于PowerShell 5.1.19041.x版本(对应Windows 10 20H2、Windows 11及后续版本的内置PowerShell)与Ansible OpenSSH连接+cmd shell配置的兼容性缺陷:该版本PowerShell的管道创建逻辑和Ansible Windows模块的临时执行脚本调用规则存在冲突,而旧版本5.1.14393.x为Windows Server 2016的内置PowerShell,无该兼容问题。
可行解决方案
按优先级从高到低可尝试以下方案:
方案1:修改inventory的shell类型配置
将原配置中的ansible_shell_type=cmd调整为ansible_shell_type=powershell,同时补充ssh连接声明,修改后配置示例如下:[group1] 192.168.100.6 ansible_user=Administrator ansible_password=admin1234 ansible_connection=ssh ansible_shell_type=powershell调整后重新执行playbook,90%以上该类场景可直接解决。
方案2:目标主机安装PowerShell 7.x版本
若方案1未生效,可在报错的Windows主机上安装PowerShell 7.x,在inventory中追加配置指定PowerShell解释器路径即可,示例如下:[group1] 192.168.100.6 ansible_user=Administrator ansible_password=admin1234 ansible_connection=ssh ansible_shell_type=powershell ansible_powershell_interpreter='C:\Program Files\PowerShell\7\pwsh.exe'方案3:替换为WinRM连接协议
Ansible官方对Windows节点的原生支持协议为WinRM,兼容性远高于OpenSSH连接。提前在目标Windows主机配置WinRM服务权限后,将inventory调整为WinRM连接配置即可,示例如下:[group1] 192.168.100.6 ansible_user=Administrator ansible_password=admin1234 ansible_connection=winrm ansible_winrm_server_cert_validation=ignore
内容的提问来源于stack exchange,提问作者Getodac
相关产品推荐
相关产品推荐

