# How do I get the REMOTE\_USER variable while in a sudo playbook?

**URL:** <https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587>\
**Category:** Ansible Project\
**Created:** [March 26, 2013, 6:15pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587 "2013-03-26T18:15:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steve\_Weber](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@Steve\_Weber](https://forum.ansible.com/u/Steve_Weber)\
**Post date:** [March 26, 2013, 6:15pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/1 "2013-03-26T18:15:48Z")

</div>

> [14:04] how do i access the ansible\_ssh\_user , or user variable?  
> [14:05] I cant use ansible\_user\_id because my playbook changes to root  
> [14:06] I was able to do hacks passing the ssh username like extra-vars="default\_ssh\_user=$USER’  
> [14:06] or even under a top level playbook that registers a user var for echo “$USER”  
> [14:07] but I dont want to do this task indirectly… any help?

```auto
  -u REMOTE_USER, --user=REMOTE_USER
                        connect as this user (default=s8weber)

```

I cant use $ansible\_user\_id in playbooks that sudo because the user is /root/ not /s8weber/.

How do I get the REMOTE\_USER variable?

thanks.

---

<div class="post-metadata">

**Author:** ![Mathieu\_Guillaume](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Mathieu\_Guillaume](https://forum.ansible.com/u/Mathieu_Guillaume)\
**Post date:** [March 26, 2013, 6:27pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/2 "2013-03-26T18:27:49Z")

</div>

At which point do you need this?

If this is when the runner is executing the tasks on the different hosts, I think it depends on the shell, but with bash I have a $SUDO\_USER variable.  
You should be able to access it in Ansible with $LOOKUP(env, SUDO\_USER) (or something close to that, never used lookups myself).

---

<div class="post-metadata">

**Author:** ![Mathieu\_Guillaume](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Mathieu\_Guillaume](https://forum.ansible.com/u/Mathieu_Guillaume)\
**Post date:** [March 26, 2013, 6:31pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/3 "2013-03-26T18:31:36Z")

</div>

The sudo\_user could also be made accessible in the host vars with (I think, not tested):

diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py  
index e59912c..f1f94ea 100644  
— a/lib/ansible/playbook/play.py  
+++ b/lib/ansible/playbook/play.py  
@@ -94,6 +94,7 @@ class Play(object):  
self.tags =

if self.sudo\_user != ‘root’:

- self.vars[‘sudo\_user’] = self.sudo\_user  
self.sudo = True

# \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

---

<div class="post-metadata">

**Author:** ![Steve\_Weber](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@Steve\_Weber](https://forum.ansible.com/u/Steve_Weber)\
**Post date:** [March 26, 2013, 6:51pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/4 "2013-03-26T18:51:11Z")

</div>

thanks Mathieu but im not in a python file… im in a playbook…  
Also im not trying to get the sudo user im trying to get the user that is connecting to the host…

here is some more background on the issue.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 26, 2013, 10:46pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/5 "2013-03-26T22:46:18Z")

</div>

He's suggesting a patch to the application to make this variable available.

Curious why the fact username isn't good enough though.

---

<div class="post-metadata">

**Author:** ![Steve\_Weber](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@Steve\_Weber](https://forum.ansible.com/u/Steve_Weber)\
**Post date:** [March 27, 2013, 2:43am UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/6 "2013-03-27T02:43:20Z")

</div>

Ah yes a patch.. But i`m not after the sudo\_user.  
im looking for the user name that is connecting through ‘ssh’ before sudo

> ansible-playbook -i localhost install.yml --user=$USER --extra-vars=“ssh\_user=$USER” --ask-sudo-pass

Currently i’m creating the the variable ssh\_user as a work around… so I can then pass it to a template.

# install.yml would be something like…

> - name render php-fpm file  
> sudo: yes  
> task:
> - template: src=php-fpm.j2 dest=/etc/php/php-fpm/pool.d/pool-${ssh\_user}.conf

# php-fpm.j2 would be something like

> [pool-{{ ssh\_user }}]  
> user = {{ ssh\_user }}  
> group = {{ ssh\_user }}

is there a way to do the same thing but without creating an extra variable for ssh\_user?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [March 27, 2013, 12:48pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/7 "2013-03-27T12:48:14Z")

</div>

I see.

So we want to just basically expose $ansible\_ssh\_user as a variable  
before we call tasks about it.

This is not difficult at all -- Please file a ticket so we don't forget this.

We're past the feature freeze date for 1.1 (which was Monday), so this  
will happen on 1.2's development branch after Tuesday...

A simple workaround now would just be to set the variable in the play,  
prompt for it via vars\_prompt, or pass it in with --extra-vars.

---

<div class="post-metadata">

**Author:** ![Steve\_Weber](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@Steve\_Weber](https://forum.ansible.com/u/Steve_Weber)\
**Post date:** [March 27, 2013, 2:21pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/8 "2013-03-27T14:21:46Z")

</div>

sure ill file the ticket on git hub.

---

<div class="post-metadata">

**Author:** ![Steve\_Weber](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@Steve\_Weber](https://forum.ansible.com/u/Steve_Weber)\
**Post date:** [March 27, 2013, 2:30pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/9 "2013-03-27T14:30:00Z")

</div>

issue:  
[https://github.com/ansible/ansible/issues/2516](https://github.com/ansible/ansible/issues/2516)

---

<div class="post-metadata">

**Author:** ![Steve\_Weber](https://avatars.discourse-cdn.com/v4/letter/s/a698b9/32.png) [@Steve\_Weber](https://forum.ansible.com/u/Steve_Weber)\
**Post date:** [April 15, 2013, 5:37pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/10 "2013-04-15T17:37:27Z")

</div>

I found this workaround

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Ziegler](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@Wolfgang\_Ziegler](https://forum.ansible.com/u/Wolfgang_Ziegler)\
**Post date:** [February 11, 2014, 1:18pm UTC](https://forum.ansible.com/t/how-do-i-get-the-remote-user-variable-while-in-a-sudo-playbook/12587/11 "2014-02-11T13:18:58Z")

</div>

Just using {{ ansible\_ssh\_user }} works for me - probably that got improved meanwhile.
