返回值

Ansible 模块通常返回一个可以注册到变量中的数据结构,或者在 ansible 程序输出时直接看到。每个模块可以选择性地记录其自己独特的返回值(可通过 ansible-doc 和 主文档站点查看)。

本文档涵盖所有模块通用的返回值。

注意

其中一些键可能由 Ansible 本身在处理模块的返回信息后设置。

通用

backup_file

对于那些在操作文件时实现 backup=no|yes 的模块,如果原始文件被更改,则为创建的备份文件的路径。

"backup_file": "./foo.txt.32729.2020-07-30@06:24:19~"

changed

一个布尔值,指示该任务是否必须对目标主机或委托主机进行更改。

"changed": true

diff

关于先前状态和当前状态之间差异的信息。通常是一个包含条目 beforeafter 的字典,然后将由回调插件将其格式化为 diff 视图。

"diff": [
        {
            "after": "",
            "after_header": "foo.txt (content)",
            "before": "",
            "before_header": "foo.txt (content)"
        },
        {
            "after_header": "foo.txt (file attributes)",
            "before_header": "foo.txt (file attributes)"
        }

failed

一个布尔值,指示任务是否失败。

"failed": false

invocation

有关如何调用模块的信息。

"invocation": {
        "module_args": {
            "_original_basename": "foo.txt",
            "attributes": null,
            "backup": true,
            "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
            "content": null,
            "delimiter": null,
            "dest": "./foo.txt",
            "directory_mode": null,
            "follow": false,
            "force": true,
            "group": null,
            "local_follow": null,
            "mode": "666",
            "owner": null,
            "regexp": null,
            "remote_src": null,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": "/Users/foo/.ansible/tmp/ansible-tmp-1596115458.110205-105717464505158/source",
            "unsafe_writes": null,
            "validate": null
        }

msg

一个字符串,其中包含传递给用户的通用消息。

"msg": "line added"

rc

一些模块执行命令行实用程序或旨在直接执行命令(raw、shell、command 等),此字段包含这些实用程序的“返回代码”。

"rc": 257

results

如果此键存在,则表示该任务存在循环,并且它包含每个项目的正常模块“结果”列表。

"results": [
    {
        "ansible_loop_var": "item",
        "backup": "foo.txt.83170.2020-07-30@07:03:05~",
        "changed": true,
        "diff": [
            {
                "after": "",
                "after_header": "foo.txt (content)",
                "before": "",
                "before_header": "foo.txt (content)"
            },
            {
                "after_header": "foo.txt (file attributes)",
                "before_header": "foo.txt (file attributes)"
            }
        ],
        "failed": false,
        "invocation": {
            "module_args": {
                "attributes": null,
                "backrefs": false,
                "backup": true
            }
        },
        "item": "foo",
        "msg": "line added"
    },
    {
        "ansible_loop_var": "item",
        "backup": "foo.txt.83187.2020-07-30@07:03:05~",
        "changed": true,
        "diff": [
            {
                "after": "",
                "after_header": "foo.txt (content)",
                "before": "",
                "before_header": "foo.txt (content)"
            },
            {
                "after_header": "foo.txt (file attributes)",
                "before_header": "foo.txt (file attributes)"
            }
        ],
        "failed": false,
        "invocation": {
            "module_args": {
                "attributes": null,
                "backrefs": false,
                "backup": true
            }
        },
        "item": "bar",
        "msg": "line added"
    }
    ]

skipped

一个布尔值,指示该任务是否被跳过

"skipped": true

stderr

一些模块执行命令行实用程序或旨在直接执行命令(raw、shell、command 等),此字段包含这些实用程序的错误输出。

"stderr": "ls: foo: No such file or directory"

stderr_lines

当返回 stderr 时,我们还会始终提供此字段,该字段是字符串列表,原始输出中的每一行对应一个项目。

"stderr_lines": [
        "ls: doesntexist: No such file or directory"
        ]

stdout

一些模块执行命令行实用程序或旨在直接执行命令(raw、shell、command 等)。此字段包含这些实用程序的正常输出。

"stdout": "foo!"

stdout_lines

当返回 stdout 时,Ansible 始终提供一个字符串列表,其中每个字符串包含原始输出中的一行。

"stdout_lines": [
"foo!"
]

内部使用

这些键可以由模块添加,但会从注册的变量中删除;它们由 Ansible 本身“消耗”。

ansible_facts

此键应包含一个字典,该字典将附加到分配给主机的 facts。这些将可以直接访问,不需要使用注册变量。

exception

此键可以包含模块中异常引起的追溯信息。它仅在高详细级别 (-vvv) 下显示。

warnings

此键包含将呈现给用户的字符串列表。

deprecations

此键包含将呈现给用户的字典列表。字典的键为 msgversion,值是字符串,version 键的值可以是空字符串。

另请参阅

集合索引

浏览现有集合、模块和插件

GitHub 模块目录

浏览核心和额外模块的源代码

沟通

有疑问?需要帮助?想分享你的想法?请访问 Ansible 通信指南