要在Ansible中使用shell模块输出运行命令的结果,可以使用Ansible的register功能来存储命令的输出结果,并使用debug模块将结果打印出来。以下是一个示例代码:
- name: Run shell command and display output
hosts: localhost
tasks:
- name: Run command and store output
shell: ls -l
register: command_output
- name: Display output
debug:
var: command_output.stdout_lines
在上面的代码中,我们使用shell模块执行了一个ls -l命令,并将输出结果存储在command_output变量中。然后使用debug模块打印出command_output的stdout_lines属性,即命令的输出结果。
运行以上代码,将会输出类似以下的结果:
TASK [Display output] *************************************************************************************************
ok: [localhost] => {
"command_output.stdout_lines": [
"total 0",
"-rw-r--r-- 1 user staff 0 Oct 1 00:00 file1.txt",
"-rw-r--r-- 1 user staff 0 Oct 1 00:00 file2.txt",
"-rw-r--r-- 1 user staff 0 Oct 1 00:00 file3.txt"
]
}
这样就可以在Ansible中使用shell模块输出运行命令的结果了。