Operations 20 min read

Master Ansible: From Installation to Playbooks and Advanced Modules

This comprehensive guide walks you through installing Ansible, configuring its core files, defining inventories and variables, using key modules, crafting ad‑hoc commands, building playbooks, leveraging roles, applying tags, and mastering YAML syntax for efficient automation across diverse environments.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Ansible: From Installation to Playbooks and Advanced Modules

Basic Deployment

Install Ansible

# yum -y install epel-release
# yum list all *ansible*
# yum info ansible
# yum -y install ansible

Ansible configuration files

/etc/ansible/ansible.cfg    Main configuration file
/etc/ansible/hosts          Inventory
/usr/bin/ansible-doc        Help files
/usr/bin/ansible-playbook   Playbook runner

Define Inventory

# cd /etc/ansible/
# cp hosts{,.bak}
# > hosts
# cat hosts
[webserver]
127.0.0.1
192.168.10.149

[dbserver]
192.168.10.113

Key‑based SSH connection

# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]

Ansible command basics

# ansible-doc -l
# ansible-doc -s MODULE_NAME

Common modules

Examples include command, cron, user, group, copy, file, ping, service, shell, script, yum, setup and others. Each module’s parameters and typical usage are shown.

Ansible Playbooks

A playbook consists of one or more plays. Each play defines hosts, remote_user, variables, tasks, handlers, and roles. Tasks invoke modules with arguments; handlers are triggered by notifications; roles provide reusable components.

YAML Basics

YAML is a human‑readable data‑serialization format. It uses indentation, hyphens for lists, and colons for key‑value pairs. Example structures for scalars, lists, and dictionaries are provided.

Fundamental Elements

Variables

Variable names may contain letters, numbers and underscores, starting with a letter. Facts are gathered from remote hosts and can be accessed via ansible_facts. The register keyword stores task output for later use. Variables can be passed via --extra-vars or through roles.

Inventory

Inventory files group hosts, support host‑specific variables, group variables, and nested groups. Parameters such as ansible_ssh_host, ansible_ssh_port, ansible_ssh_user, etc., control connection details.

Conditional execution

Use when statements with Jinja2 expressions to run tasks only when conditions are met. Filters like failed, success, and skipped can be applied to registered results.

Loops

Iterate over items with with_items or newer loop syntax. Lists and dictionaries can be looped to create multiple resources.

Roles

Roles organize playbooks into reusable directories containing tasks, files, templates, handlers, vars, and meta. They can be included in a playbook with the roles keyword and accept parameters.

Tags

Tags allow selective execution of tasks. Use --tags with ansible‑playbook to run only tagged sections, and the special tag always forces execution.

Author: kangvcar Source: https://my.oschina.net/kangvcar/blog/1830155
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-management
MaGe Linux Operations
Written by

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.

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.