在Ansible中,任务的执行结果会返回一个字典类型的值,其中包含了诸如执行成功或失败等信息。常见的返回值属性包括.changed和.found。
.changed属性用于指示Ansible是否进行了任何更改。如果发生了更改则为True,否则为False。
.found属性用于指示Ansible是否找到了符合要求的资源。如果找到了则为True,否则为False。
以下是一个简单的Ansible playbook示例,演示如何使用.changed和.found属性:
- hosts: localhost
tasks:
- name: Check if the file exists
stat:
path: /path/to/file
register: file_result
- name: Print if the file exists
debug:
msg: "File exists"
when: file_result.stat.exists
- name: Print if the file was changed
debug:
msg: "File was changed"
when: file_result.changed
在上面的示例中,我们检查了文件是否存在,然后根据结果打印了不同的消息。如果文件存在,则打印“File exists”消息,如果文件不存在则不会执行该任务。如果文件被更改了,则打印“File was changed”消息。
注意,如果使用了一些模块,例如command或shell,那么.changed属性会根据命令的实际输出来确定是否进行了更改。