Ansible 命令执行模块(学习笔记九)
2018-03-26 本文已影响108人
SkTj
命令执行模块有四个:command、raw、shell、script
command、raw
1、command为系统默认模块,使用时可以直接省略:
ansible all -a "pwd"
![](https://img.haomeiwen.com/i9967595/c42b773d6f1f4e5e.png)
2、转换到别的目录中,执行程序,chdir为command模块自带的参数:
ansible all -a "pwd chdir=/tmp"
![](https://img.haomeiwen.com/i9967595/af32922f5c23da9d.png)
3、command不支持管道命令:
![](https://img.haomeiwen.com/i9967595/1ef8e19b1ce7dd0e.png)
4、raw和command类似,两个模块都是调用远程主机的指令,但是raw支持管道命令:
ansible all -m raw -a "cd /tmp;pwd"
![](https://img.haomeiwen.com/i9967595/deca0ff18625c40e.png)
shell、script
5、shell模块调用远程主机的指令,支持shell特性,包括执行脚本、管道命令等:
ansible all -m shell -a "cd /tmp;pwd"
![](https://img.haomeiwen.com/i9967595/60c57541bbf17c5f.png)
6、shell直接执行脚本,执行的脚本放在远程主机上:
ansible all -m shell -a "/root/test.sh"
![](https://img.haomeiwen.com/i9967595/36790d28e01b0259.png)
7、script只能执行脚本,不能调用其他指令,但是script执行的是存放在ansbile管理机上的脚本,并且script不支持管道命令:
ansible all -m script -a "/root/test.sh"
![](https://img.haomeiwen.com/i9967595/e7b42c317db22b03.png)
8、几个模块中,command是默认模块,建议使用shell,功能较方便,script和shell的区别是一个执行控制端的脚本,一个执行远程端的脚本。