From 636e705260780ffc721f7fa9f6973275cce9244c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Job=20Evers=E2=80=90Meltzer?= Date: Fri, 13 Apr 2018 16:22:53 -0400 Subject: [PATCH] fix user module error when generating ssh keys w/o a home (#29055) * Fixes https://github.com/ansible/ansible/issues/29028 * raise Exception when no home directory --- lib/ansible/modules/system/user.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/system/user.py b/lib/ansible/modules/system/user.py index 1326b7f4b4..1e79b3f6f0 100644 --- a/lib/ansible/modules/system/user.py +++ b/lib/ansible/modules/system/user.py @@ -619,14 +619,18 @@ class User(object): if os.path.isabs(self.ssh_file): ssh_key_file = self.ssh_file else: + if not os.path.exists(info[5]) and not self.module.check_mode: + raise Exception('User %s home directory does not exist' % self.name) ssh_key_file = os.path.join(info[5], self.ssh_file) return ssh_key_file def ssh_key_gen(self): info = self.user_info() - if not os.path.exists(info[5]) and not self.module.check_mode: - return (1, '', 'User %s home directory does not exist' % self.name) - ssh_key_file = self.get_ssh_key_path() + try: + ssh_key_file = self.get_ssh_key_path() + except Exception: + e = get_exception() + return (1, '', str(e)) ssh_dir = os.path.dirname(ssh_key_file) if not os.path.exists(ssh_dir): if self.module.check_mode: