Repository: ansible/ansible-modules-core

Issue:
User module on SunOS always returns changed in check mode
**Issue Type:** Bug Report
**Ansible Version:** ansible 1.9.4 (also present in devel)
**Ansible Configuration:** No customisations
**Environment:** Running from RHEL7 against Solaris 11.
**Summary:**
Executing the user module to create a user that already exists and has the same attributes as those requested using the '-C' check mode parameter always results in 'changed=true'.

**Steps To Reproduce:**
  Create account with: 

```
ansible solhost -m user -a 'name=testuser state=present'
```

  Check account with:

```
ansible solhost -m user -a 'name=testuser state=present' -C
```

**Expected Result:**

```
solhost | success >> {
    "append": false,
    "changed": false,
    "comment": "",
    "group": 10,
    "home": "/export/home/testuser",
    "move_home": false,
    "name": "testuser",
    "shell": "/usr/bin/bash",
    "state": "present",
     "uid": 24147
}
```

i.e. "changed": false, in the above listing

**Actual Results:**

```
solhost | success >> {
    "append": false,
    "changed": true,
    "comment": "",
    "group": 10,
    "home": "/export/home/testuser",
    "move_home": false,
    "name": "testuser",
    "shell": "/usr/bin/bash",
    "state": "present",
    "uid": 24147
}
```

i.e. Incorrectly reports: "changed": true, in the above listing



Discussion:

Resolving patch:
diff --git a/system/user.py b/system/user.py
--- a/system/user.py
+++ b/system/user.py
@@ -1352,20 +1352,21 @@ def modify_user_usermod(self):
             cmd.append('-s')
             cmd.append(self.shell)
 
-        if self.module.check_mode:
-            return (0, '', '')
-        else:
-            # modify the user if cmd will do anything
-            if cmd_len != len(cmd):
+        # modify the user if cmd will do anything
+        if cmd_len != len(cmd):
+            (rc, out, err) = (0, '', '')
+            if not self.module.check_mode:
                 cmd.append(self.name)
                 (rc, out, err) = self.execute_command(cmd)
                 if rc is not None and rc != 0:
                     self.module.fail_json(name=self.name, msg=err, rc=rc)
-            else:
-                (rc, out, err) = (None, '', '')
+        else:
+            (rc, out, err) = (None, '', '')
 
-            # we have to set the password by editing the /etc/shadow file 
-            if self.update_password == 'always' and self.password is not None and info[1] != self.password:
+        # we have to set the password by editing the /etc/shadow file 
+        if self.update_password == 'always' and self.password is not None and info[1] != self.password:
+            (rc, out, err) = (0, '', '')
+            if not self.module.check_mode:
                 try:
                     lines = []
                     for line in open(self.SHADOWFILE, 'rb').readlines():
@@ -1382,7 +1383,7 @@ def modify_user_usermod(self):
                 except Exception, err:
                     self.module.fail_json(msg="failed to update users password: %s" % str(err))
 
-            return (rc, out, err)
+        return (rc, out, err)
 
 # ===========================================
 class DarwinUser(User):


Test patch: