πŸš€ Getting Started with Ansible for DevOps Automation

Deepak RajDeepak Raj
2 min read

Ansible is a powerful open-source automation tool used for configuration management, application deployment, and orchestration. In this post, I’ll walk you through my learning notes and practical insights while working with Ansible β€” from passwordless SSH setup to writing roles and playbooks.


πŸ” Passwordless SSH Authentication

Before using Ansible, it's essential to set up passwordless authentication between the Ansible controller (master) and the managed nodes (targets).

Steps:

  1. Run ssh-keygen on the Ansible controller to generate SSH keys.

  2. Copy the public key (id_rsa.pub) to the target node’s ~/.ssh/authorized_keys.

  3. Use the private IP address of the target server to establish the connection.

This setup allows Ansible to connect securely without prompting for a password.


πŸ“‚ Ansible Essentials

  • Inventory File: Lists all target servers and can be grouped like:

      iniCopyEdit[webservers]
      192.168.1.10
    
      [dbservers]
      192.168.1.20
    
  • Ad-Hoc Commands: Quick, one-time commands, such as:

      bashCopyEditansible -i inventory all -m shell -a "touch devopsclass"
    

Use ad-hoc for fewer tasks. For automation of multiple steps, use playbooks.


πŸ“œ Playbooks

A playbook is a YAML file containing a series of tasks to be executed on managed nodes.

Example: Installing and Starting Nginx

- name: Install and start nginx
  hosts: all
  become: true

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes

    - name: Install nginx
      apt:
        name: nginx
        state: present

    - name: Start nginx
      service:
        name: nginx
        state: started

Run the Playbook:

ansible-playbook -vvv -i inventory first-playbook.yaml

🧩 Ansible Roles

Roles are an efficient way to organize complex playbooks.

Create a Role:

ansible-galaxy role init kubernetes

Role Directory Structure:

kubernetes/
β”œβ”€β”€ README.md        # Info about the role
β”œβ”€β”€ defaults/        # Default variables
β”œβ”€β”€ files/           # Static files
β”œβ”€β”€ handlers/        # Handlers for services
β”œβ”€β”€ meta/            # Role metadata (dependencies etc.)
β”œβ”€β”€ tasks/           # Main list of tasks
β”œβ”€β”€ templates/       # Jinja2 templates
β”œβ”€β”€ tests/           # Test playbooks or inventories
└── vars/            # Role variables

βœ… Summary

  • Setup SSH access for seamless automation.

  • Use ad-hoc commands for quick tasks.

  • Write playbooks for structured workflows.

  • Use roles for scalable and reusable configurations.


🧠 This is a concise learning log from my Ansible journey as part of my DevOps studies. Hope it helps you get started with automation using Ansible!

Want to see the full source files? Check them out on my GitHub:
πŸ‘‰ GitHub Repo: devops-shell-notes

0
Subscribe to my newsletter

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

Written by

Deepak Raj
Deepak Raj