Catalog / Ansible Cheatsheet
Ansible Cheatsheet
A concise reference guide for Ansible, covering essential concepts, modules, and commands for effective infrastructure automation.
Core Concepts
Key Components
Control Node |
The machine where Ansible is installed and from which plays are executed. |
Managed Nodes |
The servers or devices being managed by Ansible. |
Inventory |
A list of managed nodes, organized into groups. Can be a simple text file or a dynamic inventory script. |
Modules |
Reusable, standalone scripts that Ansible uses to perform tasks on managed nodes. Examples: |
Tasks |
A single unit of work defined in a playbook, calling a specific module with specific arguments. |
Playbooks |
YAML files that define a set of tasks to be executed on managed nodes. Orchestrates the configuration management process. |
Idempotency
Ansible modules are designed to be idempotent, meaning they only make changes if necessary to bring the system to the desired state. This prevents unintended side effects from repeated playbook runs. |
Ansible Configuration Files
|
Main configuration file for Ansible. Sets defaults for inventory location, module paths, etc. Can be located in |
Playbooks & Syntax
Basic Playbook Structure
|
|
Common Modules
|
Manages apt packages (install, remove, update). |
|
Manages yum packages (install, remove, update). |
|
Copies files to managed nodes. |
|
Manages file attributes (permissions, ownership, symlinks). |
|
Manages services (start, stop, restart, reload). |
|
Manages user accounts. |
|
Templates a file out to a remote system. |
Variables
Variables can be defined in inventory files, playbook files, or as command-line arguments. Example:
|
Commands & Usage
Common Commands
|
Displays the Ansible version. |
|
Executes an Ansible playbook. |
|
Runs the |
|
Installs an Ansible role from Ansible Galaxy. |
|
Encrypts a file using Ansible Vault. |
|
Decrypts a file using Ansible Vault. |
Inventory Management
Ansible uses an inventory file to define the managed nodes. The default location is Example:
|
Ad-Hoc Commands
Ad-hoc commands are a quick way to execute single tasks on managed nodes without writing a full playbook. Example:
This command executes the |
Advanced Features
Roles
Roles are a way to organize and reuse Ansible content. A role typically includes tasks, variables, handlers, and templates. Directory Structure:
|
Handlers
Handlers are tasks that are only executed when notified by another task. This is useful for restarting services after a configuration change. Example:
|
Loops
Loops allow you to repeat a task multiple times with different values. Example:
|