I'm using the User
in ~/.ssh/config
file to specify the user name ansible uses to access the remote server, for example:
Host 123.234.098.076
User my_local_user_name
Is there a way to find that user name in Ansible? In the following playbook ansible_user
is defined:
---
- hosts: "all"
tasks:
- name: "perform whoami"
shell: whoami
register: whoami
- set_fact:
ansible_user: "{{ whoami.stdout }}"
- debug:
msg: "I am user: {{ ansible_user }}" # will display: "I am user: my_local_user_name"
However I'm not sure of any unintended consequences of setting the ansible_user
directly as opposed to using the remote_user
setting in the playbook, in the inventory or in the ansible config such as:
---
- hosts: "all"
remote_user: my_local_user_name
tasks:
#- name: "perform whoami"
# shell: whoami
# register: whoami
#- set_fact:
# ansible_user: "{{ whoami.stdout }}"
- debug:
msg: "I am user: {{ ansible_user }}" # will display: "I am user: my_local_user_name"