What you want to use is simply {{ inventory_hostname }} (or {{ inventory_hostname_short }} for the short name).
The hostvars object is a way to access the variables of every host that Ansible knows about. Thus, hostvars[inventory_hostname] will provide you with an object containing all known facts about the current host, hostvars['foo'] will provide you with an object containing all known facts about the host "foo", etc.
Suppose you have a host group called "db_servers" and you want to create a list of the IP addresses of all these hosts in the template. Here's how you do it:
{% for host in groups['db_servers'] %} {{ hostvars[host]['ansible_eth0']['ipv4']['address'] }} {% endfor %}
source share