Ansible Automation Basics
Automate server configuration with Ansible — inventory, playbooks, modules, roles, and idempotent infrastructure management.
What you'll learn
- ✓What Ansible is and how agentless automation works
- ✓Inventory files and host groups
- ✓Writing playbooks with tasks and handlers
- ✓Using roles for reusable automation
Prerequisites
- •Basic Linux command line skills
- •SSH access to at least one remote server
Ansible automates server configuration, application deployment, and orchestration. It is agentless — no software to install on target machines. It connects over SSH and runs tasks you define in YAML playbooks.
Install
# macOS
brew install ansible
# Ubuntu/Debian
sudo apt install ansible
# pip
pip install ansible
Inventory
Define the machines you manage in an inventory file.
# inventory.ini
[webservers]
web1.example.com
web2.example.com
[databases]
db1.example.com
[all:vars]
ansible_user=deploy
ansible_python_interpreter=/usr/bin/python3
Testing connectivity
ansible all -i inventory.ini -m ping
Ad-hoc commands
Run a single command across all hosts:
ansible webservers -i inventory.ini -m shell -a "uptime"
ansible databases -i inventory.ini -m apt -a "name=postgresql state=present" --become
Playbooks
A playbook is a YAML file with a list of plays. Each play targets a group of hosts and runs a sequence of tasks.
# setup-web.yml
---
- name: Configure web servers
hosts: webservers
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
update_cache: yes
- name: Copy Nginx config
copy:
src: files/nginx.conf
dest: /etc/nginx/nginx.conf
notify: restart nginx
- name: Ensure Nginx is running
service:
name: nginx
state: started
enabled: yes
handlers:
- name: restart nginx
service:
name: nginx
state: restarted
Run it:
ansible-playbook -i inventory.ini setup-web.yml
Handlers
Handlers run only when notified, and only once at the end of all tasks. Use them for service restarts.
Variables
- name: Deploy app
hosts: webservers
vars:
app_port: 8080
app_user: deploy
app_dir: /opt/myapp
tasks:
- name: Create app directory
file:
path: "{{ app_dir }}"
state: directory
owner: "{{ app_user }}"
- name: Create systemd service
template:
src: templates/myapp.service.j2
dest: /etc/systemd/system/myapp.service
Jinja2 templates
# templates/myapp.service.j2
[Unit]
Description=My Application
[Service]
User={{ app_user }}
WorkingDirectory={{ app_dir }}
ExecStart={{ app_dir }}/bin/start --port {{ app_port }}
Restart=always
[Install]
WantedBy=multi-user.target
Conditionals and loops
tasks:
- name: Install packages
apt:
name: "{{ item }}"
state: present
loop:
- nginx
- curl
- git
- name: Start service only on Ubuntu
service:
name: nginx
state: started
when: ansible_distribution == "Ubuntu"
Roles
Roles organize playbooks into reusable components.
roles/
nginx/
tasks/
main.yml
handlers/
main.yml
templates/
nginx.conf.j2
files/
defaults/
main.yml
vars/
main.yml
Use a role in a playbook:
- name: Setup servers
hosts: webservers
become: yes
roles:
- nginx
- app
- monitoring
Creating a role scaffold
ansible-galaxy init roles/nginx
Idempotency
Every Ansible module is designed to be idempotent — running a playbook twice produces the same result. Ansible reports:
- ok: already in desired state, no change made
- changed: state was modified
- failed: task failed
Vault for secrets
Encrypt sensitive variables:
ansible-vault create vars/secrets.yml
ansible-vault edit vars/secrets.yml
ansible-playbook playbook.yml --ask-vault-pass
Summary
Ansible automates infrastructure with agentless, idempotent playbooks. Define your inventory, write tasks in YAML, use roles for organization, and vault for secrets. Run it once or a hundred times — the result is the same. Start by automating your most repetitive server setup task.
Related articles
- DevOps Ansible Playbooks for Configuration Management
A hands-on guide to writing Ansible playbooks that configure servers, deploy apps, and enforce desired state across your fleet.
- DevOps Ansible vs Terraform: Configuration vs Infrastructure
Compare Ansible and Terraform for DevOps automation. Learn when to use each tool, how they differ in approach, and how to combine them effectively.
- DevOps Configuration Management: Ansible, Chef, and Puppet
Compare Ansible, Chef, and Puppet for configuration management. Learn how each tool works with practical examples for server provisioning and application deployment.
- DevOps Getting Started with Terraform
Learn Terraform from scratch — providers, resources, variables, state, modules, and deploying your first infrastructure as code.