Master Ansible: From Basics to Advanced Automation with Playbooks
This article provides a comprehensive guide to Ansible, covering its purpose, key features, installation steps, SSH key setup, inventory configuration, core commands, essential modules, playbook syntax, handlers, role structures, and practical examples for automating Linux system administration.
Ansible Overview
Ansible is a popular open‑source automation tool that simplifies operations by allowing engineers to execute tasks across many hosts with minimal effort, reducing manual errors and increasing efficiency.
Key Features
Written in Python, easy to extend.
Thousands of built‑in modules.
One command can manage thousands of machines.
Agent‑less, communicates over SSH.
Adopted by major cloud providers and enterprises.
Ansible Roles
Users : Interact with Ansible via CMDB integration, public/private APIs, ad‑hoc commands, or pre‑written playbooks.
Toolset : Consists of Inventory, Modules, Plugins, and API.
Targets : Any Linux or non‑Linux host, as well as network devices.
Installation & Basic Configuration
Ansible can be installed on a Linux control node (e.g., RedHat, Debian, CentOS) using YUM:
<code># 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</code>Verify installation:
<code># ansible --version
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
python version = 2.7.5</code>Set up password‑less SSH for remote hosts:
<code># ssh-keygen -t rsa
# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]</code>Inventory Configuration
The inventory file (default
/etc/ansible/hosts) defines host groups. Example entries:
<code>[web]
192.168.100.20
192.168.100.30
[test]
www.example.com:222
mail
yj1.kgc.cn
yj[2:5].kgc.cn</code>Run commands against groups or individual hosts using the
-ior
--inventory-fileoptions.
Core Commands
ansible: Execute ad‑hoc tasks (e.g.,
ansible all -m ping).
ansible-doc: Show module documentation.
ansible-playbook: Run YAML playbooks.
ansible-console: Interactive shell for Ansible.
Common Modules
command : Run a command without shell features.
shell : Run a command with full shell support.
copy : Transfer files to remote hosts.
hostname : Manage remote hostnames.
yum : Manage packages on RPM‑based systems.
service : Control system services.
user : Manage user accounts.
Playbook Basics
Playbooks are YAML files that describe a series of tasks. A minimal example:
<code>---
- hosts: web1
remote_user: root
tasks:
- name: add user
user:
name: user1
state: present
- name: copy file
copy:
src: /etc/passwd
dest: /home
</code>Key elements include
hosts,
remote_user,
tasks,
handlers, and
roles. Playbooks can be validated (
--syntax-check), tested (
-C), and executed (
ansible-playbook playbook.yml).
Handlers
Handlers run only when notified by a task and execute after all tasks finish. Example:
<code>- name: change port
command: sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
notify: restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted</code>Roles
Roles are reusable collections of files, templates, tasks, handlers, and variables stored under
/etc/ansible/roles/. A typical role directory contains
files,
templates,
tasks,
handlers,
vars,
defaults, and
meta. Roles can be invoked in a playbook:
<code>- hosts: web
roles:
- mysql
- httpd</code>Practical Example: Installing MariaDB
A playbook can automate the installation of MariaDB, copy configuration files, restart the service, create a database, and grant privileges.
Overall, Ansible provides a powerful, agent‑less framework for automating configuration management, application deployment, and routine operational tasks across heterogeneous environments.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.