shell 模块在远程主机上执行命令。与 command 模块不同的是,shell 模块在远程主机中执行命令时,会经过远程主机上的 /bin/sh 程序处理。
1. 常用参数
free_form:required,指定需要远程执行的命令。
chdir: 指定工作目录,在执行对应的命令之前,会先进入到 chdir 参数指定的目录中。
creates:指定一个文件,当指定的文件存在时,就不执行对应命令。
removes:使用此参数指定一个文件,当指定的文件不存在时,就不执行对应命令。
executable:指定 shell 文件,需要使用绝对路径。通 常情况下,远程主机中的默认shell都是bash,如果你想要使用其他类型的shell执行命令,则可以使用此参数指定某种类型的shell去执行对应 的命令,指定shell文件时,需要使用绝对路径。
2. 示例
#使用shell模块可以在远程服务器上执行命令,它支持管道与重定向等符号。
[root@ansible-manager ~]# ansible host1 -m shell -a "chdir=/testdir echo mytest > test"
[root@ansible-manager ~]# ansible host1 -m shell -a "chdir=/testdir ls"
[root@ansible-manager ~]# ansible host1 -m shell -a "chdir=/testdir cat test"
如果执行的命令需要csh解析,那么可以指定使用csh在远程主机上执行对应的命令,使用csh的语法定义了一个数字类型的变量TestNum,然后将TestNum变量的值重定向到了/testdir/TestNumFile,在bash中,@符号不能用于定义变量, 所以,可以使用executable指定需要的shell类型。
[root@ansible-manager ~]# ansible test70 -m shell -a 'executable=/bin/csh @ TestNum=666 ; echo $TestNum > /testdir/TestNumFile'
————Blueicex 2020/2/2 18:06 blueice1980@126.com