简体   繁体   English

需要语法与Ansible Shell和命令模块一起使用

[英]Need syntax to work with both Ansible shell & command module

The below syntax works fine. 下面的语法工作正常。

---
- hosts: all

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

However when i change shell module to command module I was expecting this to still work. 但是,当我将外壳模块更改为命令模块时,我期望它仍然可以正常工作。

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

But it does not print the uptime value with the command module. 但是它不会使用命令模块显示正常运行时间值。

Can I not have the same syntax to work with both the shell as well as the command module ? 我不能在外壳程序和命令模块上使用相同的语法吗?

Can I not have the same syntax to work with both the shell as well as the command module ? 我不能在外壳程序和命令模块上使用相同的语法吗?

Yes, of course, by just manually doing the job that shell: is going to do and wrapping the string in sh -c 是的,当然,只需手动执行shell:要做的工作并将字符串包装在sh -c

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM