Let's say you have a playbook that does not include the remote_user parameter.
---
- hosts: all
tasks:
- name: demo
stat:
path: /path/to/example.txt
register: out
...
Let's say john.doe runs this playbook.
[john.doe@ansible1]# ansible-playbook foo.yml -i server1.example.com,
Running this playbook should return the following. Notice the output does not identify that john.doe made the SSH connection.
PLAY [all]
TASK [demo]
ok: [server1.example.com]
PLAY RECAP
server1.example.com : ok=1 changed=0 unreachable=0 failed=0
The ansible_user_id variable can be used.
- hosts: all
tasks:
- name: store the stats of example.txt in the 'out' variable
stat:
path: /path/to/example.txt
register: out
- debug:
var: ansible_user_id
...
Which should return the following.
ok: [server1.example.com] => {
"ansible_user_id": "john.doe"
}
Or, the -vvv flag can be used to include some SSH output.
[john.doe@ansible1 ]# ansible-playbook foo.yml -i server1.example.com, -vvv
The following should be included in the output. In this example, "none" means that the user that invoked the playbook will be the SSH user.
<server1.example.com> ESTABLISH SSH CONNECTION FOR USER: None
The remote_user parameter can be used to define what user will be used when making an SSH connection to a managed node (e.g. the target system). In this example, user jane.doe will be used in the SSH connection. Now, when john.doe invokes the ansible-playbook command, jane.doe will be the user that makes the SSH connection.
---
- hosts: all
remote_user: jane.doe
tasks:
- name: store the stats of example.txt in the 'out' variable
stat:
path: /path/to/example.txt
register: out
Now, when the playbook is invoked with the -vvv flag, jane.doe is identified as the SSH user.
<server1.example.com> ESTABLISH SSH CONNECTION FOR USER: jane.doe
The --user command line flag will take precedence over the remote_user parameter.
[john.doe@ansible1 ]# ansible-playbook foo.yml -i server1.example.com, --user jack.doe -vvv
Did you find this article helpful?
If so, consider buying me a coffee over at