Ansible模块[01]:script
1、应用
- 在远程主机上执行ansible主机上的脚本,且不需要将脚本复制到被执行的远程主机上。
2、参数
-
脚本路径,必须参数
-
chdir:在执行脚本之前,先进入远程主机上的指定目录。
-
cmd:需要运行的本地脚本的路径,其后可跟选项或参数。
-
creates:
-
当远程主机上的该文件存在时,不运行此步骤。
-
当远程主机上不存在该文件时,运行此步骤。
-
-
removes:
- 当远程主机上的该文件不存在时,不运行此步骤。
- 当远程主机上存在该文件时,运行此步骤。
-
executable:用于调用脚本的解释器的名称或路径。
3、示例
1、运行一个带参数的脚本
- name: Run a script with arguments
script: /some/local/script.sh --some-argument 1234
2、运行一个带参数的脚本(使用“cmd”参数)。
- name: Run a script with arguments (using 'cmd' parameter)
script:
cmd: /some/local/script.sh --some-argument 1234
3、当远程节点上不存在file.txt时,运行脚本。
- name: Run a script only if file.txt does not exist on the remote node
script: /some/local/create_file.sh --some-argument 1234
args:
creates: /the/created/file.txt
4、当远程节点上存在file.txt时,运行脚本。
- name: Run a script only if file.txt exists on the remote node
script: /some/local/remove_file.sh --some-argument 1234
args:
removes: /the/removed/file.txt
5、使用系统路径中的解释器来运行脚本。
- name: Run a script using an executable in a system path
script: /some/local/script.py
args:
executable: python3
6、使用非系统路径中的解释器来运行脚本。
- name: Run a script using an executable in a non-system path
script: /some/local/script
args:
executable: /some/remote/executable