Jinja 2 template syntax error

Hi,

Im having an issue with the following Jinja2 template and I just cant figure out how to get it to display the ip4 addresses of the hosts from the hostvars for the hosts in the inventory file below

{% for host_system in groups['hosts'] %}

{% if "ansible_eth0" in hostvars[host_system] -%}

sentinel known-sentinel mymaster {{ hostvars[host_system]['ansible_eth0']['ipv4']['address'] }} 26379 {{ hostvars[host_system]['sentinel_id'] }}

{%- endif -%}

{% endfor %}

Inventory

[hosts:children]

haproxyhosts

redishosts

[haproxyhosts]

Hahost1

Hahost2

[redishosts]

Rhost1

Rhost2

Error

fatal: [10.193.45.158]: FAILED! => {

    "changed": false,

    "failed": true,

    "invocation": {

        "module_args": {

            "daemon_reload": false,

            "enabled": true,

            "masked": null,

            "name": "redis-sentinel",

            "state": "restarted",

            "user": false

        }

    },

    "msg": "Unable to restart service redis-sentinel: Job for redis-sentinel.service failed because the control process exited with error code. See \"systemctl status redis-sentinel.service\" and \"journalctl -xe\" for details.\n"

}

fatal: [10.193.45.157]: FAILED! => {

    "changed": false,

    "failed": true,

    "invocation": {

        "module_args": {

            "daemon_reload": false,

            "enabled": true,

            "masked": null,

            "name": "redis-sentinel",

            "state": "restarted",

            "user": false

        }

    },

    "msg": "Unable to restart service redis-sentinel: Job for redis-sentinel.service failed because the control process exited with error code. See \"systemctl status redis-sentinel.service\" and \"journalctl -xe\" for details.\n"

}

What does the generated config look like?
The error is just redis-sentinel crashing.

Looks like I pastes the wrong error. The error is undefined variable ansible_eth0. It’s works when I only use one group rather than the combined group. Plus it doesn’t seem to see the host var properties for the hosts

Hi,

15 juin 2017 19:13 “Owen Corcoran” <cyperplex@gmail.com> a écrit:

What the play book is running is

{% for host_system in groups[‘hosts’] %}
sentinel known-sentinel mymaster {{ hostvars[host_system][‘ansible_eth0’][‘ipv4’][‘address’] }} 26379 {{ hostvars[host_system][‘sentinel_id’] }}
{% endfor %}

The error im getting implied the host_system variable isnt getting a value that hostvars can process to determine the values im trying to reference

“changed”: false,
“failed”: true,
“invocation”: {
“module_args”: {
“backup”: “yes”,
“dest”: “/etc/redis-sentinel.conf”,
“group”: “redis”,
“mode”: “0644”,
“owner”: “redis”,
“src”: “templates/redis-sentinel.conf.j2”
},
“module_name”: “template”
},
“msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘ansible_eth0’”

The template looks correct, so two things can result in this message, you are not running facts and/or the hosts doesn't have a interface name eth0.

You can easily check if they have eth0 with ad-hoc Ansible
ansible hosts -m setup -a "filter=ansible_eth0"

Checked all hosts, and they all have eth0 interfaces.

The loop in the template works when i limit it to one host group . I think its just not getting the vars the error is

AnsibleUnderDefinedVariable: dict object has no attribute ansible_eth0