如何使用ansible命令使用script,不写成剧本
时间: 2023-07-20 22:05:22 浏览: 44
使用ansible命令执行script可以直接使用"command"模块,例如:
```
ansible <host> -m command -a "/path/to/script.sh"
```
其中,`<host>`为目标主机,`/path/to/script.sh`为要执行的脚本路径。这条命令会在目标主机上执行指定的脚本。
你也可以通过"shell"模块执行脚本,例如:
```
ansible <host> -m shell -a "/path/to/script.sh"
```
这条命令也会在目标主机上执行指定的脚本。
需要注意的是,使用ansible执行脚本需要目标主机上已经安装了ansible,并且已经配置好了ansible的SSH连接。如果没有安装ansible或者没有配置好SSH连接,可以参考ansible的官方文档进行安装和配置。
相关问题
ansible使用script怎么写
在 Ansible 中使用 script 模块,可以在远程主机上执行本地脚本。以下是使用 script 模块的示例 Ansible 任务:
```
- name: Run local script on remote host
hosts: myhost
tasks:
- name: Execute script
script: /path/to/local/script.sh
```
在上面的示例中,`/path/to/local/script.sh` 是本地脚本的路径。在运行 Ansible 任务时,该脚本将被复制到远程主机,并在远程主机上执行。如果脚本需要传递参数,可以使用 `args` 参数,如下所示:
```
- name: Run local script on remote host with arguments
hosts: myhost
tasks:
- name: Execute script with arguments
script: /path/to/local/script.sh
args:
arg1: value1
arg2: value2
```
在上面的示例中,`arg1` 和 `arg2` 是脚本的参数名,`value1` 和 `value2` 是参数值。在本地脚本中,可以使用 `$1`、`$2` 等变量来引用这些参数。
ansible中script怎么使用
在 Ansible 中,script 模块用于在目标主机上执行本地脚本。可以使用以下语法来使用 script 模块:
```
- name: Execute script on remote host
script: /path/to/local/script.sh
```
其中,`/path/to/local/script.sh` 是要在目标主机上执行的本地脚本的路径。
此外,还可以使用 script 模块的其他参数,例如:
- `chdir`:指定脚本在目标主机上执行时要切换到的目录。
- `creates`:指定一个文件路径,如果该文件已经存在,则脚本不会被执行。
- `executable`:指定要使用的解释器,例如 `/bin/bash` 或 `/usr/bin/python`。
下面是一个示例,演示如何在目标主机上执行本地脚本,并将输出保存到文件中:
```
- name: Execute script on remote host
script: /path/to/local/script.sh
args:
chdir: /opt/myapp
register: script_output
- name: Save script output to file
copy:
content: "{{ script_output.stdout }}"
dest: /tmp/script_output.txt
```
在这个示例中,我们使用 script 模块执行了 `/path/to/local/script.sh` 脚本,并将输出保存到 `script_output` 变量中。接着,我们使用 copy 模块将输出写入到 `/tmp/script_output.txt` 文件中。