25

What is the cleanest way of doing this? Tried adding the user to the sudo group with the users module but Ansible reports it can't find the sudo group.

Hyperfocus
  • 1,177
  • 4
  • 14
  • 23

4 Answers4

14

Instead of

- name: create a new user
    user: name=user
          state=present
          groups="group1, group2"
          password={{ password }}
          comment="Comment"

I did

- name: create a new user
    user: name=user
          state=present
          group=primary-group
          groups="sudo"
          password={{ password }}
          comment="Comment"

And the user was added to the sudo group.

Hyperfocus
  • 1,177
  • 4
  • 14
  • 23
9
- name: Create Deploy user
  user: name={{ deploy }} comment="Deploy User" groups="sudo,admin,{{ deploy }}"
  sudo: yes

Be careful of spaces in your groups list... You get the "group does not exist" if you have them.

RG308
  • 91
  • 1
  • 1
1

To do it in a traditional linux way using ansible :)

Make a file, create_user.yml and add the following code.
Change <username> with the username you want to add to the sudo group

- name: "run command"
  become: true
  gather_facts: no
  hosts: all
  tasks:
    - name: add user to sudo group
      shell: usermod -aG sudo <username>
      args:
        executable: /bin/bash

Now run this user_create.yml playbook with the command

ansible-playbook -K <filename>.yml
Dr. Mian
  • 151
  • 3
  • 11
1

groups="admin" will create user add in admin groups

linbo
  • 111
  • 3