Hostvar variable index in template

Hi,

I have a problem and I can’t find a solutions.

In my playbook, I have a host based variable called: server_name
I would like to use in a template file the IP address of the server which hostname’s is stored in the server_name variable.
Like this:

{{ hostvars[‘{{ server_name }}’][‘ansible_eth0’][‘ipv4’][‘address’] }}

If I use this sentences in a roles, is working fine, but if I use in a template, this error happen:
FAILED! => {“changed”: false, “msg”: “AnsibleUndefinedVariable: "hostvars[‘{{ server_name }}’]" is undefined”}

Have you any idea how can I solve this problem?

Best regards,
Janos

Hi,

I have a problem and I can't find a solutions.

In my playbook, I have a host based variable called: server_name
I would like to use in a template file the IP address of the server which hostname's is stored in the server_name variable.
Like this:
{{ hostvars['{{ server_name }}']['ansible_eth0']['ipv4']['address'] }}

If I use this sentences in a roles, is working fine, but if I use in a template, this error happen:
FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: \"hostvars['{{ server_name }}']\" is undefined"}

Have you any idea how can I solve this problem?

Best regards,
Janos

If you use variables inside {{ ... }} you can't wrap them again in {{ ... }}.
Remove the curly braces and the single quotes:

   {{ hostvars[server_name]['ansible_eth0']['ipv4']['address'] }}

Regards
          Racke

Oh, Thank you so much!

Best regards,
Janos