If I have a following entry in my Ansible hosts
file:
[dbserver]
myserver
Then elsewhere in my task code I can refer to myserver
as {{ groups['dbserver'][0] }}
to get the hostname dynamically. Works great.
Now - similar scenario. Say I changed the hosts
file to:
[dbserver]
db1 ansible_host=myserver ansible_user=myuser
What I found out that {{ groups['dbserver'][0] }}
will now return "db1" and it seems to be a plain string. But what if I need to refer to both the actual hostname "myserver" and also need to know the user "myuser". How do I access these values?
ansible_host
andansible_user
are defined for a specific hosts, so they are accessible withhostvars
: