Configuring the Default SSH User
To set the default SSH user for Ansible connections, you can use the ansible_user
variable. This variable can be defined at different levels, including the global, inventory, and task levels.
Global Configuration
To set the default SSH user globally, you can add the following line to your Ansible configuration file (typically located at ~/.ansible.cfg
or /etc/ansible/ansible.cfg
):
[defaults]
ansible_user = myuser
This will set the default SSH user to myuser
for all Ansible connections.
Inventory Configuration
Alternatively, you can set the default SSH user at the inventory level. In your inventory file (e.g., hosts.yml
), you can add the ansible_user
variable to the appropriate host or group:
all:
hosts:
webserver1.example.com:
ansible_user: myuser
webserver2.example.com:
ansible_user: anotheruser
This will set the default SSH user to myuser
for webserver1.example.com
and anotheruser
for webserver2.example.com
.
Task Configuration
You can also set the default SSH user at the task level by using the ansible_user
variable in your playbook:
- hosts: webservers
tasks:
- name: Install Apache
apt:
name: apache2
state: present
become: true
become_user: myuser
In this example, the become_user
option is used to specify the user that should be used to execute the task, which overrides the default SSH user.
By using these configuration options, you can ensure that Ansible connects to your remote hosts using the appropriate SSH user, making your automation workflows more secure and reliable.