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 -

```
# 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

On Fri, Jul 28, 2023 at 8:04 PM Prady A <pradyumna....@gmail.com> 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  (vxj-...@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
>
> --
> 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 ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAEuB3Ao1t7Wi%3DjcoBVezKwUiz_-EyaZ6%2BF_Gjb8EYRK8PusGjg%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAEuB3Ao1t7Wi%3DjcoBVezKwUiz_-EyaZ6%2BF_Gjb8EYRK8PusGjg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Thanks,
Abhijeet Kasurde

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAFwWkHrBZU%2B3yVhRA93vALu%2Bc7AEQbtieeWeHSfeNB0Ni_iU1A%40mail.gmail.com.

Reply via email to