How do the group_vars and host_vars directories work, and where does Ansible look for them?
Create group_vars/ and host_vars/ next to the inventory or the playbook. A file named after a group (group_vars/servers) holds that group's variables; a file named after a host (host_vars/demo1.example.com) holds that host's. Ansible looks next to both the inventory and the playbook; if both exist, the playbook's directories take precedence.
* The file name is the link: it must match the group or host name in the inventory exactly. *
The files use the same YAML key-value syntax as a vars: block:
# group_vars/servers
user: joe
No include statement is needed; Ansible loads them automatically for every host in the matching group, or for the matching host. That's what makes them the preferred home for inventory variables: the inventory stays a list of hosts, and each group's or host's settings live in a file whose name says what it's for.
Gotcha: the name must match exactly. A file group_vars/webserver does nothing for a group called webservers, and Ansible does not warn you.
Go deeper:
Organizing host and group variables (official docs) โ file naming, directories per group, and lookup locations.