<p>The registered variable <code>smm_output</code> is a dict object and you can correctly dive into it. But you cannot use <code>smm_output.results.stdout_lines</code> because <code>smm_output.results</code> 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 <code>smm_output.results.0.stdout_lines</code>.</p>

CC BY-SA 4.0
2020-02-05T15:38:07.967Z
2020-02-05T15:38:07.967Z
TRW
<p>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 <code>ok</code> is from <code>10.240.22.44</code> but the contents of the variable indicates that the commands actually ran on <code>10.240.18.21</code> and <code>10.240.19.20</code>.</p>

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

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

<p>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.</p>

<p>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 <code>hostvars</code></p>

<p>Alternatively if the command really does need to run from that one host, registering results when looks are involved is a known PITA that <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#registering-variables-with-a-loop" rel="nofollow noreferrer" title="Registering Variables With a Loop">I believe the ansible docs cover what the result is</a> and it's pretty much universally going to be a pain to parse later.</p>

<p>However I suspect that there's something else going on here and this is an <a href="https://en.wikipedia.org/wiki/XY_problem" rel="nofollow noreferrer" title="XY Problem">XY Problem</a> type thing and you are trying to achieve something specific but are now <a href="https://en.wiktionary.org/wiki/yak_shaving" rel="nofollow noreferrer" title="Yak Shaving">Yak Shaving</a> other problems.</p>

CC BY-SA 4.0
2020-02-29T05:27:59.737Z
2020-02-29T05:27:59.737Z
hvindin
<p>I have the following output resulted from a shell command which is run on ansible on every host from my inventory:</p>

<pre><code>TASK [debug]
ok: [10.240.22.44] =&gt; {
    "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"
                ]
            }
        ]
    }
}
</code></pre>

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

<pre><code>"stdout_lines": [
    "smm1: SMM: 1.10 (TESM14F)",
    "smm1: PSOC: 0.7"
</code></pre>

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

<pre><code>- name: Get smm version
  shell: some command
  register: smm_output
</code></pre>

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

<pre><code>- debug:
    msg: "{{ smm_output }}"
</code></pre>

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

<p>I also tried with <strong>json_query</strong> filter but with no results</p>

<p>So how do I parse that output?</p>

CC BY-SA 4.0
2020-02-05T15:15:00.223Z
While not a direct answer, I believe this answer provides a solution to a similar problem. https://stackoverflow.com/a/31881763/6177193
Argyle
<ansible>
ansible filter debug output
Bogdan Stoica
# ansible filter debug output

(Asked by: username_0 on 2020-02-05)

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?

**Tags:** ansible

> **Comments:**
> **username_1**: While not a direct answer, I believe this answer provides a solution to a similar problem. https://stackoverflow.com/a/31881763/6177193  

---

## 2 answers

### Answer by username_2 on 2020-02-05. Score: 2

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`.


---

### Answer by username_3 on 2020-02-29. Score: 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.


---

