Skip to main content
InfrastructureIntermediate

Ansible for DevOps: Automating Server Configuration

Get started with Ansible for server automation — inventory, playbooks, roles, common modules, and best practices for DevOps workflows.

N
Neeraj Jha
·Updated September 11, 2026·4 min read
Ansible for DevOps: Automating Server Configuration

Ansible is an agentless automation tool that uses SSH to configure servers, deploy applications, and orchestrate workflows. No daemons, no agents — just Python and SSH.

Why Ansible?

  • Agentless — nothing to install on managed nodes
  • Idempotent — running a playbook twice produces the same result
  • Declarative — describe the desired state, not the steps
  • Simple — YAML playbooks are readable by anyone

Installation

bash
# macOS
brew install ansible

# Ubuntu / Debian
sudo apt update
sudo apt install ansible

# Verify
ansible --version

Inventory

The inventory defines which servers Ansible manages. Create an inventory.ini:

ini
[webservers]
web1.example.com ansible_user=deploy
web2.example.com ansible_user=deploy

[databases]
db1.example.com ansible_user=deploy

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Test connectivity:

bash
ansible all -i inventory.ini -m ping

Your First Playbook

Create setup-webserver.yml:

yaml
---
- name: Configure web servers
  hosts: webservers
  become: yes

  vars:
    app_port: 3000
    node_version: "20"

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
        cache_valid_time: 3600

    - name: Install required packages
      apt:
        name:
          - nginx
          - curl
          - git
          - ufw
        state: present

    - name: Enable UFW firewall
      ufw:
        state: enabled
        policy: deny

    - name: Allow SSH
      ufw:
        rule: allow
        name: OpenSSH

    - name: Allow HTTP and HTTPS
      ufw:
        rule: allow
        port: "{{ item }}"
        proto: tcp
      loop:
        - "80"
        - "443"

    - name: Copy Nginx config
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/sites-available/default
      notify: Restart Nginx

  handlers:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted

Run it:

bash
ansible-playbook -i inventory.ini setup-webserver.yml

Roles

Roles organize playbooks into reusable components:

roles/
  nginx/
    tasks/main.yml
    templates/nginx.conf.j2
    handlers/main.yml
    defaults/main.yml
  nodejs/
    tasks/main.yml
    defaults/main.yml
  postgresql/
    tasks/main.yml
    templates/pg_hba.conf.j2
    handlers/main.yml

Use roles in a playbook:

yaml
---
- name: Full server setup
  hosts: webservers
  become: yes
  roles:
    - nginx
    - nodejs

Common Modules

ModulePurposeExample
apt / yumPackage managementapt: name=nginx state=present
copyCopy filescopy: src=app.conf dest=/etc/app.conf
templateJinja2 templatestemplate: src=nginx.j2 dest=/etc/nginx/nginx.conf
serviceManage servicesservice: name=nginx state=started enabled=yes
userManage usersuser: name=deploy shell=/bin/bash
fileFile/directory opsfile: path=/data state=directory mode=0755
gitClone repositoriesgit: repo=https://github.com/... dest=/opt/app
docker_containerManage containersdocker_container: name=app image=myapp:latest

Ansible Vault

Encrypt sensitive variables:

bash
# Create encrypted file
ansible-vault create secrets.yml

# Edit encrypted file
ansible-vault edit secrets.yml

# Run playbook with vault
ansible-playbook -i inventory.ini site.yml --ask-vault-pass

Best Practices

  • Use --check mode (dry run) before applying changes
  • Group variables in group_vars/ and host-specific ones in host_vars/
  • Always use become: yes explicitly rather than running Ansible as root
  • Tag tasks so you can run subsets: ansible-playbook site.yml --tags "nginx"
  • Test playbooks with Molecule for role unit testing

Ansible makes server configuration repeatable and version-controlled. Combined with Git, it brings the same rigor to infrastructure that developers have for application code.

Tagged with

Enjoyed this article?

Get more DevOps insights delivered to your inbox.

Get new posts by email

Subscribe to get an email when a new blog post is published. Skip anytime.

No spam, unsubscribe anytime.

N

Written by

Neeraj Jha

Platform administrator and lead writer.

View all posts

Discussion

0 comments

Sign in to join the conversation.

Be the first to comment

Start a conversation about this post

Share: