自动化运维、大数据、Docker

实例学习ansible系列(4)常用模块之command/shell/raw

知识点:使用module command或者shell或者raw都能调用对象机器上的某条指令或者某个可执行文件。

使用方法

[root@host31 ~]# ansible localhost -m command -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello

[root@host31 ~]# ansible localhost -m shell -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello

[root@host31 ~]# ansible localhost -m raw -a "echo hello"
localhost | SUCCESS | rc=0 >>
hello


[root@host31 ~]#

是否支持管道

module 是否支持管道
command 不支持管道
shell 支持管道
raw 支持管道
[root@host31 ~]# ansible localhost -m command -a "ps -ef |wc -l"
localhost | FAILED | rc=1 >>
error: garbage option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).

[root@host31 ~]# ansible localhost -m shell -a "ps -ef |wc -l"
localhost | SUCCESS | rc=0 >>
448

[root@host31 ~]# ansible localhost -m raw -a "ps -ef |wc -l"
localhost | SUCCESS | rc=0 >>
445


[root@host31 ~]#

直接执行某个文件

[root@host31 ~]# ansible host32 -m command -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello world

[root@host31 ~]# ansible host32 -m raw -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello world


[root@host31 ~]# ansible host32 -m shell -a "/tmp/ttt.sh"
host32 | SUCCESS | rc=0 >>
hello world

[root@host31 ~]#

都是支持的,但是需要注意/tmp/ttt.sh应该有执行权限

ansible-doc -s取得更详细信息

希望知道更加详细的module的信息,最好的方法是使用ansible自带的ansible-doc的-s选项

[root@host31 ~]# ansible-doc -s raw
- name: Executes a low-down and dirty SSH command
  action: raw
      executable             # change the shell used to execute the command. Should be an absolute path to the executable.
      free_form=             # the raw module takes a free form command to run
[root@host31 ~]#
[root@host31 ~]# ansible-doc -s shell
- name: Execute commands in nodes.
  action: shell
      chdir                  # cd into this directory before running the command
      creates                # a filename, when it already exists, this step will *not* be run.
      executable             # change the shell used to execute the command. Should be an absolute path to the executable.
      free_form=             # The shell module takes a free form command to run, as a string.  There's not an actual option
                               named "free form".  See the examples!
      removes                # a filename, when it does not exist, this step will *not* be run.
      warn                   # if command warnings are on in ansible.cfg, do not warn about this particular line if set to
                               no/false.
[root@host31 ~]#
[root@host31 ~]# ansible-doc -s command
- name: Executes a command on a remote node
  action: command
      chdir                  # cd into this directory before running the command
      creates                # a filename or (since 2.0) glob pattern, when it already exists, this step will *not* be run.
      executable             # change the shell used to execute the command. Should be an absolute path to the executable.
      free_form=             # the command module takes a free form command to run.  There is no parameter actually named 'free
                               form'. See the examples!
      removes                # a filename or (since 2.0) glob pattern, when it does not exist, this step will *not* be run.
      warn                   # if command warnings are on in ansible.cfg, do not warn about this particular line if set to
                               no/false.
[root@host31 ~]#

本文转载自 实例学习ansible系列(4)常用模块之command/shell/raw

赞(0) 打赏
蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明蜷缩的蜗牛 » 实例学习ansible系列(4)常用模块之command/shell/raw
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏