ansible.builtin.script 模块 – 在远程节点上运行本地脚本(传输后)
注意
此模块是 ansible-core 的一部分,包含在所有 Ansible 安装中。在大多数情况下,即使没有指定 集合关键字,您也可以使用简短的模块名称 script。但是,我们建议您使用 完全限定的集合名称 (FQCN) ansible.builtin.script,以便轻松链接到模块文档并避免与可能具有相同模块名称的其他集合发生冲突。
概要
ansible.builtin.script 模块采用脚本名称,后跟一个空格分隔的参数列表。
需要自由格式命令或
cmd参数,请参见示例。路径中的本地脚本将被传输到远程节点,然后执行。
给定的脚本将通过远程节点上的 shell 环境进行处理。
此模块不需要远程系统上的 Python,类似于 ansible.builtin.raw 模块。
此模块也支持 Windows 目标。
注意
此模块具有相应的 操作插件.
参数
参数 |
注释 |
|---|---|
在远程节点上运行脚本之前更改到此目录。 |
|
要运行的本地脚本的路径,后跟可选参数。 |
|
远程节点上的文件名,如果它已存在,则不会执行此步骤。 |
|
此选项控制使用 vault 自动解密源文件。 选项
|
|
用于调用脚本的可执行文件的名称或路径。 |
|
本地脚本文件的路径,后跟可选参数。 |
|
远程节点上的文件名,如果它不存在,则不会执行此步骤。 |
属性
属性 |
支持 |
描述 |
|---|---|---|
可以在检查模式下运行并返回更改状态预测,而无需修改目标,如果不受支持,则操作将被跳过。 |
||
支持:无 |
将在差异模式下返回有关更改内容(或可能需要在检查模式下更改的内容)的详细信息 |
|
平台: 全部 此操作是少数不需要远程 Python 的操作之一,因为它将命令直接传递到连接字符串中 |
可以对其进行操作的目标操作系统/系列 |
|
支持:完全 |
指示操作是否采用“原始”或“自由格式”字符串作为选项,并对其进行特殊解析 |
|
支持:无 |
使用 Ansible 的严格文件操作功能来确保正确的权限并避免数据损坏 |
|
支持:完全 |
可以自动解密 Ansible 保险库文件 |
注意
注意
通常,编写 Ansible 模块比推送脚本更可取。将您的脚本转换为 Ansible 模块以获得额外积分!
ansible.builtin.ssh 连接插件将在执行脚本时强制通过
-tt分配伪终端。伪终端没有 stderr 通道,所有 stderr 都发送到 stdout。如果您依赖于分隔的 stdout 和 stderr 结果键,请切换到包含 ansible.builtin.copy 和 ansible.builtin.command 的任务集,而不是使用 ansible.builtin.script.如果本地脚本的路径包含空格,则需要用引号将其括起来。
此模块也支持 Windows 目标。
如果脚本返回非 UTF-8 数据,则必须对其进行编码以避免出现问题。一种选择是将输出通过
base64进行管道传输。
另请参见
另请参见
- ansible.builtin.shell
在目标上执行 shell 命令。
- ansible.windows.win_shell
有关 **ansible.windows.win_shell** 模块的官方文档。
示例
- name: Run a script with arguments (free form)
ansible.builtin.script: /some/local/script.sh --some-argument 1234
- name: Run a script with arguments (using 'cmd' parameter)
ansible.builtin.script:
cmd: /some/local/script.sh --some-argument 1234
- name: Run a script only if file.txt does not exist on the remote node
ansible.builtin.script: /some/local/create_file.sh --some-argument 1234
args:
creates: /the/created/file.txt
- name: Run a script only if file.txt exists on the remote node
ansible.builtin.script: /some/local/remove_file.sh --some-argument 1234
args:
removes: /the/removed/file.txt
- name: Run a script using an executable in a non-system path
ansible.builtin.script: /some/local/script
args:
executable: /some/remote/executable
- name: Run a script using an executable in a system path
ansible.builtin.script: /some/local/script.py
args:
executable: python3
- name: Run a Powershell script on a Windows host
script: subdirectories/under/path/with/your/playbook/script.ps1