Manage Linux users and groups, configure SSH, and modify the Sudo configuration on managed hosts.
The Ansible ansible.builtin.user module lets you create, configure, and remove user accounts on managed hosts.
You can remove or add a user, set a user's home directory, set the UID for system user accounts, manage passwords, and assign a user to supplementary groups.
To create a user that can log in to the machine, you need to provide a hashed password for the password parameter.
See "How do I generate encrypted passwords for the user module?" for information on how to hash a password.
The following example demonstrates the ansible.builtin.user module:
- name: Create devops_user if missing, make sure is member of correct groups
ansible.builtin.user:
name: devops_user
shell: /bin/bash
groups: sys_admins, developers
append: trueThe | |
The | |
The |
The ansible.builtin.user module also provides information in return values, such as the user's home directory and a list of groups that the user is a member of.
These return values can be registered into a variable and used in subsequent tasks.
More information is available in the documentation for the module.
Table 9.1. Commonly Used Parameters for the User Module
| Parameter | Comments |
|---|---|
comment
| Optionally sets the description of a user account. |
group
| Optionally sets the user's primary group. |
groups
| Optionally sets a list of supplementary groups for the user. When set to a null value, all groups except the primary group are removed. |
home
| Optionally sets the user's home directory location. |
create_home
| Optionally takes a Boolean value of true or false.
A home directory is created for the user if the value is set to true. |
system
| Optionally takes a Boolean value of true or false.
When creating an account, this makes the user a system account if the value is set to true.
This setting cannot be changed on existing users. |
uid
| Sets the UID number of the user. |
state
| If set to present, create the account if it is missing (the default setting).
If set to absent, remove the account if it is present. |
The ansible.builtin.user module can generate an SSH key if called with the generate_ssh_key parameter.
The following example demonstrates how the ansible.builtin.user module generates an SSH key:
- name: Create an SSH key for user1
ansible.builtin.user:
name: user1
generate_ssh_key: true
ssh_key_bits: 2048
ssh_key_file: .ssh/id_my_rsa 
The | |
The | |
The |
The ansible.builtin.group module adds, deletes, and modifies groups on the managed hosts.
The managed hosts need to have the groupadd, groupdel, and groupmod commands available, which are provided by the shadow-utils package in Red Hat Enterprise Linux 9.
For Microsoft Windows managed hosts, use the win_group module.
The following example demonstrates how the ansible.builtin.group module creates a group:
- name: Verify that the auditors group exists
ansible.builtin.group:
name: auditors
state: present 
The | |
The |
Table 9.2. Parameters for the Group Module
| Parameter | Comments |
|---|---|
gid
| This parameter sets the GID number to for the group. If omitted, the number is automatically selected. |
local
| This parameter forces the use of local command alternatives (instead of commands that might change central authentication sources) on platforms that implement it. |
name
| This parameter sets the name of the group to manage. |
state
| This parameter determines whether the group should be present or absent on the remote host. |
system
| If this parameter is set to true, then the group is created as a system group (typically, with a GID number below 1000). |
The ansible.builtin.known_hosts module manages SSH host keys by adding or removing them on managed hosts.
This ensures that managed hosts can automatically establish the authenticity of SSH connections to other managed hosts, ensuring that users are not prompted to verify a remote managed host's SSH fingerprint the first time they connect to it.
The following example demonstrates how the ansible.builtin.known_hosts module copies a host key to a managed host:
- name: Copy host keys to remote servers
ansible.builtin.known_hosts:
path: /etc/ssh/ssh_known_hosts
name: servera.lab.example.com
key: servera.lab.example.com,172.25.250.10 ssh-rsa ASDeararAIUHI324324 
The | |
The | |
The |
The following example demonstrates how to use the lookup plug-in to populate the key parameter from an existing file in the Ansible project:
- name: Copy host keys to remote servers
ansible.builtin.known_hosts:
path: /etc/ssh/ssh_known_hosts
name: serverb
key: "{{ lookup('ansible.builtin.file', 'pubkeys/serverb') }}" 
This Jinja2 expression uses the |
The following play is an example that uses some advanced techniques to construct an /etc/ssh/ssh_known_hosts file for all managed hosts in the inventory.
There might be more efficient ways to accomplish this, because it runs a nested loop on all managed hosts.
It uses the ansible.builtin.slurp module to get the content of the RSA and Ed25519 SSH public host keys in Base64 format, and then processes the values of the registered variable with the b64decode and trim filters to convert those values back to plain text.
- name: Configure /etc/ssh/ssh_known_hosts files
hosts: all
tasks:
- name: Collect RSA keys
ansible.builtin.slurp:
src: /etc/ssh/ssh_host_rsa_key.pub
register: rsa_host_keys
- name: Collect Ed25519 keys
ansible.builtin.slurp:
src: /etc/ssh/ssh_host_ed25519_key.pub
register: ed25519_host_keys
- name: Deploy known_hosts
ansible.builtin.known_hosts:
path: /etc/ssh/ssh_known_hosts
name: "{{ item[0] }}"
key: "{{ hostvars[ item[0] ]['ansible_facts']['fqdn'] }} {{ hostvars[ item[0] ][ item[1] ]['content'] | b64decode | trim }}"
state: present
with_nested:
- "{{ ansible_play_hosts }}"
- [ 'rsa_host_keys', 'ed25519_host_keys' ] 
| |
| |
| |
This is a two-item list of the two variables that the play uses to store host keys. |
Lookup plug-ins and filters are covered in more detail in the course DO374: Developing Advanced Automation with Red Hat Ansible Automation Platform.
The ansible.posix.authorized_key module manages SSH authorized keys for user accounts on managed hosts.
The following example demonstrates how to use the ansible.posix.authorized_key module to add an SSH key to a managed host:
- name: Set authorized key
ansible.posix.authorized_key:
user: user1
state: present
key: "{{ lookup('ansible.builtin.file', 'files/user1/id_rsa.pub') }}" 
The | |
The | |
The |
In Red Hat Enterprise Linux 9, you can configure access for a user or group to run sudo commands without requiring a password prompt.
The following example demonstrates how to use the ansible.builtin.lineinfile module to provide a group with sudo access to the root account without prompting the group members for a password:
- name: Modify sudo to allow the group01 group sudo without a password
ansible.builtin.lineinfile:
path: /etc/sudoers.d/group01
state: present
create: true
mode: 0440
line: "%group01 ALL=(ALL) NOPASSWD: ALL"
validate: /usr/sbin/visudo -cf %s 
The | |
The | |
The | |
The | |
The | |
The |
An example of the sudo validation command can be found in the examples section of the output from the ansible-navigator doc ansible.builtin.lineinfile command.
Users Module Ansible Documentation
How do I generate encrypted passwords for the user module
Group Module Ansible Documentation
SSH Known Hosts Module Ansible Documentation
Authorized Key Module Ansible Documentation
The Lookup Plugin Ansible Documentation