Manually setting up and configuring servers is time-consuming, error-prone, and difficult to scale. Ansible, an open-source automation tool, makes this process easier by allowing system administrators to define server configurations as code. This maintains uniformity, reduces manual labor, and enables rapid deployments. In this article, we’ll look at how Ansible may be used to automate server setup and configuration efficiently.

Why Choose Ansible for Automation?

Ansible stands out among automation tools due to its simplicity and agentless architecture. Here are some key benefits of using Ansible:

  • Agentless: Unlike other configuration management tools, Ansible does not require an agent to be installed on target machines. It uses SSH to communicate with remote servers.
  • Idempotent Execution: Ansible ensures that a configuration is applied only when needed, preventing unintended changes.
  • Declarative Syntax: Using YAML-based playbooks, users can describe the system’s desired state rather than writing complex scripts.
  • Scalability: Ansible can manage multiple servers at once, making it suitable for enterprise environments.
  • Cross-Platform Support: Works across different operating systems, including Linux, Windows, and macOS.

Step 1 – Update all the System Packages to make sure your Ubuntu server is up to date.

sudo apt update
sudo apt install ansible -y

No verify the installation using the command;

ansible --version

Step 2 – Configure Ansible Inventory

Ansible uses an inventory file to define the servers it manages. Create or modify the inventory file (/etc/ansible/hosts or a custom file) and add your server.

[webservers] server1.example.com server2.example.com
[databases] db1.example.com

You can use the appropriate ones.

Step 3 – Automating Server Setup with Ansible Playbooks

A playbook is a YAML file containing tasks that Ansible executes on remote servers. Below here is an example Playbook: Setting Up an Apache Web Server.

name: Setup Apache Web Server 
hosts: webservers
become: yes tasks:
 - name: Install Apache 
   apt:
   name: apache2 
   state: present
   when: ansible_os_family == "Debian"
 - name: Install Apache (RHEL-based systems) 
    yum:
   name: httpd 
   state: present
   when: ansible_os_family == "RedHat"
- name: Start and Enable Apache 
  service:
  name: apache2 
  state: started 
  enabled: yes
  when: ansible_os_family == "Debian"
- name: Start and Enable Apache (RHEL-based systems) 
   service:
   name: httpd state: started enabled: yes
   when: ansible_os_family == "RedHat"

Running the Playbook

To execute the playbook, use the following command.

ansible-playbook -i inventory_file setup_apache.yml

Automating User Management with Ansible

To create and manage users on multiple servers, use the following playbook:

name: Create Users 
hosts: all
become: yes tasks:
- name: Add a new user 
  user:
   name: deployer 
   state: present 
   groups: sudo

Best Practices for Using Ansible

  • Use Roles: Organize playbooks into roles for reusability.
  • Secure Credentials: Use Ansible Vault to store sensitive data.
  • Modular Playbooks: Break down large playbooks into smaller, modular components.
  • Use Handlers: Ensure efficient service restarts only when necessary.

Conclusion

Ansible streamlines server setup and configuration management by using a declarative, scalable, and efficient method. Administrators can use playbooks to automate infrastructure provisioning, which ensures consistency and reduces manual errors. Begin using Ansible immediately to

improve your DevOps workflow and simplify IT operations.

Server Hosting Solutions by RackNerd:

Shared Hosting
cPanel Web Hosting in US, Europe, and Asia datacenters
Logo
Reseller Hosting
Create your new income stream today with a reseller account
Logo
VPS (Virtual Private Server)
Fast and Affordable VPS services - Instantly Deployed
Logo
Dedicated Servers
Bare-metal servers, ideal for the performance-demanding use case.
Logo

Leave a comment

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