ansible 执行远端脚本返回结果
时间: 2024-03-07 12:44:28 浏览: 46
在Ansible中,可以使用`command`模块或`shell`模块来执行远程脚本并返回结果。
1. 使用`command`模块:
`command`模块用于在远程主机上执行命令,并将命令的输出作为结果返回。以下是使用`command`模块执行远程脚本并返回结果的示例:
```yaml
- name: Execute remote script using command module
hosts: your_host
tasks:
- name: Run remote script
command: /path/to/your_script.sh
register: script_output
- name: Print script output
debug:
var: script_output.stdout
```
在上面的示例中,`/path/to/your_script.sh`是要在远程主机上执行的脚本。`register`参数用于将命令的输出保存到变量`script_output`中,然后可以使用`debug`模块打印输出。
2. 使用`shell`模块:
`shell`模块也可以用于执行远程脚本并返回结果。以下是使用`shell`模块执行远程脚本并返回结果的示例:
```yaml
- name: Execute remote script using shell module
hosts: your_host
tasks:
- name: Run remote script
shell: /path/to/your_script.sh
register: script_output
- name: Print script output
debug:
var: script_output.stdout
```
在上面的示例中,`/path/to/your_script.sh`是要在远程主机上执行的脚本。同样,使用`register`参数将命令的输出保存到变量`script_output`中,并使用`debug`模块打印输出。
这样,你就可以使用Ansible执行远程脚本并获取返回结果了。