How to use Ansible for configuration management

· Category: DevOps & CI/CD

How to use Ansible for configuration management

Introduction

Ansible is an agentless automation tool that uses SSH to configure servers, deploy applications, and orchestrate tasks. It uses YAML playbooks and Jinja2 templating, making it approachable for both developers and operators.

Basic Playbook

---
- hosts: webservers
  become: yes
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

    - name: Start nginx
      service:
        name: nginx
        state: started
        enabled: yes

Roles

Organize playbooks into reusable roles:

ansible-galaxy init roles/webserver

Roles contain tasks/, handlers/, templates/, and vars/ directories, promoting reuse across environments.

Inventory and Variables

Define hosts in an inventory file and override variables per group. Ansible Vault encrypts sensitive data like passwords and API keys.

For container-based alternatives, how to use Docker Compose covers local orchestration. For CI integration, how to implement CI/CD with Jenkins pairs well with Ansible-driven deployments.