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

On Saturday, July 29, 2023 at 11:51:15 PM UTC-4 Prady A wrote:

> 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
>
> On Sun, Jul 30, 2023 at 1:23, Todd Lewis <[email protected]> wrote:
>
>> 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:
>>
>> $ 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:
>>
>> 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?
>> --
>> Todd
>>
>>
>> On 7/28/23 11:03 PM, Prady A wrote:
>>
>> 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  ([email protected])
>>
>> 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 
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "Ansible Project" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/5eeb8d90-f325-54b3-1032-73052fad4713%40gmail.com
>>  
>> <https://groups.google.com/d/msgid/ansible-project/5eeb8d90-f325-54b3-1032-73052fad4713%40gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/c2c3bab4-5a26-4f4c-99c7-2edf14022ea2n%40googlegroups.com.

Reply via email to