Bookmark this page

Managing Users and Authentication

Objectives

After completing this section, you should be able to manage Linux users and groups, configure SSH, and modify Sudo configuration on managed hosts.

The User Module

The Ansible user module lets you manage user accounts on a remote host. You can manage a number of parameters including remove user, set home directory, set the UID for system accounts, manage passwords and associated groupings. To create a user that can log into the machine, you need to provide a hashed password for the password parameter. See the reference section for a link to "How do I generate encrypted passwords for the user module?"

Example of the User Module

- name: Add new user to the development machine and assign the appropriate groups.
  user:
    name: devops_user 1
    shell: /bin/bash 2
    groups: sys_admins, developers 3
    append: yes

1

The name parameter is the only requirement in the user module and is usually the service account or user account.

2

The shell parameter optionally sets the user's shell. On other operating systems, the default shell is decided by the tool being used.

3

The groups parameter along with the append parameter tells the machine that we want to append the groups sys_asmins and developers with this user. If you do not use the append parameter then the groups will overwrite in place.

When creating a user you can specify it to generate_ssh_key. This will not overwrite an existing SSH key.

Example of User Module Generating an ssh key

- name: Create a SSH key for user1
  user:
    name: user1
    generate_ssh_key: yes
    ssh_key_bits: 2048
    ssh_key_file: .ssh/id_my_rsa

Note

The user module also offers some return values. Ansible modules can take a return value and register them into a variable. Find out more with ansible-doc and on the main doc site.

Table 9.1. Some commonly used parameters

ParameterComments
commentOptionally sets the description of a user account.
groupOptionally sets the user's primary group.
groupsList of multiple groups. When set to a null value, all groups except the primary group is removed.
homeOptionally sets the user's home directory.
create_homeTakes a boolean value of yes or no. A home directory will be created for the user if the value is set to yes.
systemWhen creating an account state=present, setting this to yes makes the user a system account. This setting cannot be changed on existing users.
uidSets the UID od user.

The Group Module

The group module allows you to manage (add, delete, modify) groups on the managed hosts. You need to have groupadd, groupdel or groupmod. For windows targets, use the win_group module.

Example of the group module

- name: Verify that auditors group exists
  group:
    name: auditors
    state: present

Table 9.2. Parameters for the group module

ParameterComments
gidOptional GID to set for the group.
localForces the use of "local" command alternatives on platforms that implement it.
nameName of the group to manage.
stateWhether the group should be present or not on the remote host.
systemIf set to yes, indicates that the group created is a system group.

The Known Hosts Module

If you have a large number of host keys to manage you will want to use the known_hosts module. The known_hosts module lets you add or remove host keys from the known_hosts file on managed host.

Example of known_host Tasks

- name: copy host keys to remote servers
  known_hosts:
    path: /etc/ssh/ssh_known_hosts
    name: host1
    key: "{{ lookup('file', 'pubkeys/host1') }}"1

1

A lookup plugin allows Ansible to access data from outside sources.

The Authorized Key Module

The authorized_key module allows you to add or remove SSH authorized keys per user accounts. When adding and subtracting users to a large bank of servers, you need to be able to manage ssh keys.

Example of authorized_key Tasks

- name: Set authorized key
  authorized_key:
    user: user1
    state: present
    key: "{{ lookup('file', '/home/user1/.ssh/id_rsa.pub') }}1

1

A key can also be taken from a url: https://github.com/user1.keys.

Revision: rh294-8.4-9cb53f0