How to create/modify a User in Ansible?

Card Puncher Data Processing

About

user management in Ansible - An how to of the User module of Ansible

Management

Create a user

  - name: "The group for the user "
    become: yes
    become_user: root
    group:
      name: 'usergroup'
      state: present
    
  - name: "Create a user"
    become: yes
    become_user: root
    user:
      name: 'username'
      comment: user name
      shell: /bin/bash
      state: present
      group: 'usergroup'
      groups: 'usergroup, usergroup2'
      password: "{{ vault_dev_login_password |  password_hash('sha512') }}"

where:

  • vault_dev_login_password is a vault variable (encrypted) but it may be not.

Example of output:

The warning is not true.

TASK [The group for the user] *************************************************************************************
changed: [dev-host]
changed: [dev-host-standby]

TASK [Create a user] **************************************************
 [WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this
module to work properly.

changed: [dev-host]
changed: [dev-host-standby]

Current User (whoami)

# The login user
# It should return ''ansible_user'' or ''remote_user''
- name: get the username running the deploy
  become: false
  local_action: command whoami
  register: local_user
- debug: var=local_user

# or the running user
- name: get the username running the deploy
  command: 'whoami'
  register: running_user
- debug: var=running_user

Add a group

Linux - Group (Gid)

# Otherwise you get a permissions errors when downloading
- name: "Sqlline - Add the group {{ sqlline_group }} to the ansible_user {{ ansible_user }}  "
  user:
    name: '{{ ansible_user }}'
    shell: /bin/bash
    groups: '{{ sqlline_group }}'
    append: true





Discover More
Card Puncher Data Processing
Ansible - Module

Module are the component that are called via a task modules_by_category With ansible, example running a module...



Share this page:
Follow us:
Task Runner