Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible create Users and Group

Tags:

ansible

I am trying to create new users and groups using Ansible playbook. Below is my folder structure.

tree
.
├── create-users.yaml
└── ubuntu

create-users.yaml playbook contains create user and group tasks. Note, I am not having any group (admin_group) and users (Rajini, Kamal) in my target machine, instead they will be created when running the playbook.

---
- name:  Create Users & Groups
  hosts: target1
  gather_facts: false
  tasks:
    - name: Create Users Task
      user:
        name: "{{ item }}"
        state: present
        password: "{{ 'default_user_password' | password_hash('sha512','A512') }}"
        shell: /bin/bash
        groups: "{{ admin_group }}"
      loop:
        - Rajini
        - Kamal

I have another file called ubuntu to pick group name and password. When running the playbook I am getting below error.

ansible-playbook --vault-id @prompt create-users.yaml -K
BECOME password:
Vault password (default):

PLAY [Create Users & Groups] *****************************************************************************************************************************************************************

TASK [Create Users Task] *********************************************************************************************************************************************************************
fatal: [target1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'admin_group' is undefined\n\nThe error appears to be in '/home/osboxes/Ansible_Project/web_deployment/Ansible/groups_vars/create-users.yaml': line 6, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - name: Create Users Task\n      ^ here\n"}

PLAY RECAP ***********************************************************************************************************************************************************************************
target1                    : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

admin_group: admin
default_user_password: Password1

Can somebody please help me on this?

Updating Output after getting help from user Moon.

ansible-playbook --vault-id @prompt create-users.yaml -K
BECOME password:
Vault password (default):

PLAY [Create Users & Groups] *****************************************************************************************************************************************************************

TASK [Create Users Task] *********************************************************************************************************************************************************************
changed: [target1] => (item=Rajini)
changed: [target1] => (item=Kamal)

PLAY RECAP ***********************************************************************************************************************************************************************************
target1                    : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

ssh [email protected]
[email protected]'s password:
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 5.0.0-23-generic x86_64)
Kamal@Ansible_Target1:~$ id
uid=1005(Kamal) gid=1001(admin) groups=1001(admin)
like image 607
Anonymuss Avatar asked Oct 26 '25 07:10

Anonymuss


1 Answers

Couple of things:

  • To use variables from ubuntu file you need specify the vars file in playbook.
  • To use default_user_password as a variable, remove the quotes '
  • If you want admin as the users primary group then use group attribute instead. groups on the other hand takes a list and add the users to the listed groups.

And, if the group isn't created yet on the target machine then first create the group using group module.

Playbook after the above changes.

---
- name: Create Users & Groups
  hosts: target1
  gather_facts: false
  vars_files: ubuntu
  tasks:
    - name: Create group
      group:
        name: "{{ admin_group }}"
        state: present

    - name: Create Users Task
      user:
        name: "{{ item }}"
        state: present
        password: "{{ default_user_password | password_hash('sha512','A512') }}"
        shell: /bin/bash
        group: "{{ admin_group }}"
      loop:
        - Rajini
        - Kamal
like image 141
Moon Avatar answered Oct 29 '25 08:10

Moon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!