需要语法才能与 Ansible shell 和命令模块一起使用

Need syntax to work with both Ansible shell & command module

下面的语法工作正常。

---
- hosts: all

  tasks:
    - name: run this command and ignore the result
      shell: echo "The server is UP since " `uptime`

然而,当我将 shell 模块更改为命令模块时,我希望它仍然有效。

      command: echo "The server is UP since " `uptime`

但它不会用命令模块打印正常运行时间值。

我不能使用相同的语法来处理 shell 和命令模块吗?

Can I not have the same syntax to work with both the shell as well as the command module ?

是的,当然,只需手动完成 shell: 将要做的工作并将字符串包装在 sh -c

- set_fact:
    the_command: sh -c 'echo "The server is UP since `uptime`"'
- command: '{{ the_command }}'
- shell: '{{ the_command }}'