ansible的命令操作模块<6>,ansible命令模块


ansible的前面的模块都是对受管主机中的文件进行修改或者插入操作,今天继续跟随学习ansible的命令模块,比如说我要通过ansible在受管主机上执行我们常用的某些命令,该怎么操作

command模块:

image
image
ansible test177 -m command -a "ls"
在177中以root的身份查看177当前环境下的文件或目录
image
ansible test177 -m command -a 'chdir=/root/test ls'
在177上的test目录下执行ls命令:
image
ansible test177 -m command -a "creates=/root/test/file2 echo love"
在远程主机上找是否有file2文件,如果有就不输出,没有就输出love:

image

shell模块:

shell模块可以在远程主机上执行命令,与command不同的是,shell执行命令时会经过远程主机的bin/sh进行处理
image
以上是shell的参数
当然shell最大的好处就是它识别管道符和导入符号
ansible test177 -m shell -a "chdir=/root/test/ echo test > file3"
以上命令表示在177的test目录下面,将test这个字符串导入到test目录下的file3文件中
image
image

script模块:

可以帮助我们在受管机上执行我们ansible主机的脚本文件
image
ansible test177 -m script -a "chdir=/root/test /root/sa.sh"
表示在177的test目录下面执行ansible主机root/sa.sh脚本
image
ansible test177 -m script -a "creates=/root/test/file1 /root/sa.sh"
表示如果file1这个文件在受管主机上存在,我就不执行此脚本,如果存在就行这个及脚本
我远程主机上有file1但没有file5
image
我的脚本只是一个简单的echo‘so young’
我们还可以将上述命令中的creates换成removes,意思是相反的,如果没有就不执行,如果有就执行

相关内容