🎯 Ansible Basics: Getting Started with Automation

Getting Started with Ansible Automation

In this beginner-friendly tutorial, we’ll explore the basics of Ansible and create a simple playbook to configure a web server. You’ll learn how to install Ansible, set up your inventory, and write your first playbook.

What is Ansible?

Ansible is an open-source automation tool that helps you:

  • Configure systems
  • Deploy software
  • Orchestrate advanced workflows
  • Manage infrastructure as code

Prerequisites

  • Ubuntu/Debian or RHEL/CentOS system
  • Python 3.x installed
  • Basic command line knowledge
  • SSH access to target servers

Installation

  1. Install Ansible: ```bash

    For Ubuntu/Debian

    sudo apt update sudo apt install ansible

For RHEL/CentOS

sudo yum install epel-release sudo yum install ansible


2. Verify installation:
```bash
ansible --version

Basic Concepts

  1. Control Node: The machine where Ansible is installed
  2. Managed Nodes: The servers you want to manage
  3. Inventory: List of managed nodes
  4. Playbooks: YAML files containing automation tasks
  5. Tasks: Individual units of work
  6. Modules: Pre-built functions for specific operations

Practical Example: Web Server Setup

Let’s create a playbook that sets up a basic web server with Nginx.

  1. Create inventory file (hosts):
    [webservers]
    webserver1 ansible_host=192.168.1.10
    
  2. Create playbook (setup_webserver.yml): ```yaml —
    • name: Configure Web Server hosts: webservers become: yes tasks:
    • name: Update apt cache apt: update_cache: yes when: ansible_os_family == “Debian”

    • name: Install Nginx package: name: nginx state: present

    • name: Start Nginx service service: name: nginx state: started enabled: yes

    • name: Create custom index page copy: content: | <!DOCTYPE html> <html> <head> Welcome to My Server </head> <body> <h1>Hello from Ansible!</h1> <p>This page was configured using Ansible automation.</p> </body> </html> dest: /var/www/html/index.html ```

  3. Run the playbook:
    ansible-playbook -i hosts setup_webserver.yml
    

Understanding the Playbook

Our playbook:

  1. Updates package cache (Debian/Ubuntu only)
  2. Installs Nginx
  3. Ensures Nginx is running and starts on boot
  4. Creates a custom index page

Best Practices

  1. Inventory Organization:
    • Group related servers
    • Use meaningful hostnames
    • Document host variables
  2. Playbook Structure:
    • Use descriptive names
    • Include comments
    • Keep tasks focused
  3. Security:
    • Use SSH keys instead of passwords
    • Limit sudo access
    • Encrypt sensitive data with ansible-vault

Verification

After running the playbook:

  1. Check Nginx status:
    ansible webservers -i hosts -m command -a "systemctl status nginx"
    
  2. Test web access:
    curl http://192.168.1.10
    

Common Issues & Solutions

  1. SSH Connection Issues:
    # Add to ansible.cfg
    [defaults]
    host_key_checking = False
    
  2. Permission Errors:
    • Ensure proper sudo configuration
    • Check file permissions
  3. Package Installation Fails:
    • Verify internet connectivity
    • Check repository access

Next Steps

  1. Explore more Ansible modules
  2. Learn about variables and templates
  3. Try configuring multiple servers
  4. Practice writing idempotent playbooks

Conclusion

You’ve learned the basics of Ansible automation and created your first working playbook. This foundation will help you explore more advanced Ansible features and tackle larger automation projects.

For the next tutorial in this series, we’ll dive into intermediate concepts like roles, variables, and templates.

Written on July 10, 2025