How do ansible.cfg settings decide which inventory file is used without -i, and which user and SSH key Ansible logs in with?
inventory = hosts.yml in [defaults] makes hosts.yml the default inventory; remote_user = ansibleuser sets the login user; private_key_file = /path/to/key sets which private SSH key to authenticate with (same as --private-key on the command line).
[defaults]
inventory = hosts.yml
remote_user = ansibleuser
private_key_file = /path/to/private/key
These three lines remove the need to repeat connection details on every command. With them in the project's ansible.cfg, ansible all --list-hosts reads hosts.yml automatically, and every connection logs in as ansibleuser with that key.
Lines starting with # in a generated ansible.cfg (like #remote_user = root) are commented-out defaults: they document the setting but have no effect until you remove the #. If remote_user stays unset, Ansible falls back to the connection plugin's default, normally the user running Ansible.
Go deeper:
Ansible Configuration Settings (official docs) —
DEFAULT_HOST_LIST,DEFAULT_REMOTE_USER,DEFAULT_PRIVATE_KEY_FILE.Connection methods and details (official docs) — how user and key selection interacts with SSH.