If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The user module is similar to the useradd and usermod commands. The user module is used to create or modify a users account on a managed node (e.g. target system).
If the users account does not exist, the users account will be created. If the users account already exists, the users account will be modified.
Before using this module, you should probably use the group module to create the group if the group does not yet exist on the managed node.
There are two group parameters:
- group - update the users primary group
- groups - add the user to be a member of additional groups
In this example, John Doe's primary group will be set to wheel.
---
- hosts: all
tasks:
- name: john.doe primary group is 'wheel'
ansible.builtin.user:
name: john.doe
group: wheel
...
Let's say John Doe's primary group is 'john.doe' and you want to add John Doe to group2 and group3. In this scenario, you would use groups (with an "s") and append.
---
- hosts: all
tasks:
- name: add john.doe to 'group2' and 'group3'
ansible.builtin.user:
name: john.doe
groups: group2,group3
...
Did you find this article helpful?
If so, consider buying me a coffee over at