Information about Ansible: magic variables
You can access information about Ansible operations, including the Python version being used, the hosts and groups in inventory, and the directories for playbooks and roles, using “magic” variables. Like connection variables, magic variables areSpecial Variables. Magic variable names are reserved - do not set variables with these names. The variableenvironmentis also reserved.
The most commonly used magic variables arehostvars,groups,group_names, andinventory_hostname. Withhostvars, you can access variables defined for any host in the play, at any point in a playbook. You can access Ansible facts using thehostvarsvariable too, but only after you have gathered (or cached) facts.  Note that variables defined at play objects are not defined for specific hosts and therefore are not mapped to hostvars.
If you want to configure your database server using the value of a ‘fact’ from another node, or the value of an inventory variable assigned to another node, you can usehostvarsin a template or on an action line:
Withgroups, a list of all the groups (and hosts) in the inventory, you can enumerate all hosts within a group. For example:
You can usegroupsandhostvarstogether to find all the IP addresses in a group.
You can use this approach to point a frontend proxy server to all the hosts in your app servers group, to set up the correct firewall rules between servers, and so on. You must either cache facts or gather facts for those hosts before the task that fills out the template.