Open In App

How to Create User using Ansible Playbook?

Last Updated : 24 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Ansible Architecture Diagram

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 ?

  • Playbooks are ansibles’ configuration and orchestration language written in yaml.
  • Playbooks are ansible execution files in which files there are steps of tasks defined and it plays a plays like number of tasks are there.
  • Playbooks serves as the primary for automating the tasks and orchestration management.
  • Playbooks allow you to define the desired state of your infrastructure, and Ansible takes care of ensuring that the systems reach that state.

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:

  • AMI– amazon Linux 2
  • Instance type- t2.micro
  • Security group- allow SSH(22),HTTP(80),HTTPS(443) traffic from anywhere
  • Configure storage – 8gb with root volume type gp2
  • Connect the ec2 instance with the terminal with the help of ssh.
ssh -i  "keypair-pem file" ec2-user@<instance-public-ip address>compute-1.amazonaws.com

EC2 Console

Step 2: Installing Ansible

  • install Ansible with amazon-linux-extras and the version of ansible is 2.9.23.
sudo amazon-linux-extras install ansible2

Installing Ansible

Step 3: Hosts file configuration

  • After installed the ansible in /etc/ansible directory a hosts file is created.
  • we need to configure this file called hosts.
sudo vi /etc/ansible/hosts

Configuring Host File

  • configure the inventory file or hosts file located at /etc/ansible/hosts with slave server’s details such as host_name,ansible_host,ansible_user, ssh_private_key_file path…..
  • for this create one group name as [localhost] you can configure this group name as you wish.
  • below this group localhost we add the slave server details.
  • set the permissions to the keypair with read access only
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

Configuring Inventory File

Step 4: Check Connection Between the Master and Slave server

  • now we need to check the connection to this slave server with the ping module.
ansible [host_name] -m ping

Checking Host Node Connectivity

Step 5: Create a Playbook file to Create User

  • to write a playbook we need to create file for this playbook
sudo vi filename.yml

Create playbook file

  • Write the Playbook Code in yaml file as shown in below:
- 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)

Writing a Ansible Playbook

  • you can check the playbook syntax error
ansible-playbook <playbook_filename> --syntax-check

Step 6: Execute the Playbook

  • execute the playbook file playbook.yml which has yaml script.
ansible-playbook adduser.yml

Executing Playbook

  • playbook is successfully executed.

Step 7: Verifying User Creation

  • we need to make sure the user is successfully created or not.
  • we can check the user id and users’ details from this ansible by using ad-hoc command.
ansible localhost -m command -a "id Vamsi"

Verfiying User Creation

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads