Ansible - Create a user

> Infrastructure as code > Ansible

1 - About

An how to of the User module of Ansible

3 - Management

3.1 - 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 | 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]
Advertising

3.2 - Current User (whoami)

- name: get the username running the deploy
  become: false
  local_action: command whoami
  register: current_user

- debug: var=current_user

It should return ansible_user or remote_user

3.3 - Add a group

# 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
infra/ansible/user.txt · Last modified: 2019/09/11 12:15 by gerardnico