0

I have the following output resulted from a shell command which is run on ansible on every host from my inventory:

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"
                ]
            }
        ]
    }
}

The information I need to extract and display on screen is this (for every host):

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

The previous command with the full output is registered as a variable using ansible register module:

- name: Get smm version
  shell: some command
  register: smm_output

Then I'm trying to display it using (which displays the full output):

- debug:
    msg: "{{ smm_output }}"

If I'm using msg: "{{ smm_output.results.stdout_lines }}" it returns error

I also tried with json_query filter but with no results

So how do I parse that output?

1
1

The registered variable smm_output is a dict object and you can correctly dive into it. But you cannot use smm_output.results.stdout_lines because smm_output.results is a list. You need to iterate over it or filter the first element of results. So, if you know its only one element in the list, you should use smm_output.results.0.stdout_lines.

0

So there's something wild going on there with how that command is being called. It's not just being executed on each host (as one night expect) but it looks sus to me because the ok is from 10.240.22.44 but the contents of the variable indicates that the commands actually ran on 10.240.18.21 and 10.240.19.20.

Which I can only imagine happening if you were to run the task something like this:

- name: run command
  shell: /opt/confluent/bin/nodefirmware smm1
  loop:
    - 10.240.18.21
    - 10.240.19.20
  register: smm_output

I assume that's not what you're doing because the loop there is pointless, but if you were intending to run the command on each host then not looping and just using the variable would work.

In the likely scenario that you need to run the command on each host individually 1 at a time then you should be able to delegate the facts to those hosts and look it up in the hostvars

Alternatively if the command really does need to run from that one host, registering results when looks are involved is a known PITA that I believe the ansible docs cover what the result is and it's pretty much universally going to be a pain to parse later.

However I suspect that there's something else going on here and this is an XY Problem type thing and you are trying to achieve something specific but are now Yak Shaving other problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.