我正在尝试运行一个简单的playbook来运行Powershell脚本。但它一直都在失败。如果我直接在服务器上运行Powershell脚本,它可以正常工作。下面是剧本和剧本。 是否需要做一些事情来让Ansible运行Powershell脚本?
Powershell脚本(rdp.ps1):
set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server'-name "fDenyTSConnections" -Value 0
剧本:
---
- name: Enable Remote Desktop on
hosts: all
tasks:
- name: Enable Remote Desktop
script: files/Enable_RDP.ps1
错误:
已更改:[10.10.3.170] => {“changed”:true,“invocation”:{“module_args”:{“_ raw_params”:“files / Enable_RDP.ps1”},“module_name”:“script”},“rc”:0,“stderr”:“ #< CLIXML \ r \ nhttp://schemas.microsoft.com/powershell/2004/04 \(http://schemas.microsoft.com/powershell/2004/04%5C)“> set-ItemProperty:不能查找路径_x000D__x000A_'HKLM:\ SystemCurrentControlSetControlTerminal Server',因为它不_x000D__x000A_exist._x000D__x000A_At C:\ Users \ Administrator \ AppData \ Local \ Temp \ _ ansible-tmp-1454943953.17-24292631_x000D__x000A_07433 \ Enable_RDP.ps1:2 char:1_x000D__x000A_ + set-ItemProperty -Path'HKLM:SystemCurrentControlSetControlTerminal _x000D__x000A_Server'-name ... _ x000D__x000A_ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ObjectNotFound:(HKLM:\ SystemCur ... Terminal Serv _x000D__x000A_ er:String)[Set-ItemProperty],ItemNotFoundException_x000D__x000A_ + FullyQualifiedErrorId:PathNotFound,Microsoft.PowerShell.Commands.SetIt _ x000D__x000A_ emPropertyCommand_x000D__x000A_ _x000D__x000A_“,”stdout“:”“,”stdout_lines“:[]}
所以我编辑了剧本,尝试使用'raw'命令运行命令......但是这些也失败了。
---
- name: Enable Remote Desktop on
hosts: all
tasks:
- name: Enable Remote Desktop
raw: 'set-ItemProperty -Path "HKLM:SystemCurrentControlSetControlTerminal Server"-name "fDenyTSConnections" -Value 0'
我也试过没有'',就像这样:
---
- name: Enable Remote Desktop on
hosts: all
tasks:
- name: Enable Remote Desktop
raw: set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server'-name "fDenyTSConnections" -Value 0
然后我使用Raw命令运行以下playbook来创建一个文件夹。它奏效了。一个非常基本的Powershell命令。
---
- name: Enable Remote Desktop on
hosts: all
tasks:
- name: Enable Remote Desktop
raw: New-Item -Path c:\test3 -ItemType directory
什么可能导致一个剧本失败而另一个剧本成功?肯定Ansible应该能够运行任何Powershell脚本吗?或者是否有某种先决条件或特定的方式来编写这些Powershell脚本以使它们工作?
答案 0 :(得分:4)
PowerShell告诉Ansible它"无法找到路径"致:
HKLM:SystemCurrentControlSetControlTerminal Server
原因很简单,没有这样的路径! : - )
该字符串中的路径分隔符似乎在路上丢失,这是您正在寻找的路径:
HKLM:\System\CurrentControlSet\Control\Terminal Server