Search This Blog

Saturday 27 February 2021

Ansible Playbook for Linux User Creation

Step 1) Create a simple playbook using user ansible module for user creation.

[osboxes@master ansible-playbooks]$ cat>linux-user.yml
---
- name: Create a Linux User
  hosts: all
  become: true
  become_user: root
  tasks:
      - name: User Account Creation
        user:
          name: pavan
          uid: 3405
          state: present

Step 2) Run the playbook using --syntax-check for syntax verification.

[osboxes@master ansible-playbooks]$ ansible-playbook linux-user.yml -i inventory.txt --syntax-check

playbook: linux-user.yml

Step 3) Run playbook using -C option for trial run. It will not perform actual task rather it will check if the task is going to be successful or failed. Here in the following example it says that task is going to be failed but it did not perform anything. Error is due to not providing -K option for asking password. 

[osboxes@master ansible-playbooks]$ ansible-playbook -C linux-user.yml -i inventory.txt

PLAY [Create a Linux User] **********************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
fatal: [linuxhost]: FAILED! => {"msg": "Missing sudo password"}

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

I did the trail run again and it says task is going to be successful. 

[osboxes@master ansible-playbooks]$ ansible-playbook -C linux-user.yml -i inventory.txt -K
BECOME password:

PLAY [Create a Linux User] **********************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]

TASK [User Account Creation] ********************************************************************************************************************************
changed: [linuxhost]

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

Step 3) Now run the actual playbook task and verify if the user is created on remote host. 

[osboxes@master ansible-playbooks]$ ansible-playbook linux-user.yml -i inventory.txt -K
BECOME password:

PLAY [Create a Linux User] **********************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]

TASK [User Account Creation] ********************************************************************************************************************************
changed: [linuxhost]

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


Step 4) Verify if the linux user is created on remote host

[osboxes@linuxhost ~]$ id pavan
uid=3405(pavan) gid=1001(sftpusers) groups=1001(sftpusers)

No comments: