在Ansible中,可以使用ansible.builtin.shell
模块将命令序列化以在远程主机上执行。下面是一个包含代码示例的解决方法:
- name: 执行命令序列
hosts: remote_hosts
tasks:
- name: 执行命令1
ansible.builtin.shell:
cmd: "command1"
register: result1
- name: 执行命令2
ansible.builtin.shell:
cmd: "command2"
register: result2
- name: 执行命令3
ansible.builtin.shell:
cmd: "command3"
register: result3
# 可以继续添加更多的命令
- name: 打印命令结果
ansible.builtin.debug:
var:
- result1
- result2
- result3
在上面的示例中,ansible.builtin.shell
模块用于执行命令,并将结果注册到变量中。你可以根据需要继续添加更多的命令。最后,使用ansible.builtin.debug
模块打印命令的结果。
注意:在执行命令时,需要确保远程主机上已经安装了Ansible。如果远程主机上没有安装Ansible,可以使用raw
模块来执行命令,但这种方法无法序列化命令结果。