# Reliably getting user homedir, despite become

**URL:** <https://forum.ansible.com/t/reliably-getting-user-homedir-despite-become/34957>\
**Category:** Ansible Project\
**Created:** [April 11, 2021, 7:15am UTC](https://forum.ansible.com/t/reliably-getting-user-homedir-despite-become/34957 "2021-04-11T07:15:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![need\_abettername](https://avatars.discourse-cdn.com/v4/letter/n/5fc32e/32.png) [@need\_abettername](https://forum.ansible.com/u/need_abettername)\
**Post date:** [April 11, 2021, 7:15am UTC](https://forum.ansible.com/t/reliably-getting-user-homedir-despite-become/34957/1 "2021-04-11T07:15:11Z")

</div>

I’d like these results when querying a remote machine, but am having problems finding the correct user vars to pass

‘sometypeofuser’: Alice,  
become: true  
Result: /home/alice

‘sometypeofuser’: Alice,  
become: false  
Result: /home/alice

‘sometypeofuser’: root  
become: true or false  
Result: /root/

Got myself in a muddle here so an example would be much appreciated please,  
It’s also possible that I’ve made a mistake in my inventory that I can’t find at the moment.

I’ve been trying

command: echo ~  
This isn’t ideal as is affected by become and also registers a ‘changed’ in the results

ansible\_env.HOME but am always getting the account running the play

Also, I may be mistaken here but seems that if you run the play on the control machine is doesn’t ssh out and then in and that changes the ansible.env results (?)

Thank you in advance

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 12, 2021, 4:44pm UTC](https://forum.ansible.com/t/reliably-getting-user-homedir-despite-become/34957/2 "2021-04-12T16:44:45Z")

</div>

ansilbe\_env reflects 'the user used to gather facts on that target',  
if the target is localhost .. that is what you get.

As for getting the home of the of user that logged in, the following  
should work for most common cases:

shell: getent passwd ${SUDO\_USER:-$LOGNAME}|cut -f 6 -d ":"
