๐ Ansible Summary Guide
๐ Ansible Summary Guide
I. What is Ansible?
Ansible is an open-source automation tool used for:
-
Configuration management
-
Application deployment
-
Provisioning
-
Orchestration
It is agentless, using SSH (or WinRM) to manage remote systems.
II. Core Concepts
-
Playbook
A YAML file that defines a set of tasks to be executed on hosts. It's the main way you describe automation in Ansible. -
Inventory
A list of managed nodes (hosts), defined in a file (hosts
), INI, YAML, or dynamically (e.g., via AWS). -
Module
A unit of work. Ansible ships with hundreds of modules (e.g.,yum
,apt
,copy
,template
,user
,service
, etc.). -
Task
A single action to be executed (e.g., install a package, start a service). -
Role
A standardized way to organize tasks, handlers, files, variables, etc. Reusable and modular. -
Handler
Special tasks triggered only by anotify
from another task (e.g., restart nginx if config changes). -
Facts
System information (gathered bysetup
module) about a host (OS, IP, memory, etc.).
III. Ansible Architecture
-
Control Node
The machine where Ansible is installed and run from. It connects to managed nodes. -
Managed Nodes
The remote systems (Linux, Windows, etc.) that are configured and managed. -
Connection Methods
-
SSH (default): For Linux/Unix nodes.
-
WinRM: For Windows nodes.
-
-
Inventory File Example
IV. Playbook Example
V. Important Directories (in Roles)
VI. Command Line Usage
Command | Description |
---|---|
ansible | Run ad-hoc commands. |
ansible-playbook | Run playbooks. |
ansible-galaxy | Manage roles from Ansible Galaxy. |
ansible-vault | Encrypt/decrypt sensitive data. |
VII. Variables in Ansible
-
Defined in playbooks, inventory, roles, or extra vars.
-
Can be encrypted with Vault.
-
Precedence: CLI > Playbook > Role vars > Inventory vars > Facts > Defaults
VIII. Ansible Galaxy
A public hub for sharing Ansible roles.
IX. Error Handling and Debugging
-
Use
ignore_errors: yes
to skip errors. -
Use
register
anddebug
to capture and print variable data.
X. Best Practices
-
Use roles to organize reusable automation code.
-
Use Ansible Vault for secrets.
-
Maintain separate inventories for staging, production.
-
Always run playbooks in check mode before applying.
-
Use handlers for idempotent service restarts.
-
Test playbooks with Molecule or Vagrant.
-
Keep your playbooks idempotent (running twice = same result).
Comments
Post a Comment