Ansible 解析调试输出

Ansible parse debug output

我使用 ansible 调试模块从 3 个不同的主机获得了这个 ansible 输出:

TASK [debug] ***************************************************************************************************************************************************************************************
ok: [10.240.22.44] => {
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": true,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": true,
                "cmd": "/opt/confluent/bin/nodefirmware smm1",
                "delta": "0:00:00.128325",
                "end": "2020-02-05 11:22:19.435049",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "/opt/confluent/bin/nodefirmware smm1",
                        "_uses_shell": true,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "warn": true
                    }
                },
                "item": "10.240.18.20",
                "rc": 0,
                "start": "2020-02-05 11:22:19.306724",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "smm1: SMM: 1.10 (TESM14F)\nsmm1: PSOC: 0.7",
                "stdout_lines": [
                    "smm1: SMM: 1.10 (TESM14F)",
                    "smm1: PSOC: 0.7"
                ]
            },
            {
                "_ansible_ignore_errors": true,
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "_ansible_parsed": true,
                "changed": true,
                "cmd": "/opt/confluent/bin/nodefirmware smm1",
                "delta": "0:00:00.096292",
                "end": "2020-02-05 11:22:22.847949",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "/opt/confluent/bin/nodefirmware smm1",
                        "_uses_shell": true,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "warn": true
                    }
                },
                "item": "10.240.19.21",
                "rc": 0,
                "start": "2020-02-05 11:22:22.751657",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "smm1: SMM: 1.10 (TESM14F)\nsmm1: PSOC: 0.7",
                "stdout_lines": [
                    "smm1: SMM: 1.10 (TESM14F)",
                    "smm1: PSOC: 0.7"
                ]
            }
        ]
    }
}

我正在尝试解析此输出并最终显示,对于每个主机,严格来说:

"stdout_lines": [
                    "smm1: SMM: 1.10 (TESM14F)",
                    "smm1: PSOC: 0.7"

显示上述输出的剧本是这样的:

   - debug:
       msg: "{{ smm_output }}"
     when: "('primary' in default_hostname or 'Primary' in default_hostname)"
     tags: ['ic', 'smm']

我尝试使用“{{ smm_output.stdout_lines }}”但是说没有这样的字典对象

有什么线索吗?

您正在注册循环任务的输出。引用文档:

When you register a variable in a task with a loop, the registered variable contains a value for each item in the loop. The data structure placed in the variable during the loop will contain a results attribute, that is a list of all responses from the module. For a more in-depth example of how this works, see the Loops section on using register with a loop.

要调试 smm_output 中各自 results 的每个 stdout_lines,您可以使用以下任务:

    - name: debug result
      debug:
        var: item.stdout_lines
      loop: "{{ smm_output.results }}"

参考:https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables