How to create a user in linux with Ansible Playbook

In this example, the playbook is run on all hosts in the inventory and uses become: true to run the tasks with superuser privileges. The task creates a new user named “newuser” and adds them to the “wheel” group. The password is hashed using SHA-512 encryption. You can also use ansible-vault to encrypt the password if you want to keep it more secure.

---
- hosts: all
  become: true
  tasks:
  - name: Create a new user
    user:
      name: newuser
      state: present
      groups: wheel
      password: $6$rounds=656000$5saltstringsaltstring$

Note:

  • hosts: all means this playbook will run on all the machines in the inventory file.
  • become: true means to run the task with superuser privilege.
  • password: $6$rounds=656000$5saltstringsaltstring$ is a hashed password, you can also use plain-text password but that is not recommended for security reason.
  • You can also use ansible modules like useradd and passwd to create user and set password as well.

Leave a Reply

Your email address will not be published. Required fields are marked *