Can you define variables in the inventory, and where should they preferably live?
Yes: inventory variables apply only to particular hosts or groups. But it is usually better to put them in separate host_vars/ and group_vars/ directories than directly into the inventory file.
A playbook often needs values that differ per machine or per group: the HTTP port of the web servers, the SSH user of a legacy box, the datacentre a host sits in. Ansible lets you attach them in the inventory itself, for example web1.example.com http_port=8080 in INI, or a [webservers:vars] section for a whole group.
That works for a handful of values, but it mixes two jobs in one file: which hosts exist and how they are configured. As the values grow, the inventory becomes hard to read and to review. The cleaner convention is a directory next to the inventory:
inventory
group_vars/
webservers.yml # applies to every host in [webservers]
host_vars/
web1.example.com.yml # applies to that one host
Ansible loads these files automatically by name, so each one stays small, can be versioned on its own and is easy to find.
Go deeper:
Organizing host and group variables (official docs) โ
host_vars/andgroup_vars/layout and how values are merged.