Master Ansible Basics: Essential Modules and Commands for Automation
This guide walks you through Ansible's core architecture, host inventory setup, variable definitions, and the most commonly used modules—including group, user, copy, cron, shell, and ping—showing practical command examples and how to retrieve module help with ansible‑doc.
Introduction
This article, originally presented by Linux operations expert Wei Wei, introduces the fundamentals of Ansible for beginners and intermediate automation engineers.
Ansible Architecture
Ansible consists of several core components:
Ansible core : the Ansible software itself.
Host inventory : a list of managed hosts defined in /etc/ansible/hosts.
Core modules : built‑in modules that perform most tasks.
Custom modules : user‑written modules for specific functions.
Playbook : a file that defines a series of tasks.
Connection plugin : handles the initial connection to the first managed host.
Host Inventory and Variables
Hosts are defined in /etc/ansible/hosts. Example entry:
[webservers]
www1.magedu.com http_port=80 maxRequestsPerChild=100
www2.magedu.com http_port=8080 maxRequestsPerChild=200Host‑specific variables can be added in the inventory and are accessible within playbooks. Group variables are defined for all hosts in a group, e.g.:
[webservers:vars]
ntp_server=ntp.magedu.com
nfs_server=nfs.magedu.comBasic Ansible Command Syntax
The general command format is:
ansible <host-pattern> [-f forks] [-m module_name] [-a args]Common options: -f forks: number of parallel processes. -m module_name: module to use. -a args: arguments for the module.
Key Modules and Examples
Group Module
Creates or manages groups on remote hosts.
# ansible datanodes -m group -a 'name=developer'Result example:
{
"changed": true,
"gid": 501,
"name": "developer",
"state": "present",
"system": false
}To delete the group:
# ansible datanodes -m group -a 'name=developer state=absent'User Module
Manages user accounts.
# ansible all -m user -a 'name=dev uid=666 group=developer'To remove the user:
# ansible all -m user -a 'name=dev uid=666 group=developer state=absent'Copy Module
Transfers files to remote hosts.
# ansible datanodes -m copy -a 'src=/etc/issue dest=/tmp/issue.ansible mode=600 owner=zabbix'Using the content parameter to create a file with inline text:
# ansible datanodes -m copy -a 'content="hello world!
how are you?
" dest="/tmp/content.ansible"'Cron Module
Manages scheduled tasks.
# ansible data1 -m cron -a 'name="pingtest" minute="*/1" job="ping www.baidu.com"'To delete a job, set state=absent.
Shell Module
Executes commands on remote hosts, supporting shell features like pipelines.
# ansible target -m shell -a 'ps aux | grep httpd'Ping Module
Checks network connectivity; a successful host returns pong.
# ansible all -m pingGetting Module Help
Use ansible-doc to list modules or view detailed documentation.
ansible-doc -l # list all modules
ansible-doc -s group # show example usage for the group moduleThe left column shows module names, the right column provides brief descriptions.
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.
