Q:

How to access stdout from a variable using Ansible

Getting an error while accessing stdout from a registered variable using Ansible
I have registered the task output to a variable temp_var, so the variable content will be like below.
"msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "imntg",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "changed": true,
                "failed": false,
                "item": "imntg",
                "rc": 0,
                "stderr": "",
                "stderr_lines": [],
                "stdout": "03-26-2019-06h40m21s\n03-26-2019-06h30m21s\n",
                "stdout_lines": [
                    "03-26-2019-06h40m21s",
                    "03-26-2019-06h30m21s"
                ]
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "imntg4",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "changed": true,
                "failed": false,
                "item": "imntg4",
                "rc": 0,
                "stderr": "",
                "stderr_lines": [],
                "stdout": "03-26-2019-06h40m21s\n03-26-2019-06h30m21s\n",
                "stdout_lines": [
                    "03-26-2019-06h40m21s",
                    "03-26-2019-06h30m21s"
                ]
            },
            {
                "_ansible_ignore_errors": null,
                "_ansible_item_label": "",
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "changed": false,
                "item": "",
                "skip_reason": "Conditional result was False",
                "skipped": true
            }
        ]
    }
}

I am trying to access _ansible_item_label, stdout from the above output.
The desired output should be like below.
TASK [debug] ***********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "03-26-2019-06h40m21s\n03-26-2019-06h30m21s\n - imntg",
        "03-26-2019-06h40m21s\n03-26-2019-06h30m21s\n - imntg4"
    ]
}

below is the code how I have tried .
  - debug:
      msg: "{% set temp_var_tag_list = [] %}{% for result in temp_var.results %}{{ temp_var_tag_list.append(result.stdout ~ ' - ' ~ result._ansible_item_label) }}{% endfor %}{{ temp_var_tag_list }}"

I'm able to access _ansible_item_label but getting error while accessing stdout and the same thing working, if I  accessitem instead of stdout.
any help here?

A:

this is because your results list has 3 elements, the last of them has no stdout key.
You could add an "if exists, then" logic in your jinja code, like below:
  - debug:
      msg: "{% set temp_var_tag_list = [] %}{% for result in temp_var.results %}{% if result.stdout is defined -%}{{ temp_var_tag_list.append(result.stdout ~ ' - ' ~ result._ansible_item_label) }}{%- endif %}{%- endfor %}{{ temp_var_tag_list }}"

produced output:
TASK [debug] ***********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "03-26-2019-06h40m21s\n03-26-2019-06h30m21s\n - imntg",
        "03-26-2019-06h40m21s\n03-26-2019-06h30m21s\n - imntg4"
    ]
}

hope it helps
PS: i assume your desired output is not the one you described in your question, but the one you try to achieve from the jinja code, because these two are not consistent. ("imntg4 - imntg4" is not in your stdout)

