Repository: ansible/ansible

Issue:
user module with option local fails on Ubuntu
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
user module

##### ANSIBLE VERSION
```
ansible 2.4.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
Nothing changed.

##### OS / ENVIRONMENT
Ubuntu 16.04LTS
local ansible run (local host only)
libuser 0.60 installed
Using the AD for authentication, users are defined in /etc/passwd => nsswitch.conf does NOT have sss for passwd, group, and shadow.

##### SUMMARY
I tried to create a local user using the user module with the local option (available as of 2.4).
This fails on Ubuntu 16.04LTS with the error:

`Error parsing arguments: unknown option.
 Usage: luseradd [-irMn?] [-i|--interactive] [-r|--reserved] 
        [-c|--gecos=STRING] [-d|--directory=STRING] [-k|--skeleton=STRING]
         [-s|--shell=STRING] [-u|--uid=NUM] [-g|--gid=STRING] 
        [-M|--nocreatehome] [-n|--nocreategroup] [-P|--plainpassword=STRING] 
        [-p|--password=STRING] [--commonname=STRING] [--givenname=STRING] 
        [--surname=STRING] [--roomnumber=STRING] [--telephonenumber=STRING]
         [--homephone=STRING] [-?|--help] [--usage] [OPTION...] user ` 

This works if local is false.

##### STEPS TO REPRODUCE
Install libuser if not installed
Add a new user to the local /etc/passwd file using the user module and the local option set to true.

<!--- Paste example playbooks or commands between quotes below -->
```yaml
  user:
    comment: "{{ item.gecos }},{{ dmi_users.org_unit }}"
    createhome: true
    group: "{{ item.gid }}"
    groups: "{{ item.groups|join(\",\") }}"
    append: false
    home: "{{ item.home }}"
    name: "{{ item.uid }}"
    password: "KB"
    shell: "{{ item.shell }}"
    state: "present"
    uid: "{{ item.uidnumber }}"
    local: true
  with_items: "{{ users }}"
```
users contains one full user record. This works fine with local: false

##### EXPECTED RESULTS
To add the user just as with local:false.

##### ACTUAL RESULTS
The module fails with the above error message. 

<!--- Paste verbatim command output between quotes below -->
```

```


Discussion:
cc @sfromm
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@rmfrankch FYI, the reason it's failing is because `luseradd` doesn't support `-m` (which is its default behavior). As a temporary workaround, you can add `createhome: false` to get it working (it'll still create the homedir), but this won't work in the future if we fix this properly (since both `useradd` and `luseradd` seem to support `-M` (don't create home dir)), which we'd probably just switch to... 
cc @bcoca - looks like you added the `local` support. It looks like there are several easy ways to fix this, though I'm not sure what'd be best supported across the various distros...
@**nitzmahone**: Unfortunately the workaround won't work, as I have both cases: with and without creating homes using the user module. Your suggestion would mean that I cannot use the local option if I don't want to create the homes.
So, this option is a blocker until fixed.
so we need to make it -M when luser? many 'useradd' implementations only support -m, not -M

does lusermod have the same issues?
this patch should fix
```
diff --git a/lib/ansible/modules/system/user.py b/lib/ansible/modules/system/user.py
index 6208d2394e..d0189ac548 100644
--- a/lib/ansible/modules/system/user.py
+++ b/lib/ansible/modules/system/user.py
@@ -424,7 +424,10 @@ class User(object):
             cmd.append(self.password)

         if self.create_home:
-            cmd.append('-m')
+            if self.local:
+                cmd.append('-M')
+            else:
+                cmd.append('-m')

             if self.skeleton is not None:
                 cmd.append('-k')
```

Resolving patch:
diff --git a/lib/ansible/modules/system/user.py b/lib/ansible/modules/system/user.py
--- a/lib/ansible/modules/system/user.py
+++ b/lib/ansible/modules/system/user.py
@@ -424,7 +424,10 @@ def create_user_useradd(self):
             cmd.append(self.password)
 
         if self.create_home:
-            cmd.append('-m')
+            if self.local:
+                cmd.append('-M')
+            else:
+                cmd.append('-m')
 
             if self.skeleton is not None:
                 cmd.append('-k')


Test patch: