在Ansible中,shell模块无法直接注册变量。但是可以使用register参数将命令的输出结果保存到一个变量中。
以下是一个示例代码,演示了如何使用shell模块执行命令并将输出结果保存到一个变量中:
- name: Run shell command and register output
shell: ls -l
register: shell_output
- name: Print shell command output
debug:
var: shell_output.stdout_lines
在上面的示例中,shell模块执行了一个ls -l命令,并将输出结果保存到了shell_output变量中。然后,使用debug模块打印shell_output.stdout_lines变量的值。
注意,shell_output.stdout_lines是一个列表,包含了命令输出的每一行。如果想要获取整个输出作为一个字符串,可以使用shell_output.stdout。
希望以上解决方法能帮助到你!