1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fix error when using os.getlogin() without a tty

This commit is contained in:
James Cammarata 2014-06-03 09:36:19 -05:00
parent 6bc056e012
commit 849b0f87ce

View file

@ -1020,7 +1020,18 @@ class AnsibleModule(object):
context = self.selinux_default_context(dest)
creating = not os.path.exists(dest)
switched_user = os.getlogin() != pwd.getpwuid(os.getuid())[0]
try:
login_name = os.getlogin()
except OSError:
# not having a tty can cause the above to fail, so
# just get the LOGNAME environment variable instead
login_name = os.environ.get('LOGNAME', None)
# if the original login_name doesn't match the currently
# logged-in user, or if the SUDO_USER environment variable
# is set, then this user has switched their credentials
switched_user = login_name and login_name != pwd.getpwuid(os.getuid())[0] or os.environ.get('SUDO_USER')
try:
# Optimistically try a rename, solves some corner cases and can avoid useless work, throws exception if not atomic.