Master Ansible: From Basics to Advanced Automation for Ops Teams
This comprehensive guide introduces Ansible's fundamentals, explains why it is the preferred automation tool, details its architecture, communication methods, installation steps, configuration files, inventory management, and demonstrates common Ad‑Hoc and playbook commands for efficient operations across Linux and Windows environments.
1. Basics
The future of operations relies on traditional industries leveraging cloud and AI to process big data, requiring increasingly sophisticated automation tools to deliver value in minutes rather than days.
1.1 Definition
Automation aims to run repetitive tasks automatically without human intervention. Ansible is a key tool that helps achieve this by providing modules for software deployment, configuration, management, CI/CD, and zero‑downtime upgrades.
System layer: Linux, Windows
Virtualization: VMWare, Docker, OpenStack
Commercial hardware: F5, ASA
System applications: Apache, Zabbix, RabbitMQ, SVN, GIT
1.2 Why Choose Ansible
Python‑based, low learning curve for operators
Rich built‑in modules and commercial extensions
Decentralized configuration migration
Agentless: no client setup required
1.3 Working Principle
Ansible has no client; it uses SSH on Linux and PowerShell on Windows. The control node authenticates, pushes modules to targets, executes them, and cleans up temporary files.
Ansible workflow involves four roles:
Users (via CMDB, API, Ad‑Hoc, or playbooks)
Ansible command line
Inventory (host list)
Modules, plugins, and API
1.4 Communication Method
Since Ansible 1.3 the default is OpenSSH (Linux) and PowerShell (Windows). Authentication can be password or SSH key based.
1.5 Installation
# Install Python
yum install python-pip python-devel -y
# Install gcc and development tools
yum install gcc glibc-devel zlib-devel rpm-build openssl-devel -y
# Upgrade pip
pip install --upgrade pip
# Install Ansible
pip install ansible --upgrade
# Verify installation
ansible --versionAlternatively, use the system package manager:
yum install ansible -y1.6 Directory Structure
Run rpm -ql ansible to view the layout:
Configuration files: /etc/ansible/ Executable binaries: /usr/bin/ansible‑* Python libraries: /usr/lib/pythonX.X/site‑packages/ansible/ Documentation:
/usr/share/doc/ansible‑*/1.7 Configuration File Details
The main config file is /etc/ansible/ansible.cfg. Important sections include:
defaults : basic settings such as forks, timeout, and fact gathering.
privilege_escalation : sudo configuration for non‑root users.
ssh_connection : SSH arguments, control path, and pipelining.
accelerate : optional acceleration daemon settings.
colors : output color customization.
1.8 Common Ansible Commands
Executable files reside in /usr/bin/. Key commands: ansible: run ad‑hoc tasks on hosts. ansible-doc: view module documentation. ansible-galaxy: manage roles from Ansible Galaxy. ansible-playbook: execute playbooks. ansible-vault: encrypt/decrypt sensitive data.
Example of checking host reachability:
ansible all -m win_ping1.9 Inventory Configuration
The inventory file ( /etc/ansible/hosts by default) defines hosts and groups in INI style. Hosts can be listed by IP or hostname, with optional port numbers. Groups are declared in brackets, and a host can belong to multiple groups.
# Example inventory
[webservers]
web1.magedu.com http_port=808 maxRequestsPerchild=801
web[10:20].magedu.com
[dbservers]
db-a.magedu.com
db-[b:f].magedu.com
[webservers:children]
apache
nginx
[webservers:vars]
ntp_server=ntp.magedu.com2. Two Common Command Sets
2.1 Ad‑Hoc Commands
Ad‑Hoc commands are one‑off operations executed directly from the command line. Syntax:
ansible <host-pattern> -m <module> -a <arguments>Typical options include -i for inventory, -f for parallelism, -u for remote user, and -k to prompt for SSH password.
Examples:
Check if hosts are alive: ansible linux -m ping Run a command on all hosts: ansible linux -m command -a 'hostname' List hosts in a group:
ansible linux --list-hosts2.2 Playbooks
Playbooks are YAML files that define a series of tasks to be applied to groups of hosts, providing a reusable and version‑controlled automation workflow.
Playbooks are executed with ansible-playbook <playbook.yml> and can include variables, handlers, and roles for complex deployments.
2.3 Additional Tools
ansible-galaxy: install and manage reusable roles. ansible-vault: encrypt sensitive variables and files.
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.
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.
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.
