Operations 26 min read

Master Ansible Automation: A Complete Guide to Playbooks, Modules, and Roles

This comprehensive tutorial introduces Ansible, covering its core concepts, features, installation, inventory management, command-line usage, essential modules, playbook syntax, handlers, and role-based organization, enabling readers to automate infrastructure tasks efficiently and reliably.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Ansible Automation: A Complete Guide to Playbooks, Modules, and Roles

Ansible Overview

Ansible is a popular open‑source automation tool that simplifies operations by enabling repeatable, error‑free management of large numbers of hosts.

Key Features

Python‑based, easy to extend for developers.

Rich built‑in modules covering most automation needs.

Agentless architecture using SSH.

Scales to thousands of machines with a single command.

Supported by major cloud providers and vendors.

Ansible Roles

How users interact with Ansible (CMDB, API, ad‑hoc commands, playbooks).

Core toolset includes Inventory, Modules, Plugins, and API.

Supported objects span Linux, non‑Linux, public/private clouds, and network devices.

Installation

Ansible can be installed via YUM; only Python and SSH are required. Example repository configuration and installation commands:

# cd /mnt/ansiblerepo/ansiblerepo/repodata/
# vim /etc/yum.repos.d/local.repo
[local]
name=centos
baseurl=file:///mnt/ansiblerepo/ansiblerepo
enabled=1
gpgcheck=0
# yum -y install ansible

Verify installation:

# ansible --version
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
python version = 2.7.5

SSH Key Setup for Password‑less Login

# ssh-keygen -t rsa
# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

Inventory Configuration

The inventory file (default /etc/ansible/hosts) defines host groups and can include ports, wildcards, and range expressions.

[web]
192.168.100.20
192.168.100.30
[test]
www.benet.com:222
mail
yj1.kgc.cn
yj[2:5].kgc.cn

Common Ansible Commands

ansible all -m ping

– check connectivity. ansible web --list – list hosts in a group. ansible web -m command -a "df -hT" – run ad‑hoc commands. ansible-playbook playbook.yml – execute a playbook.

Modules Overview

command : Executes a command without shell features.

shell : Executes a command with full shell support.

copy : Copies files to remote hosts.

hostname : Manages hostnames.

yum : Manages packages via YUM.

service : Controls services.

user : Manages user accounts.

Playbook Structure

Playbooks are YAML files that define hosts, remote_user, tasks, handlers, and roles. Example snippet:

---
- hosts: web1
  remote_user: root
  tasks:
    - name: add user
      user:
        name: user1
        state: present
      tags:
        - aaa
    - name: add group
      group:
        name: root
        system: yes
      tags:
        - bbb
- hosts: web2
  remote_user: root
  tasks:
    - name: copy file to web
      copy:
        src: /etc/passwd
        dest: /home
      tags:
        - ccc
...

Handlers and Roles

Handlers run only when notified by a task, ensuring actions like service restarts happen after configuration changes. Roles organize related tasks, handlers, files, templates, and variables under a directory structure (e.g., /etc/ansible/roles/mysql).

Example: Deploy MariaDB with a Role

The article outlines a full workflow: install MariaDB, copy configuration files, restart the service, create a database, and grant privileges, all orchestrated through Ansible roles and playbooks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

configuration-management
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.