7

I can't add a user to multiple groups, I keep getting msg: this module requires key=value arguments.

This is the piece of code I'm trying:

- name: make a new user
  user: name=user
        state=present
        groups="group1", "group2", "group3"
        comment="comment"

The documentation says: Groups= Puts the user in this comma-delimited list of groups. When set to the empty string ('groups='), the user is removed from all groups except the primary group.

I have tried with "group", 'group', and without colons and still get the same error.

http://docs.ansible.com/user_module.html

6

The correct syntax is:

- name: make a new user
  user: name=user
        state=present
        groups="group1, group2, group3"
        comment="comment"
4

There are two issues with the code you posted:

  1. To pass multiple values to groups, use comma-separated values with no spaces in between: groups: group1,group2
  2. In YAML, when you put each key on its own line, swap the = for :

Here's an example of working code:

- name: make a new user
  user: 
    name: johnsmith
    state: present
    groups: group1,group2
    comment: "comment"
    append: no # If yes, will only add groups, not set them to just the list in groups.
  • The qualifier "with no spaces between" is important! I just got burned by that myself (see this open Ansible issue). – evadeflow Jan 27 '17 at 14:57
2

I get Group " group2" does not exist. (But without the quotes, thats to show the extra space).

Correct way is

groups={{ group }},{{ sudo_group }}
  • As for the response that "group2 doesn't exist", a group has to exist before you add a user to the group. Adding a user, and specifying the user is a member of a group that doesn't exist, does not create the group. When adding users and groups, create the groups first, then add your users. (The one exception is the group that is created at the same time as the user, on systems that use group-per-user (most Linux's)). – Andrew Edelstein Aug 11 '15 at 21:36
  • @AndrewEdelstein: you're 100% correct, but there's a more subtle/nefarious issue at work in this case. The error message says: " group2" does not exist". I added quotes around the string " group2" to highlight the fact that it contains a leading space. Ansible gives that error message even if a group named "group2" (no leading space) exists[!] I consider this a bug, though perhaps there are technical reasons that make it difficult for Ansible to strip whitespace from these tokens. – evadeflow Jan 27 '17 at 15:08
2

The above answers are incorrect. The right way to define a variable:

groups: group1,group2 

Then to use:

action: user groups={{user.groups}}
  • 2
    there is no set order for displaying answers.. "above" can mean one thing to you and a different thing to someone else. Or even a different thing to you, tomorrow. – dan3 Mar 22 '15 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.