Loop with hostvars groups

9 views
Skip to first unread message

Rafael Tomelin

unread,
Aug 27, 2019, 8:06:46 AM8/27/19
to ansible...@googlegroups.com

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?



--
Atenciosamente,

Rafael Tomelin
Tel.: 51-984104084
Skype: rafael.tomelin

LPI ID: LPI000191271
Red Hat Certified Engineer
Puppet Professional 2017 Certification

vinoth kumar

unread,
Aug 27, 2019, 8:10:41 AM8/27/19
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAGEUqbCW9GZVwGQ%2BtqQopQBpOAEV-c7_08ud0TWu-AawmEDiRA%40mail.gmail.com.

Rafael Tomelin

unread,
Aug 27, 2019, 8:47:38 AM8/27/19
to ansible...@googlegroups.com
Hi Vinoth,

Thanks, your recommendation is work.

Reply all
Reply to author
Forward
0 new messages