command模块
在远程主机上执行命令,但不会经过远程主机shell处理,因此重定向、管道等操作会失效
[centos@self1-centos7-2 ~]$ ansible-doc -s command
- name: Execute commands on targets
command:
argv: # Passes the command as a list rather than a string. Use `argv' to avoid quoting values that would otherwise be interpreted
incorrectly (for example "user name"). Only the string or the list form can be provided,
not both. One or the other must be provided.
chdir: # 指定一个目录,在执行对应的命令之前,先进入这个目录
creates: # 当creates指定的名字或glob匹配到文件名时,不执行对应的命令
free_form: # (required) 指定远程需要运行的命令
removes: # 与creates相反,文件名没匹配到就不执行操作.
stdin: # Set the 标准输入 of the command directly to the specified value.
stdin_add_newline: # If set to `yes', append a newline to stdin data.
strip_empty_ends: # Strip去除 empty lines from the end of stdout/stderr in result.
warn: # Enable or disable task warnings.
示例:
[root@self1-centos7-2 17:09:52 ~]#ansible self1-1 -m command -a "ls"
self1-1 | CHANGED | rc=0 >>
anaconda-ks.cfg
original-ks.cfg
[root@self1-centos7-2 17:10:42 ~]#ansible self1-1 -m command -a "chdir=/test ls"
self1-1 | CHANGED | rc=0 >>
filetestfind.txt
rc.local
[root@self1-centos7-2 17:11:09 ~]#ansible self1-1 -m command -a "creates=/test/rc.local echo fileexist!"
-bash: !": event not found
[root@self1-centos7-2 17:12:05 ~]#ansible self1-1 -m command -a 'creates=/test/rc.local echo "file exist!"'
self1-1 | SUCCESS | rc=0 >>
skipped, since /test/rc.local exists
[root@self1-centos7-2 17:14:06 ~]#ansible self1-1 -m command -a 'creates=/test/rc.loca echo "file is not exist!"'
self1-1 | CHANGED | rc=0 >>
file is not exist!
[root@self1-centos7-2 17:14:28 ~]#ansible self1-1 -m command -a 'removes=/test/rc.local echo "file is exist!"'
self1-1 | CHANGED | rc=0 >>
file is exist!
shell模块
也是在远程主机上执行命令,与command不同的是,shell模块在远程主机中执行命令是,会经过远程主机上/bin/sh程序处理。有这个就不需要command?
[root@self1-centos7-2 17:15:08 ~]#ansible-doc -s shell
- name: Execute shell commands on targets
shell:
chdir: # 首先进入的目录
creates: # 匹配到不执行命令.
executable: # 默认为bash,如果用别的shell,在这里指定.
free_form: # (required) 要执行的命令
removes: # 没有匹配到不执行命令.
stdin: # Set the stdin of the command directly to the specified value.
stdin_add_newline: # Whether to append a newline to stdin data.
warn: # Whether to enable task warnings.
示例:
支持管道和重定向
[root@self1-centos7-2 17:16:49 ~]#ansible self1-1 -m shell -a 'chdir=/test echo "file is not exist!" > filetestfind'
self1-1 | CHANGED | rc=0 >>
[centos@self1-centos7-3 test]$ cat filetestfind
file is not exist!
[root@self1-centos7-2 17:30:31 ~]#ansible self1-1 -m shell -a "executable=/bin/csh @ chdir=test;echo $chdir >> /test/filetestfind"
script模块
script模块可以在远程主机上执行管理主机上的脚本,不需要拷贝后才能执行
[root@self1-centos7-2 17:33:23 ~]#ansible-doc -s script
- name: Runs a local script on a remote node after transferring it
script:
chdir: # Change into this directory on the remote node before running the script.
creates: # A filename on the remote node, when it already exists, this step will *not* be run.
decrypt: # This option controls the autodecryption of source files using vault.
executable: # Name or path of a executable to invoke the script with.
free_form: # (required) 指定要执行的脚本
removes: # A filename on the remote node, when it does not exist, this step will *not* be run.
示例:顺便回顾一下前面用过的命令
[root@self1-centos7-2 17:38:49 ~]#ansible database -m copy -a "src=/etc/fstab dest=/test/"
self1-1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
...
}
[root@self1-centos7-2 17:40:39 ~]#ansible database -m shell -a "ls /test"
self1-1 | CHANGED | rc=0 >>
filetestfind
filetestfind.txt
fstab
rc.local
[root@self1-centos7-2 17:41:25 ~]#vim line.sh
[root@self1-centos7-2 17:43:50 ~]#cat line.sh
#!/bin/bash
linecount="$(wc -l $1 | cut -d' ' -f1)"
echo "$1 has $linecount lines."
[root@self1-centos7-2 17:44:05 ~]#chmod +x line.sh
[root@self1-centos7-2 17:44:25 ~]#ll line.sh
-rwxr-xr-x. 1 root root 84 Jun 12 17:43 line.sh
[root@self1-centos7-2 17:45:42 ~]#ansible database -m script -a 'chdir=/test ./line.sh fstab'
self1-1 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 10.1.0.10 closed.\r\n",
"stderr_lines": [
"Shared connection to 10.1.0.10 closed."
],
"stdout": "fstab has 9 lines.\r\n",
"stdout_lines": [
"fstab has 9 lines."
]
}
[root@self1-centos7-2 17:46:50 ~]#ansible database -m script -a 'chdir=/test removes=/test/rc.local ./line.sh rc.local'
self1-1 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 10.1.0.10 closed.\r\n",
"stderr_lines": [
"Shared connection to 10.1.0.10 closed."
],
"stdout": "rc.local has 24 lines.\r\n",
"stdout_lines": [
"rc.local has 24 lines."
]
}