StanS
StanS

Reputation: 49

Ansible - how to add selected attributes from dict variable into a template?

I am trying to create a report from a job. Thinking about creating a local file and emailing it. But I don't know how I can get the needed data from this variable.

This is the output of the test var

ok: [localhost] => {
    "test": {
        "changed": false,
        "failed": true,
        "msg": "One or more items failed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "elapsed": 4.0650629,
                "failed": true,
                "item": "prdrimaknmcs01.perceptive.cloud",
                "msg": "timeout while waiting for prdrimaknmcs01.perceptive.cloud:9091 to start listening",
                "wait_attempts": 2
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "elapsed": 4.0600610999999995,
                "failed": true,
                "item": "prdrimaknmcs02.perceptive.cloud",
                "msg": "timeout while waiting for prdrimaknmcs02.perceptive.cloud:9091 to start listening",
                "wait_attempts": 2
            }
        ],
        "skipped": false
    }
}

I need to add both items and messages into my template file. The problem is that this specific var is for 2 tests but it can be any number of tests from 1 to 10. So I thought about using "for" in a variable substitution in my template. Similar to this. Cannot make it work.

    {% for rslts in test %}
Message: {{ test.results.msg }}
{% endfor %}

Upvotes: 0

Views: 61

Answers (1)

phanaz
phanaz

Reputation: 1645

test.results is a list, not test, so your loop must look like this:

{% for rslt in test.results %}
Message: {{ rslt.msg }}
{% endfor %}

You iterate over the entries of test.results and have them available in the loop in rslt, so you must then access the respective value via rslt.msg.

Upvotes: 2

Related Questions