ansible常用模块之命令操作
(1)、command模块
帮助在远程主机上执行命令,但是不会经过远程主机的shell处理。
chdir:指定一个目录,在执行命令前会先进入到参数指定的目录中去。
creates:当指定文件存在时就不执行指定的命令。
removes:当指定文件不存在时就不执行指定的命令(存在时就执行指定的命令)。
ansible testB -m command -a "ls" #默认ls家目录
ansible testB -m command -a "chdir=/testdir ls" #查询指定路径
(2)、shell模块
帮助在远程主机上执行命令,与command模块不同的是会经过远程主机的/bin/sh处理
chdir:指定一个目录,在执行命令前会先进入到参数指定的目录中去。
creates:当指定文件存在时就不执行指定的命令。
removes:当指定文件不存在时就不执行指定的命令(存在时就执行指定的命令)。
executable:默认情况下shell模块会经过远程主机的/bin/sh处理,通常情况下默认shell都是bash,可以使用此参数指定其他类型的绝对路径改变类型。
ansible testB -m shell -a "chdir=/testdir echo testdir>test"
(3)、script模块
可以在远程主机上执行ansible主机上的脚本,不需要拷贝到远程主机执行。
chdir:指定一个目录,在执行命令前会先进入到参数指定的目录中去。
creates:当指定文件存在时就不执行指定的命令。
removes:当指定文件不存在时就不执行指定的命令(存在时就执行指定的命令)。
ansible testB -m script -a "chdir=/opt /testdir/test.sh" #opt下执行ansible主机上/testdir/test.sh
ansible testB -m script -a "creates=/mnt/jj /testdir/test.sh" #有目标文件就执行