Reliably getting user homedir, despite become

8 views
Skip to first unread message

needabettername

unread,
Apr 11, 2021, 3:15:11 AM4/11/21
to Ansible Project
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

Brian Coca

unread,
Apr 12, 2021, 12:45:43 PM4/12/21
to Ansible Project
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 ":"



--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages