From 0023b1ee9eca1286493c56ce5e1aa1b2272c3e39 Mon Sep 17 00:00:00 2001 From: Artem Goncharov Date: Thu, 17 May 2018 18:09:01 +0200 Subject: [PATCH] fixes issue 39472: With python 3.6 spwd.getspnam returns PermissionError instead of KeyError if user does not have privileges --- changelogs/fragments/py36-spwd.yaml | 3 +++ lib/ansible/modules/system/user.py | 8 ++++++++ 2 files changed, 11 insertions(+) create mode 100644 changelogs/fragments/py36-spwd.yaml diff --git a/changelogs/fragments/py36-spwd.yaml b/changelogs/fragments/py36-spwd.yaml new file mode 100644 index 00000000000..7b867baa8e6 --- /dev/null +++ b/changelogs/fragments/py36-spwd.yaml @@ -0,0 +1,3 @@ +--- +bugfixes: +- spwd - With python 3.6 spwd.getspnam returns PermissionError instead of KeyError if user does not have privileges (https://github.com/ansible/ansible/issues/39472) diff --git a/lib/ansible/modules/system/user.py b/lib/ansible/modules/system/user.py index a1bfa3a8943..4968b40c372 100644 --- a/lib/ansible/modules/system/user.py +++ b/lib/ansible/modules/system/user.py @@ -236,6 +236,7 @@ EXAMPLES = ''' ''' +import errno import grp import os import platform @@ -646,6 +647,13 @@ class User(object): return passwd, expires except KeyError: return passwd, expires + except OSError as e: + # Python 3.6 raises PermissionError instead of KeyError + # Due to absence of PermissionError in python2.7 need to check + # errno + if e.errno in (errno.EACCES, errno.EPERM): + return passwd, expires + raise if not self.user_exists(): return passwd, expires