Ansible Complete Guide — Configuration Management

Sanjeev SharmaSanjeev Sharma
1 min read

Advertisement

Ansible Complete Guide — Configuration Management

Ansible automates infrastructure configuration and deployment without agents.

Introduction

Ansible uses SSH for agentless configuration management with simple YAML playbooks.

Inventory and Setup

# inventory.ini
[webservers]
web1.example.com
web2.example.com

[databases]
db.example.com
# Test connectivity
ansible all -i inventory.ini -m ping

# Run ad-hoc command
ansible webservers -i inventory.ini -m command -a "uptime"

Playbooks

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

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

    - name: Deploy app
      copy:
        src: app.conf
        dest: /etc/nginx/sites-available/default
      notify: restart nginx

  handlers:
    - name: restart nginx
      service:
        name: nginx
        state: restarted

Roles

---
# roles/webserver/tasks/main.yml
- name: Install packages
  apt:
    name: "{{ item }}"
    state: present
  loop:
    - nginx
    - curl
    - git

- name: Start services
  service:
    name: "{{ item }}"
    enabled: yes
    state: started
  loop:
    - nginx

FAQ

Q: Does Ansible require agents? A: No. Uses SSH for agentless automation.

Q: Should I use Ansible or Terraform? A: Terraform for infrastructure provisioning; Ansible for configuration management.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro