Master Ansible: From Basics to Advanced Automation with Playbooks
This comprehensive guide introduces Ansible, covering its core concepts, features, installation, inventory setup, ad‑hoc commands, module usage, playbook creation, handlers, and role-based organization to enable efficient, agent‑less automation across diverse Linux and network environments.
Ansible Overview
Ansible is a popular open‑source automation tool that simplifies configuration management, application deployment and task execution across thousands of hosts using simple, agent‑less SSH communication.
Key Features
Written in Python, easy for developers to extend.
Thousands of built‑in modules cover most tasks.
One command can manage thousands of machines.
Agent‑less, uses SSH.
Adopted by major cloud providers.
Ansible Roles
Users: how to interact with Ansible.
Toolset: inventory, modules, plugins, API.
Targets: hosts and groups that can be managed.
Users
Ansible can be driven via CMDB integration, public/private APIs, ad‑hoc commands or pre‑written playbooks (see diagram).
Toolset
The core components are Inventory, Modules, Plugins and API.
Ansible Playbooks : YAML files that define ordered tasks.
Inventory : host list, supports grouping.
Modules : execution units, built‑in or custom.
Plugins : extend functionality (connection, loop, vars, filter).
API : programmatic access for integration.
Targets
Ansible can manage Linux, non‑Linux systems and various network devices.
Installation and Configuration
Installation
Install via YUM on a Linux control node (RedHat, Debian, CentOS). Example repository setup 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 ansibleVerify Installation
# ansible --version
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]SSH Key Setup
Create RSA key pair and copy the public key to managed hosts to enable password‑less SSH.
# 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 groups and hosts. Example:
[web]
192.168.100.20
192.168.100.30
[test]
www.benet.com:222
[mail]
yj1.kgc.cn
yj[2:5].kgc.cnHosts can belong to multiple groups and groups can be referenced with the -i or --inventory-file options.
Running Commands
Examples of ad‑hoc commands, module usage and playbook execution are provided, such as pinging all hosts, listing groups, checking disk usage, installing packages, managing services, users, and files.
# ansible all -m ping
# ansible web -m command -a "df -hT"
# ansible web -m yum -a "name=httpd state=present"
# ansible web -m service -a "name=httpd enabled=yes state=restarted"Playbooks
Playbooks are YAML files that describe a series of tasks. They support variables, handlers, roles and tags. Example snippet:
---
- hosts: web1
remote_user: root
tasks:
- name: add user
user: name=user1 state=present
tags: aaa
- name: copy file
copy: src=/etc/passwd dest=/home
tags: cccHandlers run only when notified by a task, ensuring actions such as service restarts occur after configuration changes.
Roles
Roles encapsulate related tasks, files, templates and variables under a directory structure (e.g., /etc/ansible/roles/mysql). They can be included in playbooks to reuse common functionality.
- hosts: web
roles:
- mysql
- httpdSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
