Operations 28 min read

Unlock Powerful Automation with Ansible: A Complete Guide

Ansible is a popular open‑source automation platform that simplifies configuration management, deployment, and orchestration across Linux and non‑Linux hosts, offering modules, playbooks, inventories, and roles, with detailed installation steps, command usage, module examples, and best practices for efficient operations.

Linux Cloud Computing Practice
Linux Cloud Computing Practice
Linux Cloud Computing Practice
Unlock Powerful Automation with Ansible: A Complete Guide

1. Overview

Ansible is an open‑source automation tool that improves operational efficiency and reduces human error by providing a simple, agent‑less way to manage both Linux and non‑Linux systems.

2. Features

Written in Python, making it easy to extend.

Over a thousand built‑in modules cover most tasks.

One command can control thousands of hosts.

Agent‑less operation using SSH.

Adopted by major cloud providers and vendors such as AWS, GCP, Azure, Cisco, HP, VMware, and Twitter.

3. Roles and Users

Users can interact with Ansible in four ways: through a CMDB, public/private API calls, ad‑hoc command sets, or pre‑written Playbooks. The diagram below illustrates these interaction methods.

Ansible user interaction diagram
Ansible user interaction diagram

4. Toolset

Ansible’s core components are Inventory, Modules, Plugins, and API. Inventory defines host groups; Modules perform actions; Plugins add extra functionality; API enables programmatic control.

Playbooks : YAML files that describe ordered tasks.

Inventory : Host list, similar to /etc/hosts.

Modules : Core functional units, extensible by users.

Plugins : Optional extensions (connection, loop, variable, filter, etc.).

API : Interface for third‑party integration.

5. Target Objects

Ansible can manage any host—Linux, non‑Linux, or network devices—by defining them in the inventory.

6. Configuration

6.1 Installation

Ansible can be installed via RPM/YUM. Only Python and SSH are required.

# 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 (default)

6.2 Inventory

The default inventory file is /etc/ansible/hosts. Hosts are grouped with brackets, and groups can be referenced in commands using the -i or --inventory-file options. # ansible -i /etc/ansible/hosts web -m ping Example /etc/ansible/hosts:

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

6.3 Common Commands

ansible all -f 5 -m ping

– check connectivity of all hosts. ansible web --list – list hosts in the web group. ansible web -m command -a "df -hT" – display disk usage on the web group.

7. Modules

7.1 command

Executes a command on remote hosts without shell features.

# ansible web -m command -a "chdir=/ ls ./"

7.2 shell

Executes a command through the remote shell, supporting pipes and redirection.

# ansible web -m shell -a "echo hello world"
# ansible web -m shell -a "echo hello world > /1.txt"

7.3 copy

Copies files to remote hosts.

# ansible web -m copy -a "src=/etc/hosts dest=/root/a1.hosts mode=777 owner=root group=root"

7.4 hostname

Manages the remote host name.

# ansible 192.168.100.20 -m hostname -a "name=test"

7.5 yum

Manages packages via the yum package manager.

# ansible web -m yum -a "name=httpd state=present"
# ansible web -m shell -a "rpm -qa | grep httpd"

7.6 service

Controls services on remote hosts.

# ansible web -m service -a "name=httpd enabled=yes state=restarted"

7.7 user

Manages user accounts.

# ansible web -m user -a "name=user01 system=yes uid=502 group=root groups=root shell=/etc/nologin home=/home/user01 password=pwd@123"

8. Playbooks

Playbooks are YAML files that define a series of tasks. They provide a structured, repeatable way to automate complex workflows.

---
- hosts: web1
  remote_user: root
  tasks:
    - name: adduser
      user: name=user1 state=present
      tags: [aaa]
    - name: addgroup
      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]
...

9. Handlers

Handlers are tasks that run only when notified by other tasks, typically used to restart services after configuration changes.

---
- hosts: web1
  remote_user: root
  tasks:
    - name: change port
      command: sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
      notify: restart httpd server
  handlers:
    - name: restart httpd server
      service: name=httpd state=restarted
...

10. Roles

Roles organize reusable components (tasks, handlers, vars, files, templates) under /etc/ansible/roles. A typical role directory includes tasks/main.yml, handlers/main.yml, vars/main.yml, etc.

Ansible role directory structure
Ansible role directory structure

Roles can be invoked in a playbook with the roles directive, allowing multiple roles (e.g., mysql and httpd ) to be applied to target hosts.

Overall, Ansible provides a lightweight, extensible framework for automating infrastructure, application deployment, and routine tasks, making it a cornerstone technology for modern operations teams.

Configuration ManagementDevOpsAnsible
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.