如何使用 Ansible Playbook 和 win_command、win_shell 模块在 Windows 目标主机上自动化执行 PowerShell 或 cmd.exe 代码。

这两个 Ansible 模块常常被混淆,但它们在本质上是不同的。尽管两者都可以在 Windows 目标主机上执行命令,但它们的执行方式略有不同。

win_command vs win_shell

win_command

  • 在远程 Windows 节点上执行命令。
  • 绕过 Windows Shell。
  • 始终将 changed 设置为 True。

win_shell

  • 在 Windows 目标主机上执行 Shell 命令。
  • 支持重定向和 win_shell 的内置功能。
  • 始终将 changed 设置为 True。

模块对比

win_command 和 win_shell 模块都可以在 Windows 目标节点上执行命令,但它们的执行方式有显著差异。 一般来说,如果可能,优先使用专门的 Ansible 模块来执行任务。然而,有时唯一的方法是通过 win_command 或 win_shell 模块执行 Windows PowerShell 或 cmd.exe。

再次强调,尽可能避免使用 win_command/win_shell,而选择更合适的模块。 win_command 模块在目标机器上执行命令时不会使用目标 Shell,它直接执行命令。例如,它无法处理 PowerShell 或 cmd.exe 的内置功能和重定向。它不会通过 Shell 处理命令,因此像 $env:HOME 这样的变量以及 <、>、| 和 ; 等操作符不可用。相比之下,win_shell 模块执行的每个命令都具备完整的 PowerShell 或 cmd.exe 功能,可以在运行时动态扩展。从安全角度来看,win_command 模块更为稳健,因为它绕过了 Shell,结果也更具可预测性。

由于 Ansible 无法预测命令执行是否真正改变了目标系统,因此这两个模块始终返回 changed 状态。 对于非 Windows 目标,请改用 ansible.builtin.command 和 ansible.builtin.shell 模块。

win_command

  • 在远程 Windows 节点上执行命令。
  • 不受本地 Shell 变量的影响,因为它绕过了 Shell。
  • 无法运行 Shell 的内置功能和重定向。

win_shell

  • 在目标主机上执行 Shell 命令。
  • win_shell 模块比 win_command 模块更具潜在危险性,仅在确实需要 PowerShell 或 cmd.exe 功能时才应使用。
  • 如果你没有将多个命令串联在一起,也没有使用像 $env:HOME 这样的变量或 <、>、| 和 ; 这样的操作符,那么你实际上并不需要 win_shell 模块。同样,扩展 Shell 变量也需要 win_shell 模块。如果你不使用这些功能,就不要使用 win_shell 模块。有时,这可能是唯一的选择,我知道。

示例

让我通过三个 Ansible Playbook 来展示 win_command 和 win_shell 模块之间的区别。

win_command 示例代码

---
- name: win_command module demo
  hosts: all
  tasks:
    - name: check netstat
      ansible.windows.win_command: "netstat -e"
      register: command_output
    - name: command output
      ansible.builtin.debug:
        var: command_output.stdout_lines

win_command 执行结果

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory command_shell/win_date.yml
PLAY [win_command module demo] **************************************************
TASK [Gathering Facts] **********************************************************
ok: [WindowsServer]
TASK [check getdate] ************************************************************
changed: [WindowsServer]
TASK [command output] ************************************************************
ok: [WindowsServer] => {
    "command_output.stdout_lines": [
        "demo",
        "",
        "Friday, April 8, 2022 11:19:21 AM",
        "",
        ""
    ]
}
PLAY RECAP **********************************************************************
WindowsServer              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

win_shell 示例代码

---
- name: win_shell module demo
  hosts: all
  tasks:
    - name: check getdate
      ansible.windows.win_shell: |
        hostname
        Get-Date
      register: command_output
    - name: command output
      ansible.builtin.debug:
        var: command_output.stdout_lines

win_shell 执行结果

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory command_shell/win_date.yml
PLAY [win_shell module demo] **************************************************
TASK [Gathering Facts] **********************************************************
ok: [WindowsServer]
TASK [check getdate] ************************************************************
changed: [WindowsServer]
TASK [command output] ************************************************************
ok: [WindowsServer] => {
    "command_output.stdout_lines": [
        "demo",
        "",
        "Friday, April 8, 2022 18:19:36 AM",
        "",
        ""
    ]
}
PLAY RECAP **********************************************************************
WindowsServer              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $

错误模块代码示例

win_command 无法执行多行 PowerShell 命令。

---
- name: win_command module demo
  hosts: all
  tasks:
    - name: check getdate
      ansible.windows.win_command: |
        hostname
        Get-Date
      register: command_output
    - name: command output
      ansible.builtin.debug:
        var: command_output.stdout_lines

错误模块执行结果

ansible-pilot $ ansible-playbook -i virtualmachines/win/inventory command_shell/win_date_command.yml
PLAY [win_command module demo] **************************************************
TASK [Gathering Facts] **********************************************************
ok: [WindowsServer]
TASK [check getdate] ************************************************************
changed: [WindowsServer]
TASK [command output] ************************************************************
ok: [WindowsServer] => {
    "command_output.stdout_lines": [
        "demo"
    ]
}
PLAY RECAP **********************************************************************
WindowsServer              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
ansible-pilot $