ansible常用命令模块

一、概述


command 模块可以帮助我们在远程主机上执行命令。


注意:使用 command 模块在远程主机中执行命令时,不会经过远程主机的 shell 处理,在使用 command 模块时,如果需要执行的命令中含有重定向、管道符等操作时,这些符号也会失效,比如”<”, “>”, “|”, “;” 和 “&” 这些符号,如果你需要这些功能,可以参考后面介绍的 shell 模块。还有一点需要注意,如果远程节点是 windows 操作系统,则需要使用 win_command 模块。


执行 ansible 时,不加 -m 默认使用 command ,可以在 /etc/ansible/ansible.cfg 中修改。

# default module name for /usr/bin/ansible
#module_name = command

二、常用参数
free_form参数 :必须参数,指定需要远程执行的命令。需要说明一点,free_form 参数与其他参数(如果想要使用一个参数,那么则需要为这个参数赋值,也就是name=value模式)并不相同。比如,当我们想要在远程主机上执行 ls 命令时,我们并不需要写成”free_form=ls” ,这样写反而是错误的,因为并没有任何参数的名字是 free_form,当我们想要在远程主机中执行 ls 命令时,直接写成 ls 即可。因为 command 模块的作用是执行命令,所以,任何一个可以在远程主机上执行的命令都可以被称为 free_form。

chdir参数 : 此参数的作用就是指定一个目录,在执行对应的命令之前,会先进入到 chdir 参数指定的目录中。

creates参数 :看到 creates,你可能会从字面上理解这个参数,但是使用这个参数并不会帮助我们创建文件,它的作用是当指定的文件存在时,就不执行对应命令,比如,如果 /testdir/test文件存在,就不执行我们指定的命令。

removes参数 :与 creates 参数的作用正好相反,它的作用是当指定的文件不存在时,就不执行对应命令,比如,如果 /testdir/tests 文件不存在,就不执行我们指定的命令,此参数并不会帮助我们删除文件。

三、示例

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "ls"
ansible-demo3 | SUCCESS | rc=0 >>
anaconda-ks.cfg
CentOS7-Base-163.repo
Centos-7.repo

上面命令表示在 ansible-demo3 主机上执行 ls 命令,因为使用的是 root 用户,所以默认情况下,ls 出的结果是 ansible-demo3 主机中 root 用户家目录中的文件列表。

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "chdir=/testdir ls"
ansible-demo3 | SUCCESS | rc=0 >>
testfile1
testfile2

chdir 参数表示执行命令之前,会先进入到指定的目录中,所以上面命令表示查看 ansible-demo3 主机上 /testdir 目录中的文件列表,返回显示有2个文件。

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "creates=/testdir/testfile1 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
skipped, since /testdir/testfile1 exists

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "creates=/testdir/testfile3 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
test

上面命令表示 /testdir/testfile1 文件存在于远程主机中,则不执行对应命令。/testdir/testfile3 不存在,才执行”echo test”命令。

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "removes=/testdir/testfile1 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
test

[root@ansible-manager ~]# ansible ansible-demo3 -m command -a "removes=/testdir/testfile3 echo test"
ansible-demo3 | SUCCESS | rc=0 >>
skipped, since /testdir/testfile3 does not exist

上面命令表示 /testdir/testfile3 文件不存在于远程主机中,则不执行对应命令。/testdir/testfile1 存在,才执行”echo test”命令。

四、总结
本节介绍了 Ansible 常用模块之 command 模块,并举例说明如何使用,下节我们介绍 shell 模块。 

一、概述

shell 模块可以帮助我们在远程主机上执行命令。与 command 模块不同的是,shell 模块在远程主机中执行命令时,会经过远程主机上的 /bin/sh 程序处理。

二、常用参数
free_form参数 :必须参数,指定需要远程执行的命令,但是并没有具体的一个参数名叫free_form,具体解释参考 command 模块。

chdir参数 : 此参数的作用就是指定一个目录,在执行对应的命令之前,会先进入到 chdir 参数指定的目录中。

creates参数 :使用此参数指定一个文件,当指定的文件存在时,就不执行对应命令,可参考command 模块中的解释。

removes参数 :使用此参数指定一个文件,当指定的文件不存在时,就不执行对应命令,可参考 command 模块中的解释。

executable参数:默认情况下,shell 模块会调用远程主机中的 /bin/sh 去执行对应的命令,通常情况下,远程主机中的默认 shell 都是 bash。如果你想要使用其他类型的 shell 执行命令,则可以使用此参数指定某种类型的 shell 去执行对应的命令。

指定 shell 文件时,需要使用绝对路径。

三、示例
shell 模块中 chdir、creates、removes 参数的作用与 command 模块中的作用都是相同的,此处不再举例。

[root@ansible-manager ~]# ansible ansible-demo3 -m shell -a "chdir=/testdir echo mytest > test"
ansible-demo3 | SUCCESS | rc=0 >>

使用 shell 模块可以在远程服务器上执行命令,它支持管道与重定向等符号。上面命令打印出mytest并写入test文件。

[root@ansible-manager ~]# ansible ansible-demo3 -m shell -a "chdir=/testdir ls"
ansible-demo3 | SUCCESS | rc=0 >>
test
testfile1
testfile2

上面命令列出了 /testdir 下面的文件,多了个 test 文件。

[root@ansible-manager ~]# ansible ansible-demo3 -m shell -a "chdir=/testdir cat test"
ansible-demo3 | SUCCESS | rc=0 >>
mytest

上面命令列出了 test 文件的内容。

由于 command 比较安全有可预知性,所以我们平时用的时候,最好用 command。需要用到 shell 特性的时候,再用 shell。

四、总结
本节介绍了 Ansible 常用模块之 shell 模块,并举例说明如何使用,下节我们介绍 script 模块。

一、概述

script 模块可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,也就是说,脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行

二、常用参数
free_form参数 :必须参数,指定需要执行的脚本,脚本位于 ansible 管理主机本地,并没有具体的一个参数名叫 free_form,具体解释请参考 command 模块。

chdir参数 : 此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到 chdir 参数指定的目录中。

creates参数 :使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考 command 模块中的解释。

removes参数 :使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本,可参考 command 模块中的解释。

三、示例

[root@ansible-manager ~]# ansible ansible-demo3 -m script -a "chdir=/opt /testdir/testscript.sh"
ansible-demo3 | SUCCESS => {
"changed": true, 
"rc": 0, 
"stderr": "Shared connection to ansible-demo3 closed. ", 
"stdout": "testscript ", 
"stdout_lines": [
"testscript"
]
}

其中 testscript.sh 脚本为打印 ‘testscript’ 字符串。

[root@ansible-manager ~]# cat /testdir/testscript.sh
echo 'testscript'

上面命令表示 ansible 主机中的 /testdir/testscript.sh 脚本将在 ansible-demo3 主机中执行,执行此脚本之前,会先进入到 ansible-demo3 主机中的 /opt 目录

[root@ansible-manager ~]# ansible ansible-demo3 -m script -a "creates=/testdir/testfile1 /testdir/testscript.sh"
ansible-demo3 | SKIPPED

上面命令表示,ansible-demo3 主机中的 /testdir/testfile1文件已经存在,ansible 主机中的 /testdir/testscript.sh 脚本将不会在 ansible-demo3 主机中执行。

[root@ansible-demo3 ~]# ls /testdir/
test testfile1 testfile2

由于 testfile1 已经存在,则 SKIPPED。

[root@ansible-manager ~]# ansible ansible-demo3 -m script -a "removes=/testdir/testfile1 /testdir/testscript.sh"
ansible-demo3 | SUCCESS => {
"changed": true, 
"rc": 0, 
"stderr": "Shared connection to ansible-demo3 closed. ", 
"stdout": "testscript ", 
"stdout_lines": [
"testscript"
]
}

上面命令表示,ansible-demo3 主机中的 /testdir/testfile1 文件存在,ansible 主机中的 /testdir/testscript.sh 脚本则会在 ansible-demo3 主机中执行。

四、总结
本节介绍了 Ansible 常用模块之 script 模块,并举例说明如何使用,下节我们介绍 copy 模块。

三、script模块
模块介绍
script模块可以帮助我们在远程主机上执行ansible主机上的脚本,也就是说,脚本一直存在于ansible主机本地,不需要手动拷贝到远程主机后再执行。

学习此模块之前,请先参考本文中的command模块。

模块参数
此处我们介绍一些script模块的常用参数,你可以先对这些参数有一个大概了解,然后再看小示例。

参数 含义
free_form参数 必须参数,指定需要执行的脚本,脚本位于ansible主机本地,并没有具体的一个参数名叫free_form,具体解释参考command模块。
chdir参数 此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到chdir参数指定的目录中。
creates参数 使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考command模块中的解释。
removes参数 使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本,可参考command模块中的解释。
模型示例
上述参数对应的ad-hoc示例命令如下:

如下命令表示ansible主机中的/testdir/redhat-test.sh脚本将在testB主机中执行,执行此脚本之前,会先进入到testB主机中的/opt目录

脚本内容

[root@server4 ~]# cd /testdir/
[root@server4 testdir]# cat redhat-test.sh 
#!/bin/bash
echo "hello ansible"

执行命令:

[root@server4 testdir]ansible testB -m script -a "chdir=/opt /testdir/redhat-test.sh"

关于ansbile工具的shell、command、script、raw模块的区别和使用场景

command模块 [执行远程命令]

[root@node1 ansible]# ansible testservers -m command -a 'uname -n'

script模块 [在远程主机执行主控端的shell/python脚本 ] (使用相对路径)

[root@node1 ansible]# ansible testservers -m script -a '/etc/ansible/test.sh

shell模块 [执行远程主机的shell/python脚本]

[root@node1 ansible]# ansible testservers -m shell -a 'bash /root/test.sh'


raw模块 [类似于command模块、支持管道传递]

[root@node1 ansible]# ansible testservers -m raw -a "ifconfig eth0 |sed -n 2p |awk '{print $2}' |awk -F: '{print $2}'"

Ansible可以执行命令的模块有三个:

  command

  shell

  raw

应尽量避免使用这三个模块来执行命令,因为其他模块大部分都是幂等性的,可以自动进行更改跟踪。

command、shell、raw不具备幂等性。

command、shell模块:

  要求受管主机上安装Python。command可以在受管主机上执行shell命令,但是不支持环境变量和操作符(例如 '|', '<', '>', '&'),shell模块调用的/bin/sh指令执行。

raw模块:

  不需要受管主机上安装Python,直接使用远程shell运行命令,通常用于无法安装Python的系统(例如网络设备等)。

  

原文地址:https://www.cnblogs.com/chengxuyonghu/p/13746102.html