How do I get the REMOTE_USER variable while in a sudo playbook?

[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?

  -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.

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).

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

*************************************************

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.

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

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

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?

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.

sure ill file the ticket on git hub.

issue:
https://github.com/ansible/ansible/issues/2516

I found this workaround

Just using {{ ansible_ssh_user }} works for me - probably that got improved meanwhile.