From f97d00fbaddb80b12424bb57d66b3f104dd7df31 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Wed, 26 Aug 2015 08:51:52 +0300 Subject: [PATCH] user: don't generate SSH keys in check mode Fixes https://github.com/ansible/ansible/issues/11768 Test plan: - (in a Vagrant VM) created a user 'bob' with no ssh key - ran the following playbook in check mode: --- - hosts: trusty tasks: - user: name=bob state=present generate_ssh_key=yes - saw that ansible-playbook reported "changes=1" - saw that /home/bob/.ssh was still absent - ran the playbook for real - saw that /home/bob/.ssh was created - ran the playbook in check mode again - saw that ansible-playbook reported no changes - tried a variation with a different username for a user that didn't exist: ansible-playbook --check worked correctly (no errors, reported "changed") --- lib/ansible/modules/system/user.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/system/user.py b/lib/ansible/modules/system/user.py index 7e3e4c01cd3..45ce77381ce 100755 --- a/lib/ansible/modules/system/user.py +++ b/lib/ansible/modules/system/user.py @@ -577,11 +577,13 @@ class User(object): def ssh_key_gen(self): info = self.user_info() - if not os.path.exists(info[5]): + 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() ssh_dir = os.path.dirname(ssh_key_file) if not os.path.exists(ssh_dir): + if self.module.check_mode: + return (0, '', '') try: os.mkdir(ssh_dir, 0700) os.chown(ssh_dir, info[2], info[3]) @@ -589,6 +591,8 @@ class User(object): return (1, '', 'Failed to create %s: %s' % (ssh_dir, str(e))) if os.path.exists(ssh_key_file): return (None, 'Key already exists', '') + if self.module.check_mode: + return (0, '', '') cmd = [self.module.get_bin_path('ssh-keygen', True)] cmd.append('-t') cmd.append(self.ssh_type) @@ -2148,6 +2152,7 @@ def main(): # deal with ssh key if user.sshkeygen: + # generate ssh key (note: this function is check mode aware) (rc, out, err) = user.ssh_key_gen() if rc is not None and rc != 0: module.fail_json(name=user.name, msg=err, rc=rc)