Skip to content
Codeloom
DevOps

Ansible Automation Basics

Automate server configuration with Ansible — inventory, playbooks, modules, roles, and idempotent infrastructure management.

·3 min read · By Codeloom
Beginner 10 min read

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.