• 欢迎访问蜷缩的蜗牛博客 蜷缩的蜗牛
  • 微信搜索: 蜷缩的蜗牛 | 联系站长 kbsonlong@qq.com
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

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

Ansible 蜷缩的蜗牛 8个月前 (01-23) 22次浏览 已收录

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

使用方法

<code class=" hljs ruby">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible localhost -m command -a "echo hello"</span>
localhost | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
hello

[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible localhost -m shell -a "echo hello"</span>
localhost | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
hello

[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible localhost -m raw -a "echo hello"</span>
localhost | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
hello


[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

是否支持管道

module 是否支持管道
command 不支持管道
shell 支持管道
raw 支持管道
<code class=" hljs vbnet">[root@host31 ~]<span class="hljs-preprocessor"># ansible localhost -m command -a "ps -ef |wc -l"</span>
localhost | FAILED | rc=<span class="hljs-number">1</span> >>
<span class="hljs-keyword">error</span>: garbage <span class="hljs-keyword">option</span>

Usage:
 ps [options]

 <span class="hljs-keyword">Try</span> <span class="hljs-comment">'ps --help <span class="hljs-xmlDocTag"><simple|list|output|threads|misc|all></span>'</span>
  <span class="hljs-keyword">or</span> <span class="hljs-comment">'ps --help <span class="hljs-xmlDocTag"><s|l|o|t|m|a></span>'</span>
 <span class="hljs-keyword">for</span> additional help <span class="hljs-keyword">text</span>.

<span class="hljs-keyword">For</span> more details see ps(<span class="hljs-number">1</span>).

[root@host31 ~]<span class="hljs-preprocessor"># ansible localhost -m shell -a "ps -ef |wc -l"</span>
localhost | SUCCESS | rc=<span class="hljs-number">0</span> >>
<span class="hljs-number">448</span>

[root@host31 ~]<span class="hljs-preprocessor"># ansible localhost -m raw -a "ps -ef |wc -l"</span>
localhost | SUCCESS | rc=<span class="hljs-number">0</span> >>
<span class="hljs-number">445</span>


[root@host31 ~]<span class="hljs-preprocessor">#</span></code>

直接执行某个文件

<code class=" hljs ruby">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m command -a "/tmp/ttt.sh"</span>
host32 | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
hello world

[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m raw -a "/tmp/ttt.sh"</span>
host32 | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
hello world


[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ansible host32 -m shell -a "/tmp/ttt.sh"</span>
host32 | <span class="hljs-constant">SUCCESS</span> | rc=<span class="hljs-number">0</span> >>
hello world

[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

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

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

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

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

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


蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明实例学习 ansible 系列(4)常用模块之 command/shell/raw
喜欢 (0)
[]
分享 (0)