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: