Ansible剧本返回结果是指在执行Ansible剧本后,获取到剧本执行结果的方式。以下是一种解决方法,包含代码示例:
register
关键字将剧本执行结果保存到一个变量中。- name: Execute a command
hosts: all
tasks:
- name: Run the command
command: echo "Hello, World!"
register: command_output
在上面的示例中,我们使用register
关键字将echo "Hello, World!"
命令的输出结果保存到command_output
变量中。
debug
模块打印出剧本执行结果。- name: Print the command output
debug:
var: command_output.stdout
上面的示例中,我们使用debug
模块打印出command_output.stdout
变量的值,即命令的标准输出。
完整的剧本示例:
- name: Execute a command and print the output
hosts: all
tasks:
- name: Run the command
command: echo "Hello, World!"
register: command_output
- name: Print the command output
debug:
var: command_output.stdout
在执行上述剧本后,将会输出Hello, World!
作为结果。
希望以上解决方法对您有所帮助!