Ansible 命令不会输出为剧本

Ansible command wont output as playbook

我在 winRM 上使用 ansible 2.7.5 和 windows 10 主机。

如果我执行:

ansible win -I hosts -m win_command -a "PowerShell.exe ipconfig" 

我得到一个输出,它工作正常。

但我想将它写在剧本中,而不是作为一个可靠的命令,但我不会得到任何输出,所以我尝试使用标准输出,但它不起作用:

- hosts: win
  gather_facts: no
  tasks:
  - name: PowerShell Command
    win_command: powershell.exe
    register: shell_result
    args: 
      stdin: ipconfig
      -debug: 
         var: shell_result.stdout_lines

关于如何让第一个命令作为剧本工作有什么建议吗?

您的 - debug 任务未正确缩进且间距不正确。我不确定那是因为在这里发帖还是在你的原始文件中。

您对 powershell.exe 的调用遗漏了 - 参数,该参数告诉它接受标准输入上的输入。所以应该是win_command: powershell.exe -.

也就是说,ipconfig 实际上是 ipconfig.exe,所以不要尝试 运行 一个 shell 和 win_command 只是 运行 ipconfig.exe直接:

- hosts: win
  gather_facts: no
  tasks:
  - name: ipconfig
    win_command: ipconfig.exe
    register: shell_result
  - debug: 
      var: shell_result.stdout_lines