“Automate the boring stuff — before it becomes confusing stuff.”
Whether you’re new to DevOps, managing cloud servers, or just tired of logging in and running the same five shell commands everywhere, Ansible is a game-changer for simplicity, repeatability, and speed.
1. What is Ansible & Why Choose It?
Ansible is an agentless automation tool for configuration, deployment, and orchestration. It’s especially friendly for beginners and teams who want human-readable, version-controlled infrastructure. With Ansible, you don’t need to install or maintain special agents on the servers you manage; it works over standard SSH connections and uses simple YAML files to describe the desired state. This makes it easy to get started and to collaborate, since you can store and review infrastructure changes just like application code. Whether you’re automating routine server setup, cloud provisioning, or multi-tier application deployments, Ansible’s straightforward approach helps you move faster, avoid manual errors, and embrace modern DevOps practices.
Why I recommend Ansible:
- No agent needed: Only SSH and Python required on managed nodes.
- YAML all the way: Configurations you can actually read and audit.
- Idempotent: If you run a playbook 10 times, it only changes what needs changing.
- Cross-platform: Works for Windows, Linux, cloud, and on-prem.
- Vibrant module ecosystem: Thousands of built-in integrations.
2. The Architecture — How Ansible Works
Visualize Ansible as:
Your Laptop (control node)
|
+--(SSH)--> Managed Nodes (any Linux/Unix, some Windows)
|
+-- Python executes quick tasks
Components:
- Control Node: Where you install and run Ansible.
- Managed Nodes: Target systems (no agent installed).
- Inventory: Tells Ansible which hosts/groups to target.
- Modules: Do the work (install packages, copy files, restart services).
- Playbooks: YAML scripts describing steps you want run.
3. Installation & Setup
Installition of Ansbile is realtivey straightforward.
On Linux
pip install ansible
On Mac
brew install ansible
On Windows
- Use WSL2 and run the same pip command.
Check your install to make sure all is working well
ansible --version
Optional: For best practices, install linting tool to keep your yaml properly formatted:
4. Inventory — Who are We Managing?
Inventory files in Ansible define the hosts and groups of hosts you want to manage. The inventory tells Ansible who to connect to and how to organize them (e.g., by role, environment, or location). You can keep it simple with a flat list, or create groups for better targeting and reuse in playbooks.
- Use square brackets to define groups like [web] or [db].
- You can nest groups with :children, creating higher-level groupings (like [production] including both web and db servers).
- Each host can live in multiple groups, making it flexible to manage servers by multiple criteria.
By grouping hosts, you can run tasks against specific sets (e.g., all web servers or only database servers), and tailor configurations playbooks based on the target group.
Example: inventory.ini
[web]
web1.example.com
web2.example.com
[db]
db1.example.com
Test connectivity:
ansible -i inventory.ini all -m ping
5. First Ad-Hoc Commands — Try It Live
Want to install something or check a service status? You don’t even need to write a script yet—Ansible’s ad-hoc commands let you automate tasks across one, many, or all servers right from the command line.
# Install nginx webserver
ansible -i inventory.ini web -m apt -a "name=nginx state=present" --become
# Start nginx service
ansible -i inventory.ini web -m service -a "name=nginx state=started" --become
-i: Specifies your inventory file, so Ansible knows which servers to target.-m: The module to use (apt to manage packages, service for services, etc.).-a: Arguments for the module, like the name and desired state of the package or service.--become: Runs the command with sudo privileges, necessary for system-level changes.
Ad-hoc mode is a great way to quickly troubleshoot, check status, roll out a quick fix, or perform administrative tasks before you’ve written your first playbook!
6. Playbooks — The Real Magic
Playbooks are at the heart of Ansible, letting you automate complex workflows with readable YAML files. A playbook is where you codify the desired state of your infrastructure: which packages should be installed, what services should be running, and what configuration files should exist.
Instead of running individual commands, you define an ordered series of tasks for Ansible to execute, applying the same logic every time—no manual steps required. Each playbook can target one group of hosts or many, and can handle everything from basic server setup to multi-tier application deployments.
---
- name: Configure web servers
hosts: web
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx
service:
name: nginx
state: started
- The top-level keys (like name, hosts, become, and tasks) define what this playbook does.
- Under tasks, each item is a step Ansible will run for the web hosts.
- The apt module ensures Nginx is installed; the service module makes sure it is started.
- The become: tells Ansible to perform the tasks with elevated (sudo) privileges.
How to run a playbook:
ansible-playbook -i inventory.ini site.yml
Playbooks make your infrastructure reliable and repeatable—they’re a core best practice for real-world automation and form the foundation of collaborative DevOps workflows.
7. Idempotence & Popular Modules
Idempotence is a key principle in Ansible and modern configuration management. It means that when you run an Ansible playbook (or task) multiple times, it will only make changes if something is actually different from the desired state. If the system already matches what you described—for example, if a package is already installed or a user already exists—Ansible won’t do anything further for that task.
In other words: Idempotence means Ansible checks the state first, and only takes action if necessary, ensuring your automation is safe to run over and over without causing unintended side effects. This makes infrastructure predictable, reliable, and safe to apply even as your environment or team grows.
Common modules:
apt,yum: Install packagesservice: Manage servicescopy,template: Manage filesuser: Manage users
Example:
- name: Ensure deploy user exists
user:
name: deploy
state: present
8. Variables, Facts, & Templates
Variables help you keep things DRY:
vars:
app_port: 8080
tasks:
- name: Template config
template:
src: app.conf.j2
dest: /etc/app.conf
- Facts: Gathered automatically, e.g. OS, IPs, etc (
{{ ansible_distribution }}). - Templates: Use Jinja2. Example
app.conf.j2:
PORT={{ app_port }}
ENV={{ ansible_env.HOME }}
9. Roles — Make Playbooks Reusable and Organized
Organize tasks, templates, files, and variables by “role” — makes big projects manageable.
roles/
webserver/
tasks/
templates/
files/
vars/
Example:
- hosts: web
roles:
- webserver
10. Hands-On Lab: Install & Start Apache
Let’s try it!
1. Create inventory.ini:
[web]
yourweb1.example.com
yourweb2.example.com
2. Write site.yml:
- hosts: web
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache
service:
name: apache2
state: started
3. Run:
ansible-playbook -i inventory.ini site.yml
Final Thoughts
- If you can read a TODO list, you can read Ansible YAML.
- Start by automating something small, and work your way up.
- Use inventories to target many servers.
- Playbooks and roles scale up as your environment grows.
Are you already using Ansible, or just starting out? Share your favorite tips or questions!