ansible command 模块:
command - Executes a command on a remote node
在一个远程主机上执行一个命令:
简介:
command 模块接收command 名字跟着空格分隔的参数列表
[esb@esbapp1 sbin]$ ./ansible -i esbhosts esbapp2 -m command -a 'id'
1.1.1.1 | SUCCESS | rc=0 >>
uid=501(esb) gid=501(esbgrp) 组=501(esbgrp)
给定的命令 会在所有选择的节点上执行。 它不会通过shell进行处理,因此变量像$HOME和操作像
"<", ">", "|", ";" and "&" 不能工作(使用shell 模块 如果你需要那些功能)
参数
argv
(added in 2.6
允许用户提供命令作为一个列表
chdir
在运行命令前 改变目录
[esb@esbapp1 sbin]$ ssh esbapp2 "cat /tmp/aaa"
cat: /tmp/aaa: 没有那个文件或目录
[esb@esbapp1 sbin]$
[esb@esbapp1 sbin]$ ./ansible -i esbhosts esbapp2 -m command -a 'touch aaa chdir=/tmp'
[WARNING]: Consider using file module with state=touch rather than running touch
1.1.1.1 | SUCCESS | rc=0 >>
[esb@esbapp1 sbin]$ ssh esbapp2 "cat /tmp/aaa"
[esb@esbapp1 sbin]$
creates:
一个文件名 当他存在时.这个步骤不会运行
[esb@esbapp1 sbin]$ ./ansible -i esbhosts esbapp2 -m command -a 'hostname creates=/tmp/aaa'
1.1.1.1 | SUCCESS | rc=0 >>
skipped, since /tmp/aaa exists
[esb@esbapp1 sbin]$ ./ansible -i esbhosts esbapp2 -m command -a 'hostname creates=/tmp/bbb'
1.1.1.1 | SUCCESS | rc=0 >>
esbapp2
free_form
command 模块使用一种自由格式命令来运行
注意:
如果你需要运行 命令行通过shell( 比如说你使用 <, >, |, etc),
你实际需要shell模块代替, 解析shell 元字符 可以导致一个意外的命令被执行 如果
引号没有正常完成