π Getting Started with Ansible for DevOps Automation

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:
Run
ssh-keygen
on the Ansible controller to generate SSH keys.Copy the public key (
id_rsa.pub
) to the target nodeβs~/.ssh/authorized_keys
.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
Subscribe to my newsletter
Read articles from Deepak Raj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
