Operations 26 min read

Master Ansible: From Basics to Advanced Automation with Real‑World Playbooks

This guide introduces Ansible’s architecture, installation, core modules, command‑line usage, and advanced playbook techniques, providing step‑by‑step examples such as LAMP stack deployment and Kubernetes installation to help readers automate Linux system administration efficiently.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
Master Ansible: From Basics to Advanced Automation with Real‑World Playbooks

Overview

Ansible is an open‑source automation and configuration management tool for Unix‑like systems, written in Python. It communicates with managed nodes over SSH, requires no client software on the nodes, and uses YAML and Jinja2 templates for declarative configuration.

Official website

https://www.ansible.com/

Key Features

Agent‑less deployment – only the control node needs Ansible installed.

Modular architecture – modules perform specific tasks.

SSH‑based communication by default.

Centralized management of inventory and playbooks.

Simple, powerful syntax with strong extensibility.

API and custom module support via Python.

Playbooks enable complex state management.

Good support for cloud platforms and big‑data environments.

Idempotent operations – repeated runs produce the same result.

Architecture

Ansible pushes modules to target hosts over SSH, executes them, and removes them afterward. Core components include connection plugins, host inventory, core and custom modules, and playbooks.

Installation

1. Enable the EPEL repository: yum install epel-release -y 2. Install Ansible via yum: yum install ansible 3. Verify the installation: ansible --version Typical command syntax:

ansible [-i inventory] [-f forks] [group] -m module_name -a "module_args"

ansible‑doc

List all modules: ansible-doc -l Show parameters for a specific module:

ansible-doc -s <module_name>

Basic Usage

Define an inventory file (e.g., /etc/ansible/hosts) with groups and host variables such as ansible_ssh_port, ansible_ssh_user, and ansible_ssh_pass. Test connectivity with: ansible -i /etc/ansible/hosts web-servers -m ping Run ad‑hoc commands, for example:

ansible -i /etc/ansible/hosts web-servers -m command -a "uptime"

Advanced Modules

command : default module, runs commands without a shell.

shell : runs commands through /bin/sh, allowing pipes and redirection.

script : copies a local script to the remote host and executes it.

copy : transfers files (similar to scp).

file : manages file attributes such as permissions.

stat : gathers detailed file information.

get_url : downloads files from a URL with checksum verification.

yum : manages packages on RPM‑based systems.

cron : manipulates crontab entries.

service : controls system services (start, stop, restart, enable).

user : creates and manages user accounts.

Playbooks

A playbook is a YAML file that defines a list of plays. Each play maps a group of hosts to a set of tasks. Example task syntax:

- name: Install httpd
  yum: name=httpd state=present

Typical directory layout for a role includes tasks, files, templates, vars, handlers, and meta.

Real‑World Example: LAMP Stack Deployment

The playbook creates roles for preparation, MySQL, PHP, and Apache, installs required packages, copies configuration files, and ensures services are started. Example snippets:

- name: Install httpd
  yum: name=httpd state=present
- name: Deploy index.php
  copy: src=index.php dest=/var/www/html/

Execution command:

ansible-playbook -i /etc/ansible/hosts /etc/ansible/lamp/roles/site.yml

Real‑World Example: Kubernetes Installation

Clone the Ansible‑based k8s installer repository and run the appropriate playbook for single‑master or multi‑master deployment:

git clone https://github.com/lizhenliang/ansible-install-k8s
ansible-playbook -i hosts single-master-deploy.yml -u root -k

Images

Ansible architecture diagram
Ansible architecture diagram
Ansible components diagram
Ansible components diagram
Configuration ManagementDevOpsAnsiblePlaybooks
Linux Cloud Computing Practice
Written by

Linux Cloud Computing Practice

Welcome to Linux Cloud Computing Practice. We offer high-quality articles on Linux, cloud computing, DevOps, networking and related topics. Dive in and start your Linux cloud computing journey!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.