How can I add a user to multiple groups in Ansible?

The correct syntax is:

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

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.

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

Correct way is

groups={{ group }},{{ sudo_group }}

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

groups: group1,group2 

Then to use:

action: user groups={{user.groups}}