DEV Community

Cover image for From Manual to Automated: How Ansible Can Save You Hours!
Rijul Rajesh
Rijul Rajesh

Posted on

4 4 4 4 3

From Manual to Automated: How Ansible Can Save You Hours!

If you’ve ever found yourself manually configuring servers, running the same commands over and over, or troubleshooting inconsistencies across environments, then you’ll love Ansible. It’s like having a reliable assistant who follows your instructions perfectly every single time.

What is Ansible?

Ansible is an open-source configuration management, application deployment, and automation tool. Unlike other automation tools, Ansible is agentless, meaning you don’t need to install anything extra on your target machines—just SSH (for Linux/macOS) or WinRM (for Windows).

It uses YAML-based playbooks to describe the desired state of your infrastructure, making it easy to read and write even if you’re not a full-time DevOps engineer.

Why Use Ansible?

  • Simple & Human-Readable: Playbooks use YAML, which is easy to understand.
  • Agentless: No need to install any software on remote machines.
  • Scalable: Manage one machine or thousands with ease.
  • Cross-Platform: Works with Linux, macOS, Windows, and cloud services like AWS, Azure, and Google Cloud.

Setting Up Ansible

To get started, install Ansible on your control machine (the machine that will run the playbooks). If you’re on Linux/macOS, simply run:

sudo apt update && sudo apt install ansible -y   # Debian/Ubuntu
sudo dnf install ansible -y                      # Fedora
brew install ansible                             # macOS
Enter fullscreen mode Exit fullscreen mode

For Windows, you can install Ansible via WSL (Windows Subsystem for Linux) or use a Linux-based control node.

Understanding the Inventory File

Before running a playbook, Ansible needs to know which machines to manage. This is done using an inventory file, which is a simple text file listing remote servers and grouping them.

Example inventory file:

[webservers]
192.168.1.10
192.168.1.11

[databases]
dbserver.example.com ansible_user=admin ansible_port=2222
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • [webservers] and [databases] are groups of machines.
  • Each line under a group represents a host (IP address or hostname).
  • You can specify connection details like ansible_user and ansible_port for individual hosts.

Running Ansible on Your Own Machine

If you want to test Ansible locally, you can use localhost as your target machine.

Create a Local Inventory File

[local]
localhost ansible_connection=local
Enter fullscreen mode Exit fullscreen mode

Example Playbook to Install a Package on Your Own Machine

- name: Setup Local Machine
  hosts: local
  become: true
  tasks:
    - name: Install Git
      apt:
        name: git
        state: present
Enter fullscreen mode Exit fullscreen mode

Running the Playbook

ansible-playbook -i inventory local-setup.yml
Enter fullscreen mode Exit fullscreen mode

This will install Git on your local machine using Ansible.

Writing Your First Playbook

A more comprehensive Ansible playbook to install NGINX, start the service, and ensure it's enabled on boot looks like this:

- name: Configure Web Server
  hosts: webservers
  become: true
  tasks:
    - name: Install NGINX
      apt:
        name: nginx
        state: present

    - name: Start NGINX service
      service:
        name: nginx
        state: started
        enabled: true

    - name: Create a simple index.html
      copy:
        content: "<h1>Welcome to Ansible-managed NGINX!</h1>"
        dest: /var/www/html/index.html

    - name: Open firewall for HTTP traffic
      ufw:
        rule: allow
        port: '80'
        proto: tcp
Enter fullscreen mode Exit fullscreen mode

Breaking the configuration down

  • hosts: - Specifies which machines to run the playbook on.
  • become: - Runs tasks with sudo privileges.
  • tasks: - A list of actions to perform.
  • apt: - Installs the NGINX package on Debian-based systems.
  • service: - Ensures NGINX is running and enabled at boot.
  • copy: - Creates a simple index.html file.
  • ufw: - Opens port 80 for HTTP traffic.

Running Your Playbook

Assuming you have a list of remote servers defined in your inventory file, run the playbook with:

ansible-playbook -i inventory webserver-setup.yml
Enter fullscreen mode Exit fullscreen mode

And now you now have a fully configured NGINX web server up and running with firewall rules applied.

What’s Next?

Ansible is a powerful yet beginner-friendly tool that can automate everything from simple server setups to complex multi-tier deployments. Some exciting things to explore next:

  • Roles & Modular Playbooks – Organize your playbooks better.
  • Jinja2 Templating – Dynamically generate configurations.
  • Ansible Tower – A web-based UI for managing playbooks.

Learn More

If you are interested in exploring such new techniques and technologies, take a look at LiveAPI.

Its a Super-Convenient tool which you can use to generate Interactive API docs instantly! So if you are exploring a codebase, and it doesn't have a documentation ready, you can just use this to get it generated, and refer it for getting a better idea and saving time.

You can instantly try it out here!

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay