简体   繁体   中英

Ansible hostvars and template

I have haproxy role , variables defined under vars/main.yml and template defined under template/haproxy.cfg.j2

in main.yml var file I defined this variable : iface: ansible_default_ipv4.interface

in the template file:

backend app
balance     roundrobin
{% for host in groups['web'] %}
    server {{ host }} {{ hostvars[host]['ansible_'+ iface ]['ipv4']['address'] }}:80 check
{% endfor %}

The problem is when I execute the playbook I get the following error : changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute u'ansible_ansible_default_ipv4.interface'

So as I know I have problem in the concatenation ['ansible_'+iface].

Please your help.

You have ansible_ on both the variable definition

iface: ansible_default_ipv4.interface`

and the template

server {{ host }} {{ hostvars[host]['ansible_'+ iface ]['ipv4']['address'] }}:80 check

As such, the concatenation creates the name ansible_ansible_default_ipv4.interface that you see in the error message.

Remove it from one of these places and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM