Ansible 命令执行模块(学习笔记九),ansible学习笔记


命令执行模块有四个:command、raw、shell、script

command、raw

1、command为系统默认模块,使用时可以直接省略:
ansible all -a "pwd"


img_9aceb74312ae5f20bc6be288cf54251b.png image.png

2、转换到别的目录中,执行程序,chdir为command模块自带的参数:
ansible all -a "pwd chdir=/tmp"


img_a15cc1ef3d32f74ef29ee9dd1454d7a7.png image.png

3、command不支持管道命令:


img_12025b6fba05d620249a79555086ded7.png image.png

4、raw和command类似,两个模块都是调用远程主机的指令,但是raw支持管道命令:
ansible all -m raw -a "cd /tmp;pwd"


img_70b22d9e281cb2e64f23c045f503bf80.png image.png

shell、script

5、shell模块调用远程主机的指令,支持shell特性,包括执行脚本、管道命令等:
ansible all -m shell -a "cd /tmp;pwd"


img_e9efa62863cddc56a6e1ac1fb0742a89.png image.png

6、shell直接执行脚本,执行的脚本放在远程主机上:
ansible all -m shell -a "/root/test.sh"


img_6f93763b77f879428fa7c2c069a792ee.png iamge.png

7、script只能执行脚本,不能调用其他指令,但是script执行的是存放在ansbile管理机上的脚本,并且script不支持管道命令:
ansible all -m script -a "/root/test.sh"

img_6779630f2dd223235c8f30f558fce4b4.png image.png

8、几个模块中,command是默认模块,建议使用shell,功能较方便,script和shell的区别是一个执行控制端的脚本,一个执行远程端的脚本。

相关内容