# \[Ansible Project\] how to get the logged in username

**URL:** <https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276>\
**Category:** Ansible Project\
**Created:** [July 29, 2023, 3:03am UTC](https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276 "2023-07-29T03:03:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [July 29, 2023, 3:03am UTC](https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276/1 "2023-07-29T03:03:39Z")

</div>

Hi All,

I searched a quite but could able to find what I wanted  
Could any pls suggest me how to get the Username in the host machine. In Linux if I run the below command I get what I suppose to get but don’t know how to do it ansible. Any code Ansible or python would be helpful

Fin Linux:  
\*\*X1234@hostname$\*\*su -  
\*\*root@hostname#\*\*who am i  
x1234 pts/2 2023-07-29 ([vxj-cyx@domain.com](mailto:vxj-cyx@domain.com))

My **getuser.yml** :

- debug: {{ lookup(‘env’, ‘USER’) }}  
delegate\_to: localhost

- local\_action: command whoami  
register: user\_name

**root@hostname#** ansible-playbook getuser.yml  
Both returning me **root**. I wanted to have **X1234** user instead. I want use that user ID in my from address in mail module. Any insight would be very helpfu.

Regards

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 29, 2023, 3:49am UTC](https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276/2 "2023-07-29T03:49:59Z")

</div>

Hi Prady,

You are running the playbook as root that is why the playbook is returning ‘root’. If you need a particular user then you may want to run the playbook using that user.  
For example -

```auto
# whoami
akasurde

# ansible-playbook user.yml -v
PLAY [localhost] *************************************************************************

TASK [debug] *****************************************************************************
ok: [localhost] => {
"msg": "akasurde"
}

TASK [command] ***************************************************************************
changed: [localhost] => {"changed": true, "cmd": ["who", "am", "I"], "delta": "0:00:00.009702", "end": "2023-07-28 20:48:00.637555", "msg": "", "rc": 0, "start": "2023-07-28 20:48:00.627853", "stderr": "", "stderr_lines": [], "stdout": "akasurde 28 Jul 20:48 ", "stdout_lines": ["akasurde 28 Jul 20:48 "]}

PLAY RECAP *******************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

```

You can use become and become\_user for escalating the privileges mentioned in [https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_privilege\_escalation.html#become-directives](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_privilege_escalation.html#become-directives)

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [July 29, 2023, 4:56am UTC](https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276/3 "2023-07-29T04:56:41Z")

</div>

Thank you for your kind reply. Yes that’s what my guess also.  
As there are many user who run the ansible so it unpractical to change it for all the user. I wanted to get the user details in runtime …

Regards  
Prady

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 29, 2023, 4:22pm UTC](https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276/4 "2023-07-29T16:22:56Z")

</div>

Without knowing your effective ansible config — i.e. the ansible.cfg it’s using, the environment variables that can override those settings, and command line parameters that can override everything else — it’s impossible to say. Factors include “become”, “become-user”, “become-method”, “ask-pass”, “ask-become-pass”, and probably more.

Are you gathering facts? Ansible does gather facts by default, so if you aren’t turning that off somewhere, you can use the variables containing user related facts. For example:

> ```auto
> $ ansible localhost -m gather_facts | grep ansible_user
> "ansible_user_dir": "/home/utoddl",
> "ansible_user_gecos": "Todd Lewis",
> "ansible_user_gid": 12428,
> "ansible_user_id": "utoddl",
> "ansible_user_shell": "/bin/bash",
> "ansible_user_uid": 12428,
> "ansible_userspace_architecture": "x86_64",
> "ansible_userspace_bits": "64",
> 
> ```

Not an Ansible thing, but: Do Not Use “who am i” for this. That’s the same a “who -m”, which shows you the user associated with the stdin stream, but only if that user is logged in AND only if the stdin stream exists and has an associated user. There’s a whole lot of subtle going on there that we don’t want to get into in an Ansible forum. The reason your “su -” followed by “who am i” is showing your id rather than root is (probably) because it’s your id associated with the tty you logged into. But that’s going to be different for Ansible, depending on how your controller connects to the target hosts, including localhost.

I gave you the python code yesterday. It’s dead simple:

> ```auto
> import os
> import pwd
> userid = pwd.getpwuid(os.getuid())[0]
> 
> ```

But if you’re gathering facts, just use the ansible\_user\_id variable.

If that’s “root”, and that appears to be the case, then you’ll need to understand how your controller is connecting to the target hosts. My guess is you’ll eventually need to pass the invoking user’s id as an extra variable (“-e invoking\_user=${USER}”) when invoking ansible-playbook. Maybe consider a wrapper script?

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [July 30, 2023, 3:50am UTC](https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276/5 "2023-07-30T03:50:52Z")

</div>

Hi Todd,

Yeah if we pass who with any arguments it remains the same. I read it some other forum.

I tried with the python code as well as ansible\_user\_id both returns the same “root”. Since I m running the playbook from root user after su -.  
As you suggested we need to check how we can capture the userid who run the playbook from Ansible tower.

Regards  
Prady

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 31, 2023, 2:11pm UTC](https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276/6 "2023-07-31T14:11:10Z")

</div>

If users are launching this job through Ansible tower (AWX), and not from a schedule, then you can probably get their user name through the variable “awx\_user\_name”.  
There are additional variables specific to AWX. See “{{ lookup(‘ansible.builtin.varnames’, ‘^awx\_.+’) }}” to get their names. I’m seeing “awx\_inventory\_id”, “awx\_inventory\_name”, “awx\_job\_id”, “awx\_job\_launch\_type”, “awx\_job\_template\_id”, “awx\_job\_template\_name”, “awx\_project\_revision”, “awx\_project\_scm\_branch”, “awx\_user\_email”, “awx\_user\_first\_name”, “awx\_user\_id”, “awx\_user\_last\_name”, and “awx\_user\_name”.

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [July 31, 2023, 2:27pm UTC](https://forum.ansible.com/t/ansible-project-how-to-get-the-logged-in-username/38276/7 "2023-07-31T14:27:25Z")

</div>

Thank you again… you are so kind …It helped a lot…yes we orchestrate servers through Tower…Still a novice in Tower …

I went through this link there they suggested to use  
tower\_user\_id

[https://groups.google.com/g/ansible-project/c/YQ\_r9UaS-uw](https://groups.google.com/g/ansible-project/c/YQ_r9UaS-uw)

Regards
