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.
Basic Deployment
Install Ansible
# yum -y install epel-release
# yum list all *ansible*
# yum info ansible
# yum -y install ansibleAnsible configuration files
/etc/ansible/ansible.cfg Main configuration file
/etc/ansible/hosts Inventory
/usr/bin/ansible-doc Help files
/usr/bin/ansible-playbook Playbook runnerDefine Inventory
# cd /etc/ansible/
# cp hosts{,.bak}
# > hosts
# cat hosts
[webserver]
127.0.0.1
192.168.10.149
[dbserver]
192.168.10.113Key‑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_NAMECommon 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
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.
