在Ansible中,可以使用register
关键字来返回每个命令的执行结果。以下是一个使用Ansible的shell
模块来执行命令并返回结果的示例:
- name: Run command and capture output
hosts: your_host
tasks:
- name: Execute command
shell: your_command
register: result
- name: Print command output
debug:
var: result.stdout
在这个示例中,我们使用了shell
模块来执行命令,并将结果保存在result
变量中。然后,使用debug
模块打印出result.stdout
,即命令的执行结果。
你也可以使用result.stderr
来获取命令的标准错误输出,或者使用result.rc
来获取命令的返回码。
以上示例可以在Ansible playbook中使用。确保将your_host
替换为实际的主机名或组名,并将your_command
替换为要执行的命令。