The question is simple: what is the difference between ansible_user
(former ansible_ssh_user
) and remote_user
in Ansible, besides that the first one is set if configuration file and the latter one is set in plays / roles? How do they relate to -u
/ --user
command line options?
2 Answers
They both seem to be the same.
Take a look here:
# the magic variable mapping dictionary below is used to translate
# host/inventory variables to fields in the PlayContext
# object. The dictionary values are tuples, to account for aliases
# in variable names.
MAGIC_VARIABLE_MAPPING = dict(
connection = ('ansible_connection',),
remote_addr = ('ansible_ssh_host', 'ansible_host'),
remote_user = ('ansible_ssh_user', 'ansible_user'),
port = ('ansible_ssh_port', 'ansible_port'),
Source: https://github.com/ansible/ansible/blob/c600ab81ee/lib/ansible/playbook/play_context.py#L46-L55
Besides, ansible_user
is used when we want to specifiy default SSH user in ansible hosts file where as remote_user
is used in playbook context.
From https://github.com/ansible/ansible/blob/c600ab81ee/docsite/rst/intro_inventory.rst
ansible_user The default ssh user name to use.
and here is an example of using ansible_user
in ansible hosts
file:
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_user=mdehaan
-
44It's really sad how they have different names for the same option and which ssh user to log in as is not the only example. Since the first lesson everyone in our line of work learns is to never "break backward compatibility" and to not think for themselves, things like this will only pile up and cost the community countless of hours in wasted efforts to decipher the product usage. Aug 28, 2017 at 13:39
-
Features get added that cause a need for new names; in this case, connection plugins that do not use SSH. Oct 13 at 15:53
One difference between remote_user and ansible_user:
When you run a role with different users from a playbook, e.g.:
- name: Apply user configuration to user root
hosts: all
remote_user: root
- name: Apply user configuration to user murphy
hosts: all
remote_user: murphy
Then you can perform a conditional task for a distinct user by using "when: ansible_user == .." but not with "when: remote_user == ..". e.g.:
- name: Add user murphy to wheel group
user:
name: murphy
groups: wheel
append: yes
when: ansible_user == "root"
-
2The example you show actually runs two different plays with different users, not two different roles. As far as I am concerned it is not possible for now to run different roles within the same play as different users. Here is a feature request though: github.com/ansible/ansible/issues/5084– DrewDec 14, 2018 at 20:31