Open In App

How to Create User using Ansible Playbook?

In the domain of IT mechanization, overseeing client accounts across various servers can be a drawn-out and mistake-inclined task. Ansible, a strong mechanization device, offers a smoothed-out answer for this test by empowering clients to make, design, and oversee client accounts effectively and reliably across different frameworks.

By leveraging Ansible playbooks for user management, organizations can ensure consistency, reliability, and scalability in their IT operations. Ansible’s simplicity, flexibility, and extensive module library make it an ideal choice for automating user account management tasks in modern IT environments. Utilizing Ansible, framework administrators can characterize undertakings to make client accounts, set passwords, relegate authorizations, and oversee other client attributes across numerous servers and devices easily. Ansible’s agentless engineering permits it to speak with remote hosts over SSH (Secure Shell) or other transport protocols, executing assignments in parallel across distributed environments.



In this guide, we will explore how to use Ansible and create users using the playbook on Linux systems, empowering administrators to maintain framework trustworthiness and effectiveness easily.

What is Ansible?

Ansible is a collection of software tools that enables configuring systems, deploying software, and orchestrating advanced workflows to support application deployment, system updates, and more. Ansible is an open-source automation platform that automates IT tasks, simplifies complex workflows, and accelerates the delivery of infrastructure and applications. We can manage and configure other servers called slave servers from a single server called the master server.



When we install Ansible on the master server, a file is created called Hosts, also known as the inventory path. Inside this inventory path, we give details of slave servers, such as IP addresses, key pair paths, and hostnames. With this, we create a bridge between slave and master to manage configuration.

Developed by Red Hat, Ansible utilizes a simple YAML-based language (YAML Ain’t Markup Language) to define automation playbooks, enabling users to describe the desired state of their systems and automate tasks across a wide range of environments, from on-premises servers to cloud platforms. With its agentless architecture and powerful module ecosystem, Ansible empowers organizations to automate repetitive tasks, enforce consistency, and improve scalability, making it an indispensable tool for DevOps teams, system administrators, and network engineers alike. Ansible’s ease of use, flexibility, and robust features make it a popular choice for automating infrastructure management, configuration management, application deployment, and continuous delivery pipelines.

They are two working methods in Ansible:

What Are Ansible Ad-hoc Commands?

Ad-hoc command are one-line and short commands. which are executed from ansible command line tool to automate a single task on one or more managed nodes/slave servers these commands are quick and easy but not reusable.

Here is the structure of ad-hoc command written as

ansible [group_name or host_name] -m(module) command -a(argument) "parameters"

What Are Ansible Playbooks ?

Playbook Example

The following is the example of writing playbook:

- name: Install and start Nginx
  hosts: web_servers
  become: yes
  tasks:
    - name: Install Nginx package
      yum:
        name: nginx
        state: present
    - name: Ensure Nginx service is running
      service:
        name: nginx
        state: started
        enabled: yes

How to Create User using Ansible Playbook: A Step-By-Step Guide

Here, we are going to create user using ansible playbook in aws EC2 instance.

Step 1: Create An AWS account And launch EC2 instance

Launch An Instance With these below Configuration:

ssh -i  "keypair-pem file" ec2-user@<instance-public-ip address>compute-1.amazonaws.com

Step 2: Installing Ansible

sudo amazon-linux-extras install ansible2

Step 3: Hosts file configuration

sudo vi /etc/ansible/hosts

sudo chmod 400 keypair_name.pem
[group_name]
host_name ansible_host=<Private-IP> ansible_user=ec2-user ansible_ssh_private_key_file path=/parh/to/keypair

Step 4: Check Connection Between the Master and Slave server

ansible [host_name] -m ping

Step 5: Create a Playbook file to Create User

sudo vi filename.yml

- name: I want to create a User using ansible playbook
   hosts: localhost
   become: yes  # Use sudo privileges if required for user creation
   tasks:
    - name: Adding the user
      user:
        name: Vamsi  # Replace with the username you want to create
        state: present
        shell: /bin/bash  # Specify the shell for the user (optional)
        comment: "surname is KANAKALA"  # Add a comment for the user (optional)
        createhome: yes  # Create the user's home directory (optional)

ansible-playbook <playbook_filename> --syntax-check

Step 6: Execute the Playbook

ansible-playbook adduser.yml

Step 7: Verifying User Creation

ansible localhost -m command -a "id Vamsi"

Creating User Using Ansible Playbook – FAQs

After creating the User can We verify the User id?

You can simply use the ad-hoc command using command module with arguments and parameters. If the user is present, it shows the users’ id and group id

Could Ansible manage Windows hosts?

yes, Ansible can oversee Windows hosts not with standard Linux and Unix frameworks. It gives worked in modules explicitly to overseeing Windows hosts permitting clients to perform undertakings like introducing programming, designing administrations, and overseeing clients and gatherings on Windows frameworks.

How Can I get Sensitive Information in Ansible playbooks?

Ansible gives a few instruments to safely managing delicate information, including Ansible Vault for scrambling records containing touchy data, and Ansible Pinnacle (or AWX) for putting away certifications safely and infusing them into playbooks at runtime.

What are Some Best Practices for writing Ansible Playbooks?

Some best practices for writing Ansible playbooks include using descriptive variable names, organizing playbooks into logical sections, leveraging roles for code reuse, using tags for selective execution, and testing playbooks thoroughly in development environments before deploying them to production.


Article Tags :