文章目录
script 模块
指定本地的脚本文件,到远程主机运行一次
注意:和 shell 模块的不同,shell 模块是要求客户端上有这个脚本才能执行;script 是要求 ansible 服务端有这个脚本就可以了,执行的时候是不会拷贝这个脚本到客户端的。
[root@master ~]# ansible-doc -s script
- name: Runs a local script on a remote node after transferring it # 运行一个本地的脚本在远程的机器上
script:
chdir: # 此参数的作用就是指定一个远程主机中的目录,在执行对应的脚本之前,会先进入到 chdir 参数指定的目录中
cmd: # Path to the local script to run followed by optional arguments.
creates: # 使用此参数指定一个远程主机中的文件,当指定的文件存在时,就不执行对应脚本,可参考 command 模块中的解释。
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: # Path to the local script file followed by optional arguments.
removes: # 使用此参数指定一个远程主机中的文件,当指定的文件不存在时,就不执行对应脚本,可参考 command 模块中的解释。
参数含义和 shell 中类似
free_form= # 必须参数,指定需要执行的脚本,脚本位于 ansible 管理主机本地,并没有具体的一个参数名叫 free_form,具体解释请参考 command 模块。
- 测试案例:
# 在本地准备一个测试脚本
[root@master ~]# echo -e "pwd\nhostname" > /root/test.sh
[root@master ~]# cat test.sh
pwd
hostname
# 使用 ansible 测试使用本地脚本在远端机器上执行,此时的脚本是不需要在远程机器上存在的
[root@master ~]# ansible dong -m script -a "/root/test.sh"
192.168.169.161 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.169.161 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.169.161 closed."
],
"stdout": "/root\r\nnode01\r\n",
"stdout_lines": [
"/root",
"node01"
]
}
192.168.169.162 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.169.162 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.169.162 closed."
],
"stdout": "/root\r\nnode02\r\n",
"stdout_lines": [
"/root",
"node02"
]
}