How to set a default ssh user for all hosts in Ansible?

Check the example/default ansible.cfg file, and you'll find this under [defaults]:

# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
#remote_user = root

Uncomment remote_user and set the user to what you want to log in as.

Where does Ansible get ansible.cfg? The same file explains:

# nearly all parameters can be overridden in ansible-playbook 
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

Another way is to use --user to define remote ssh user. Type ansible-playbook --help to read more. This is my typical command:

ansible-playbook -i hosts site.yml --user <user> --ask-pass -vvvv

--ask-pass will prompt to enter password for --user


In addition to modifying the ansible.cfg, u can also define variables for 'all' group or other groups

https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#group-variables

[all:vars]
ansible_user = root
ansible_port = 22

Tags:

Ansible