Operations 29 min read

Master Ansible: From Basics to Advanced Configuration and Commands

This comprehensive guide walks you through Ansible fundamentals, installation methods, core concepts, configuration files, and essential command‑line usage, helping you automate Linux and Windows environments efficiently and securely.

Open Source Linux
Open Source Linux
Open Source Linux
Master Ansible: From Basics to Advanced Configuration and Commands

1. Basics

The future of operations relies on automation tools that can handle everything from simple All‑In‑One deployments to complex, high‑availability clusters with caching, message queues, and big‑data storage, shifting delivery standards from days to minutes.

Definition : Ansible is a key automation tool that originated from the novel "Ender's Game" and offers 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

Why choose Ansible

Python‑based, low learning curve for operators

Rich built‑in modules and commercial extensions

Decentralized, simple migration of configuration centers

Agentless – no client configuration needed

How it works

Ansible uses SSH on Linux and PowerShell on Windows; the control node must be Linux. Modules are invoked on the target hosts, and temporary files are removed after execution.

The workflow involves three roles:

Users – can trigger runs via CMDB, API, ad‑hoc commands, or pre‑written playbooks

Ansible toolset – the ansible command coordinates inventory, API, and modules

Target objects – any Linux/Windows host, cloud, or network device

Key components include Playbook (YAML/JSON task list), Inventory (host list), Modules, Plugins, and API.

2. Communication and Execution

Since Ansible 1.3 the default communication is OpenSSH (Windows uses PowerShell). Password and SSH authentication are supported.

3. Installation

Using pip:

// Install Python
yum install python-pip python-devel -y
// Install build 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

Using yum: yum install ansible -y Verify installation:

# ansible --version
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
...

4. Directory Structure

Query the installed files:

# rpm -ql ansible
/etc/ansible/               # configuration files
/usr/bin/ansible-xxx        # executable binaries
/usr/lib/pythonX.X/site‑packages/ansible/   # library files
/usr/share/doc/ansible‑xxxx/               # documentation

5. Configuration File Details

The main config is /etc/ansible/ansible.cfg. Important sections:

defaults : basic settings (e.g., forks, timeout)

privilege_escalation : sudo configuration

ssh_connection : SSH arguments, control path, pipelining

accelerate : optional acceleration daemon

colors : output color settings

Example snippet:

[defaults]
# some basic default values...
#forks = 5
#poll_interval = 15

[privilege_escalation]
#become=True
#become_method=sudo
#become_user=root

[ssh_connection]
#ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
#pipelining = False

6. Ansible Commands Overview

Common executables are located in /usr/bin/ (e.g., ansible, ansible-playbook, ansible-doc, ansible-galaxy, ansible-vault).

Key command categories:

ansible : ad‑hoc tasks, e.g., ansible all -m win_ping ansible-doc : view module documentation, e.g., ansible-doc -l ansible-galaxy : role management, similar to pip

ansible-playbook : run playbooks for batch operations

ansible-pull : pull‑based execution mode

ansible-vault : encrypt/decrypt sensitive data

Example ad‑hoc usage:

# Check host reachability
ansible linux -m ping

# Get hostname of all Linux hosts
ansible linux -m command -a 'hostname'

# List hosts in the "linux" group
ansible linux --list-hosts

7. Inventory File Explained

The inventory (default /etc/ansible/hosts) follows INI syntax. Groups are defined in brackets, hosts can be listed directly or with ports, and variables can be assigned per host or group.

# Hosts
192.168.37.149
ntp.magedu.com:2222

# Group definition
[webservers]
web1.magedu.com http_port=808 maxRequestsPerchild=801
web[10:20].magedu.com

# Group variables
[webservers:vars]
ntp_server=ntp.magedu.com

8. Two Core Command Sets

Ad‑hoc commands are for quick, one‑off tasks. Syntax: ansible <host-pattern> [options] with options like -m (module), -a (arguments), -i (inventory), -u (remote user), etc.

Examples:

# Ping all hosts in group "linux"
ansible linux -m ping

# Run a shell command on the group
ansible linux -m command -a 'hostname'

# Show hosts in a group
ansible linux --list-hosts

Playbooks (YAML files) define ordered tasks for multiple hosts, enabling repeatable automation.

Ansible workflow diagram
Ansible workflow diagram

Overall, this guide provides a step‑by‑step reference for installing Ansible, configuring its core files, understanding its architecture, and using both ad‑hoc commands and playbooks to automate infrastructure tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Configuration ManagementAnsible
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.