Ansible Architecture
Understanding the architecture of Ansible and how its components interact to enable automation.
Architecture of Ansible
Ansible is designed with simplicity and modularity in mind. Its architecture revolves around a few key components that work together to automate tasks across systems. The two most essential concepts you'll work with are the Inventory
and the Playbook
.
Inventory
The Inventory tells Ansible where to apply tasks. It's a list of target machines (hosts) that can be grouped and referenced in playbooks.
Static Inventory
The most commons formats are INI
and Yaml
(as they are built-in), but there are many more formats and sources that are possible.
[webservers]
192.168.1.10
192.168.1.11
[dbservers]
192.168.1.20
You can group machines by role, function, or environment (it is up to you).
Dynamic Inventory
For more complex infrastructure (like cloud environments), Ansible can generate inventory dynamically via scripts or plugins (e.g., AWS EC2, Docker, Kubernetes). I will explain later how to generate an inventory using HashiCorp Vault and Ansible.
Playbook
A playbook is a YAML
file that defines what tasks to perform on which hosts. It's the central part of how Ansible automates work.
Each Playbook is made of plays and has:
hosts
: the inventory group to targettasks
: a sequence of actions to runmodules
: the tools Ansible uses to execute tasks (e.g.,apt
,copy
,service
)
Playbook Structure
Basic structure of a playbook:
---
- name: Example Play
hosts: All
become: true
tasks:
- name: Install a package
apt:
name:
- git
state: present
name
: Descibre the Playhosts
: Targets the group from your inventorybecome
: Allows you to run tasks asroot
tasks
: A list of task definitionsapt
: This is a module used for Debian-based package management
Modules in Playbooks
Modules are what Ansible uses to actually perform actions. Some common modules include:
apt
,yum
: package installationcopy
,template
: file operationsservice
: start/stop servicesshell
,command
: run commands (less recommended)
Create your first playbook
Let’s build a basic playbook to install and configure NGINX on web servers.
Save this file as install_nginx.yml
:
---
- name: Install NGINX on web servers
hosts: webservers
become: true
tasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install nginx
apt:
name: nginx
state: present
- name: Ensure nginx is running
service:
name: nginx
state: started
enabled: yes
Congrats, you made your first playbook
!
Now let's run it:
ansible-playbook -i inventory.yml install_nginx.yml
Where :
-i inventory.yml
: specifies your inventory file.install_nginx.yml
: is the playbook file.
Next Steps
Now that you understand Ansible's basic architecture, you are ready to dive deeper. The upcoming sessions will cover more advanced concepts like variables, roles, templates, and Dynamic Inventory.