在Ansible中,可以使用when
关键字来根据返回数据进行条件判断。下面是一个示例代码:
- name: Run command and check the result
command: some_command
register: command_result
- name: Do something based on the result
debug:
msg: "The command succeeded"
when: command_result.rc == 0
- name: Do something else based on the result
debug:
msg: "The command failed"
when: command_result.rc != 0
在上面的示例中,我们首先运行一个命令并将其结果注册到command_result
变量中。然后,我们使用debug
模块根据命令的返回代码进行条件判断。如果返回代码为0,表示命令执行成功,我们输出相应的消息。如果返回代码不为0,表示命令执行失败,我们输出另外一条消息。
你可以根据实际情况修改some_command
为你想要执行的命令,并根据需要修改debug
模块中的消息内容。