Loop with hostvars groups

Hi,

I have an inventory with groups this configuration the DB Servers:

[db]
192.168.0.1
192.168.0.2

192.168.0.3

I want to do loop the groups and get variable ipaddress in template.j2. My loop is:
{% for host in groups[‘db’] -%}
host all postgres {{ hostvars[host].ansible_hostname }}/32 trust
{% endfor %}

My error

“msg”: “AnsibleUndefinedVariable: ‘ansible.vars.hostvars.HostVarsVars object’ has no attribute ‘ansible_hostname’”,

What`s correct configuration?

Hi
remove hostvars[host].ansible_hostname and try with hostvars[host].ansible_facts.hostname if you are doing it from setup module or gayher_facts

If it’s not fixed put your playbook here

Hi Vinoth,

Thanks, your recommendation is work.