DevOps Day 13 — Getting Started with Ansible: Installation, Ad-hoc Commands & Playbooks

Welcome to Day 13 of the DevOps Series.

As infrastructure grows, managing servers manually becomes difficult and error-prone.

This is where Ansible plays a major role in DevOps by enabling automation, consistency, and scalability.

In Day 13, we focus on hands-on Ansible basics — from installation to running our first playbook.


✅ What We’ll Learn Today

  • How to install Ansible

  • Why passwordless SSH is required

  • Running Ansible ad-hoc commands

  • Creating inventory files

  • Writing and executing Ansible playbooks

  • Understanding verbose logs

  • Introduction to Ansible roles


πŸ”Ή 1. Ansible Installation

Ansible is usually installed on a control node (your laptop or EC2 instance).

On Ubuntu:

sudo apt update sudo apt install ansible -y

Other platforms:

  • Mac: Homebrew (brew install ansible)

  • Windows: Chocolatey (mainly for testing)

πŸ“Œ Using package managers is recommended because they handle dependencies automatically.

Verify installation:

ansible --version

πŸ”Ή 2. Passwordless Authentication (Very Important)

Ansible works using SSH, so it must connect to target servers without asking for a password.

Step 1: Generate SSH key

ssh-keygen

Press Enter for all options.

Step 2: Copy public key to target server

ssh-copy-id user@target-server-ip

After this, you should be able to login without a password:

ssh user@target-server-ip

✅ This is mandatory for Ansible automation.


πŸ”Ή 3. Ansible Inventory File

Inventory defines which servers Ansible manages.

Example:

[web] 192.168.1.10 192.168.1.11 [db] 192.168.1.20

You can group servers logically:

  • web servers

  • database servers

  • application servers

This makes targeting easy and clean.


πŸ”Ή 4. Ansible Ad-hoc Commands

Ad-hoc commands are used for one-time tasks
(no playbook required).

Example: ping all servers

ansible all -i inventory -m ping

Create a file on all servers:

ansible all -i inventory -m shell -a "touch test.txt"

πŸ“Œ Useful for:

  • checking connectivity

  • restarting services

  • quick troubleshooting


πŸ”Ή 5. Writing Your First Ansible Playbook

Playbooks are written in YAML format.

Basic YAML structure:

--- - name: Install Nginx hosts: all become: true tasks: - name: Install nginx apt: name: nginx state: present - name: Start nginx service service: name: nginx state: started

Key sections:

  • name → description

  • hosts → target machines

  • become → run as root

  • tasks → automation steps

πŸ“Œ Always prefer Ansible modules over shell commands.


πŸ”Ή 6. Executing the Playbook

ansible-playbook -i inventory nginx.yml

You’ll see:

  • Gathering facts

  • Task execution

  • Success or failure status

This ensures repeatable and reliable automation.


πŸ”Ή 7. Verbose Mode (Debugging)

To understand what Ansible is doing internally:

ansible-playbook -i inventory nginx.yml -v ansible-playbook -i inventory nginx.yml -vv ansible-playbook -i inventory nginx.yml -vvv

More v = more details.

Very useful during troubleshooting.


πŸ”Ή 8. Introduction to Ansible Roles

When playbooks become large, managing everything in one file becomes messy.

πŸ‘‰ Roles help organize code

Create a role:

ansible-galaxy role init nginx

Roles separate:

  • tasks

  • variables

  • handlers

  • templates

  • files

Used heavily in:

  • Kubernetes setup

  • Production automation

  • Enterprise environments


πŸš€ Why This Matters in Real DevOps



Ansible helps DevOps engineers:

  • Automate server setup

  • Maintain consistency

  • Reduce manual errors

  • Manage thousands of servers

  • Work efficiently with cloud infrastructure

This is why Ansible is one of the most in-demand DevOps skills today.

Comments

Popular posts from this blog

🧩 DevOps Day 1 — Fundamentals of DevOps

DevOps Day 23 — Multi-Stage Docker Builds & Distroless Images: Build Smaller, Safer Containers

πŸš€ DevOps Day 2 — Understanding the SDLC and the Role of DevOps Engineers