Getting Started with Ansible: A Comprehensive Guide

SdeepSdeep
3 min read

Ansible is a powerful open-source automation tool used for configuration management, application deployment, and task automation. It simplifies IT operations by allowing administrators to manage multiple servers efficiently. In this blog, we’ll cover the basics of Ansible, from installation to writing playbooks and using roles.


1. Ansible Installation

Ansible is easy to install and runs on most Unix-like systems. Here’s how to install it on an Ubuntu-based system:

On the Ansible Control Node (Server):

sudo apt update
sudo apt install ansible -y

Verify Installation:

ansible --version

2. Setting Up Ansible Server and Target Server (EC2 Instance)

Prerequisites:

  • Ansible Control Node (where Ansible is installed)

  • One or more Target Nodes (servers to manage, e.g., EC2 instances)

Steps:

  1. Launch EC2 Instances (one for Ansible server, others as target nodes).

  2. Ensure SSH Access from the Ansible server to target nodes.

  3. Update /etc/hosts (optional) for easy server referencing:

     sudo nano /etc/hosts
    

    Add:

     192.168.1.10 ansible-server
     192.168.1.11 web-server
     192.168.1.12 db-server
    

3. Configure Passwordless Authentication Using SSH Keygen

For Ansible to communicate with target nodes without passwords, set up SSH key-based authentication.

On the Ansible Control Node:

  1. Generate SSH Key Pair:

     ssh-keygen -t rsa -b 4096
    
  2. Copy Public Key to Target Nodes:

     ssh-copy-id user@web-server
     ssh-copy-id user@db-server
    
  3. Test SSH Login:

     ssh user@web-server
    

4. Ansible Ad-hoc Commands

Ad-hoc commands allow quick execution of tasks without writing a playbook.

Basic Commands:

  • Ping all servers:

      ansible all -m ping
    
  • Check disk space:

      ansible all -m command -a "df -h"
    
  • Install a package (e.g., Nginx):

      ansible web-servers -m apt -a "name=nginx state=present" --become
    

5. Inventory File (Grouping Servers)

Ansible uses an inventory file (/etc/ansible/hosts by default) to define target servers.

Example Inventory File:

[web_servers]
web-server ansible_host=192.168.1.11 ansible_user=ubuntu

[db_servers]
db-server ansible_host=192.168.1.12 ansible_user=ubuntu

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Using Custom Inventory:

ansible -i /path/to/inventory_file web_servers -m ping

6. Ansible Playbook

Playbooks are YAML files that define automation tasks.

Example Playbook (nginx_install.yml):

---
- name: Install and Start Nginx
  hosts: web_servers
  become: yes
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
    - name: Start Nginx Service
      service:
        name: nginx
        state: started
        enabled: yes

Run the Playbook:

ansible-playbook nginx_install.yml

7. Ansible Galaxy (Roles)

Roles provide reusable automation components. Ansible Galaxy is a hub for sharing roles.

Install a Role:

ansible-galaxy install geerlingguy.nginx

Create a Custom Role:

ansible-galaxy init my_role

This creates a structured directory:

my_role/
├── defaults/
├── tasks/
├── handlers/
├── templates/
└── vars/

Use a Role in a Playbook:

---
- name: Use Nginx Role
  hosts: web_servers
  roles:
    - geerlingguy.nginx

Conclusion

Ansible simplifies IT automation with its agentless architecture and easy-to-learn syntax. In this guide, we covered:

  • Installing Ansible

  • Setting up servers and SSH authentication

  • Running ad-hoc commands

  • Managing inventory files

  • Writing playbooks

  • Using Ansible Galaxy roles

With these basics, you can automate server configurations, deployments, and more efficiently.

Next Steps:

  • Explore Ansible Vault for secrets management

  • Learn about Templates (Jinja2) for dynamic configurations

  • Dive into Ansible Tower/AWX for enterprise automation

Happy Automating! 🚀


Would you like a deeper dive into any specific topic? Let me know in the comments!

#DevOps #Ansible #Automation #CloudComputing #SysAdmin

0
Subscribe to my newsletter

Read articles from Sdeep directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sdeep
Sdeep

👋 Hello! I'm passionate about DevOps and I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!