Q: "Does not connect with the specified remote_user: root but with the user 'ansible'."
A: Very probably the remote_user is overridden by the variable ansible_user. The variable has a higher precedence. See the last section. For example
- set_fact:
ansible_user: admin
- command: whoami
remote_user: root
register: result
- debug:
var: result.stdout
give
"result.stdout": "admin"
Without the variable ansible_user this should work. remote_user is defined on the task level. For example,
- command: whoami
remote_user: admin
register: result
- debug:
var: result.stdout
- command: whoami
remote_user: root
register: result
- debug:
var: result.stdout
give
"result.stdout": "admin"
"result.stdout": "root"
Debug
Put a debug task into the code and see the value of the variable ansible_user. For example
- debug:
var: ansible_user
- name: "Create 'ansible' user"
remote_user: root
user:
name: "ansible"
Use ansible_user
Use ansible_user if there shouldn't be any chance to override the value.
See also parameter remote_user of the SSH connect plugin. remote_user is the parameter in the Ansible configuration. Instead, it is also possible to use variable ansible_user to change the remote user from a playbook, or task. The variable has the highest preference. See the last section. For example
- command: whoami
register: result
vars:
ansible_user: admin
- debug:
var: result.stdout
- command: whoami
register: result
vars:
ansible_user: root
- debug:
var: result.stdout
work as expected and give
"result.stdout": "admin"
"result.stdout": "root"
}
Best practice is to use public key authentication with the password of the private key provided by ssh-agent.
Disable root login
But, the best practice is to Disable root login: "Use a normal user account to initiate your connection instead, together with sudo." For example
- command: whoami
register: result
become: true
become_method: sudo
become_user: root
vars:
ansible_user: admin
- debug:
var: result.stdout
gives
"result.stdout": "root"
The remote user admin must be allowed sudo, of course
root@test_01> cat /usr/local/etc/sudoers
...
admin ALL=(ALL) NOPASSWD: ALL
Precedence
It's necessary to understand that many configuration parameters can be overridden by variables from play to the task level. In most cases, these variables are created from the name of the parameter by the addition of the prefix ansible_. The variable ansible_user and parameter remote_user is an exception (FWIW, I'm not aware of any other exception).
It's also important to keep in mind that the variables got precedence over playbook's keywords.
As an example, become directives can also be specified as variables. For example
- command: whoami
register: result
vars:
ansible_user: admin
ansible_become: true
ansible_become_method: sudo
ansible_become_user: root
- debug:
var: result.stdout
gives
"result.stdout": "root"