Mastering Ansible: From Basics to Advanced Automation
This comprehensive guide walks you through Ansible fundamentals, installation, configuration, inventory management, core commands, and practical ad‑hoc examples, empowering operations teams to automate deployments, configurations, and routine tasks with speed and reliability.
Fundamentals
Operations automation is evolving from simple All‑In‑One deployments to complex clusters, high‑availability, caching, message queues, and more, demanding tools that can complete tasks in minutes rather than days.
1.1 Definition
The automation goal is to run repeatable tasks automatically without human intervention. Ansible, named after a sci‑fi communication device, is a popular open‑source automation engine that simplifies software deployment, configuration, management, continuous integration, and zero‑downtime upgrades.
System layer: Linux, Windows
Virtualization: VMWare, Docker, OpenStack
Commercial hardware: F5, ASA
Applications: Apache, Zabbix, RabbitMQ, SVN, Git
1.2 Why Choose Ansible
Python‑based, low learning curve for operations staff
Rich built‑in modules and commercial extensions
Decentralized – simple migration of configuration centers
Agentless – only the control node needs configuration
1.3 How It Works
Ansible is agentless; it uses OpenSSH on Linux and PowerShell on Windows. The control node (must be Linux) runs modules that push commands to managed hosts and cleans up temporary files after execution.
The workflow involves three roles: the user, the Ansible toolset, and the target objects (hosts, clouds, etc.). Key components include Playbook, Inventory, Modules, Plugins, and API.
1.4 Communication
Since Ansible 1.3 the default transport is OpenSSH (or PowerShell for Windows) with password or key authentication.
1.5 Installation
# Install Python
yum install python-pip python-devel -y
# Upgrade pip
pip install --upgrade pip
# Install Ansible
pip install ansible --upgrade # Install via yum
yum install ansible -yVerify the installation with ansible --version.
1.6 Directory Layout
Configuration files: /etc/ansible/
Executables: /usr/bin/ansible‑*
Python libraries: /usr/lib/pythonX.X/site‑packages/ansible/
Documentation: /usr/share/doc/ansible‑*/
1.7 Configuration File Details
The main configuration file is /etc/ansible/ansible.cfg. Important sections include [defaults], [privilege_escalation] (sudo), [ssh_connection], [accelerate], and [colors]. Example snippets illustrate how to enable sudo, set SSH arguments, and customize output colors.
1.8 Common Commands
Key executables reside in /usr/bin/. Examples: ansible – ad‑hoc command execution ansible-doc – view module documentation ansible-galaxy – role management ansible-playbook – run playbooks ansible-pull – pull mode execution ansible-vault – encrypt/decrypt sensitive data
Sample usage: ansible all -m win_ping shows success or failure with colored output.
1.9 Inventory File
Inventory is an INI‑style file (default /etc/ansible/hosts) that defines hosts and groups, supports ranges, host variables, group variables, and nested groups. Examples demonstrate host definitions, host‑specific variables, group variables, and group children.
Two Major Command Sets
2.1 Ad‑hoc Commands
Ad‑hoc commands are one‑off tasks such as checking host reachability or running a command on a group.
Example: ansible linux -m ping returns “pong”.
Example: ansible linux -m command -a 'hostname' returns each host’s hostname.
Example: ansible linux --list-hosts lists all hosts in the “linux” group.
Signed-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.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
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.
