Ansible Fundamentals (Configuration Management)
Learn Ansible’s model: inventory, playbooks, modules, variables, and idempotency. Includes a working ping example and config management pattern.
Ansible is automation for configuration management and deployments.
Core concepts:
- Inventory: list of hosts to manage
- Playbook: YAML describing desired tasks/state
- Modules: the units that actually do work (copy, file, service, template, etc.)
- Idempotency: re-running doesn’t “keep changing” things unnecessarily
Theory first: infrastructure consistency at scale
Ansible’s real value is not just remote command execution—it is consistency. You define expected system state once and apply it repeatedly across environments with predictable outcomes.
Idempotency is the foundation of that consistency: safe re-runs make automation reliable during drift correction, incident recovery, and continuous delivery.
Learning outcomes
By the end, you can:
- create an inventory file
- run a simple playbook
- understand variables and handlers
1) Inventory
inventory.ini:
[web]
web1.example.com
web2.example.com
[db]
db1.example.com
2) Ping test (does Ansible reach hosts?)
site.yml:
---
- name: Connectivity test
hosts: web
gather_facts: false
tasks:
- name: ping
ansible.builtin.ping:
Run:
ansible-playbook -i inventory.ini site.yml
3) Example: install and start a service
---
- name: Ensure nginx is installed and running
hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Start nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
4) Variables and templates (pattern)
Variables let you reuse playbooks across environments.
A simple variable file vars.yml:
nginx_port: 8080
Then use in tasks/templates.
5) Handlers (run only when something changes)
Pattern:
- notify handler when config changes
- name: Deploy nginx config
ansible.builtin.template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: Restart nginx
handlers:
- name: Restart nginx
ansible.builtin.service:
name: nginx
state: restarted
Next steps
Next tutorials:
- Ansible roles (reusable components)
- idempotent config patterns
- Terraform + Ansible CI/CD orchestration