diff options
author | James Cammarata <jimi@sngx.net> | 2014-06-03 09:36:19 -0500 |
---|---|---|
committer | James Cammarata <jimi@sngx.net> | 2014-06-09 14:11:08 -0500 |
commit | 936e1de18bca8c9353b8d27383bf33252ec2f6a7 (patch) | |
tree | af0e0ca595fe6c830860f75da3e6e6d009d21d04 | |
parent | c4b6c0ab84abd51bf1450f60ddb5c7cfcfe3b034 (diff) | |
download | ansible-936e1de18bca8c9353b8d27383bf33252ec2f6a7.tar.gz |
Fix error when using os.getlogin() without a tty
-rw-r--r-- | lib/ansible/module_utils/basic.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index bd43ede82c..423bdfb514 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -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. |