ansible/ansible — pull request #2015 by trendels (2013-02-08 00:36:02+00:00)
Title: Handle 'groups=' (empty string) in user module (Linux)

Hi,
I noticed that the user module would not let me remove a user from all (non-primary) groups. Example (assuming groups a, b already exist):

1. Add a user to two groups
```
$ ansible -i hosts -s vm1 -m user -a 'name=testuser groups=a,b'
vm1 | success >> {
"append": false,
"changed": true,
"comment": "",
"group": 507,
"groups": "a,b",
"home": "/home/testuser",
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"uid": 505
}
$ ssh vm1 groups testuser
testuser : testuser a b
```

2. Remove from 1st group works
```
$ ansible -i hosts -s vm1 -m user -a 'name=testuser groups=a'
vm1 | success >> {
"append": false,
"changed": true,
"comment": "",
"group": 507,
"groups": "a",
"home": "/home/testuser",
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"uid": 505
}
$ ssh vm1 groups testuser
testuser : testuser a
```

3. Remove from 2nd group **fails**
```
$ ansible -i hosts -s vm1 -m user -a 'name=testuser groups='
vm1 | FAILED >> {
"failed": true,
"msg": "Group does not exist"
}
$ ssh vm1 groups testuser
testuser : testuser a
```

This patch makes step 3 work by allowing you to pass `groups=`. This relies on the fact that `usermod`/`useradd` accepts `-G ""` to mean "no groups" for exactly this purpose on Linux. I don't have access to FreeBSD or SunOS to test this, so I did *not* patch the `FreeBsdUser` and `SunOS` user subclasses.

Last step with patch:

```
$ ansible -i hosts -s vm1 -m user -a 'name=testuser groups='
vm1 | success >> {
"append": false,
"changed": true,
"comment": "",
"group": 507,
"groups": "",
"home": "/home/testuser",
"name": "testuser",
"shell": "/bin/bash",
"state": "present",
"uid": 505
}
$ ssh vm1 groups testuser
testuser : testuser
```