Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

The correct syntax is:

- name: make a new user
  user: name=user
        state=present
        groups="group1, group2, group3"
        comment="comment"
share|improve this answer

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

Correct way is

groups={{ group }},{{ sudo_group }}
share|improve this answer
    
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 at 21:36

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

groups: group1,group2 

Then to use:

action: user groups={{user.groups}}
share|improve this answer
1  
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 at 8:19

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.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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