【Ansible学习】- 命令相关模块之command, shell, raw模块

本文主要介绍Ansible的几个命令模块,包括:

  • command - 在远程节点上执行命令
  • shell - 让远程主机在shell进程下执行命令
  • raw - 执行低级的和脏的SSH命令

command模块

简介

  • command模块用于在给的的节点上运行系统命令,比如echo hello。
  • 它不会通过shell处理命令,因此不支持像$HOME这样的变量和,以及<, >, |, ;&等都是无效的。也就是在command模块中无法使用管道符

模块参数

名称 必选 备注
chdir no 运行command命令前先cd到这个目录
creates no 如果这个参数对应的文件存在,就不运行command
free_form yes 需要执行的脚本(没有真正的参数为free_form
executable no 改变用来执行命令的shell,应该是可执行文件的绝对路径。
removes no 如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反
stdin(2.4后新增) no 将命令的stdin设置为指定的值

示例

  • 列出指定目录下的文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@centos7 ~]# ansible test -m command -a "ls /root"
    172.20.21.120 | SUCCESS | rc=0 >>
    anaconda-ks.cfg
    test.sh
    whoami.rst

    [root@centos7 ~]# ansible test -m command -a "ls /root creates=test.sh"
    172.20.21.120 | SUCCESS | rc=0 >>
    skipped, since test.sh exists

    [root@centos7 ~]# ansible test -m command -a "ls /root removes=test.sh1"
    172.20.21.120 | SUCCESS | rc=0 >>
    skipped, since test.sh1 does not exist

在这个里面,首先更换目录到root目录中,然后查看test.sh是否存在,如果存在,那么命令不会执行;如果不存在,那么执行命令。

在这里也可以看到,命令是必须存在的,但是没有参数名为free_form参数

  • 切换目录执行命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@centos7 ~]# ansible test -m command -a "cat test.sh chdir=/root"
    172.20.21.120 | SUCCESS | rc=0 >>
    #!/bin/bash
    i=0
    echo $((i+1))

    [root@centos7 ~]# ansible test -m command -a "sh test.sh chdir=/root"
    172.20.21.120 | SUCCESS | rc=0 >>
    1
  • 无法使用管道符

    1
    2
    3
    4
    5
    6
    7
    8
    [root@centos7 ~]# ansible test -m command -a "ls /root | grep test"
    172.20.21.120 | FAILED | rc=2 >>
    /root:
    anaconda-ks.cfg
    test.sh
    whoami.rstls: 无法访问|: 没有那个文件或目录
    ls: 无法访问grep: 没有那个文件或目录
    ls: 无法访问test: 没有那个文件或目录non-zero return code

注意事项

  • 若要通过shell运行一个命令,比如<, >, |等,你实际上需要shell模块。
  • command模块更安全,因为它不受用户环境的影响
  • 从版本2.4开始,executable参数被删除。如果您需要此参数,请改用shell模块。
  • 对于Windows节点,请改用win_command模块。

shell模块

简介

让远程主机在shell进程下执行命令,从而支持shell的特性,如管道等。与command模块几乎相同,但在执行命令的时候使用的是/bin/sh

模块参数

名称 必选 备注
chdir no 运行command命令前先cd到这个目录
creates no 如果这个参数对应的文件存在,就不运行command
executable no 改变用来执行命令的shell,应该是可执行文件的绝对路径。
free_form yes 需要执行的脚本(没有真正的参数为free_form)
removes no 如果这个参数对应的文件不存在,就不运行command,与creates参数的作用相反
stdin(2.4后新增) no 将命令的stdin设置为指定的值

示例

  • 切换目录,执行命令并保持输出
    1
    2
    3
    4
    5
    6
    7
    [root@centos7 ~]# ansible test -m shell -a "sh test.sh > result chdir=/root"
    172.20.21.120 | SUCCESS | rc=0 >>


    [root@centos7 ~]# ansible test -m shell -a "cat result chdir=/root"
    172.20.21.120 | SUCCESS | rc=0 >>
    1

注意事项

  • 如果你想安全可靠的执行命令,请使用command模块,这也是编写playbook的最佳实践。

raw模块

简介

  • raw模块主要用于执行一些低级的,脏的SSH命令,而不是通过command模块。 raw模块只适用于下列两种场景,第一种情况是在较老的(Python 2.4和之前的版本)主机上,另一种情况是对任何没有安装Python的设备(如路由器)。 在任何其他情况下,使用shellcommand模块更为合适。
  • 就像script模块一样,raw模块不需要远程系统上的python

模块参数

名称 必选 备注
executable no 改变用来执行命令的shell,应该是可执行文件的绝对路径。
free_form yes 需要执行的脚本(没有真正的参数为free_form)

示例

  • 在远程主机上执行脚本
    1
    2
    3
    4
    [root@centos7 ~]# ansible test -m raw -a "pwd"
    172.20.21.120 | SUCCESS | rc=0 >>
    /root
    Shared connection to 172.20.21.120 closed.

注意事项

  • 如果要安全可靠地执行命令,最好使用shellcommand模块来代替。
  • 如果从playbook中使用raw,则可能需要使用gather_facts: no禁用事实收集
hoxis wechat
一个脱离了高级趣味的程序员,关注回复1024有惊喜~
赞赏一杯咖啡
0%