Ansible

๐ง Apa itu Ansible?
Ansible adalah automation tool berbasis open-source yang digunakan untuk:
- ๐ Provisioning (menyiapkan server dari awal)
- ๐ ๏ธ Configuration Management (atur dan jaga konfigurasi tetap konsisten)
- ๐ Application Deployment (deploy aplikasi dengan langkah otomatis)
- ๐งช Orchestration (sinkronisasi antar banyak server)
- ๐ Security Automation (atur firewall, user, SSH key, dll)
Dibuat oleh Red Hat, dan menggunakan SSH untuk berkomunikasi dengan target server tanpa perlu agen (agentless).
๐ Komponen dan Arsitektur Ansible
+-โโโโโ+ SSH +-โโโโโ+
, VM) |
| Control Node | ---------> | Managed Node |
| (Ansible host) | | (Server+----------------+ +----------------+
๐น Control Node:
- Tempat kamu menjalankan Ansible (biasanya laptop atau 1 VM khusus)
- Berisi: ansible, playbook, inventory, roles, dll
๐น Managed Node:
- Server/VM yang ingin kamu kelola
- Tidak perlu install apapun di sini
- Hanya butuh SSH + Python (versi 2.7 atau 3.x)
๐ Struktur Dasar Ansible
๐ธ Inventory File (hosts
)
[webservers]
192.168.1.10
192.168.1.11
[dbservers]
192.168.1.20 ansible_user=ubuntu ansible_port=22
๐ธ Playbook (site.yml
)
- name: Install nginx on webservers
hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
YAML-based dan declarative, bukan scripting imperatif.
โ๏ธ Cara Kerja Ansible
- Kamu jalankan perintah:bashCopyEditansible-playbook site.yml -i hosts
- Ansible:
- Membaca file inventory (daftar target)
- Masuk ke server via SSH
- Eksekusi task satu per satu
- Menjaga idempotensi (jalankan 2x = hasil tetap)
๐ฏ Contoh Task Praktis
โ Install NGINX di semua webserver
- name: Install nginx
apt:
name: nginx
state: present
๐ Copy SSH Key ke Semua Host
- name: Copy SSH key
"
authorized_key:
user: ubuntu
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}
๐งฑ Buat User Baru
- name: Add user 'admin'
user:
name: admin
shell: /bin/bash
groups: sudo
๐ฆ Ansible Modules (Built-in Perintah)
Modul | Fungsi |
---|---|
apt , yum , dnf | Instal paket |
copy , template | Salin file, pakai variabel |
user , group | Atur user dan grup |
service | Start/stop service |
file | Atur permission, owner |
command , shell | Jalankan perintah |
docker_container , docker_image | Kelola kontainer Docker |
proxmox (dari collection) | Kelola VM di Proxmox |
community.general.ntfy | Kirim notifikasi ke ntfy |
Kamu bisa install module tambahan via Ansible Galaxy.
๐ Idempotensi: Fitur Khas Ansible
Menjalankan task berulang tidak akan menyebabkan perubahan kalau sudah sesuai.
Contoh:
- Playbook install nginx, dijalankan 10x โ tetap hanya install sekali
- Tidak seperti shell script yang bisa bikin duplikasi
๐ Contoh Real-World Use Case (di homelab kamu)
Tujuan | Task |
---|---|
Provision VM di Proxmox | Pakai community.proxmox collection |
Install Docker di semua node | Pakai playbook apt , docker_container |
Deploy Prometheus stack | Pull Docker image, copy prometheus.yml , run stack |
Atur firewall & ssh | Tambah rule UFW + copy SSH key |
Backup config ke server | Pakai fetch atau synchronize |
Kirim notifikasi ke Telegram atau ntfy | Pakai uri atau ntfy module |
๐ฆ Tools Tambahan
Tool | Fungsi |
---|---|
ansible-galaxy | Download roles dan collections dari komunitas |
ansible-vault | Enkripsi file playbook / var sensitif |
ansible-pull | Jalankan playbook dari server Git |
AWX / Ansible Tower | UI web untuk Ansible, dashboard, job scheduling |
๐ฅ Contoh Skrip Otomasi Singkat
ansible all -i hosts -m ping
ansible-playbook -i hosts update-server.yml
ansible webservers -m apt -a "name=nginx state=latest"
โ Kelebihan Ansible
Kelebihan | Penjelasan |
---|---|
โ Tidak butuh agen | Cukup SSH saja |
โ Mudah dipelajari | Format YAML, tidak perlu scripting |
โ Idempotent | Aman dijalankan berulang |
โ Support banyak modul | Termasuk Docker, Proxmox, cloud, dsb |
โ Sangat cocok untuk homelab & production |
๐ง Penutup
Ansible sangat cocok untuk kamu yang:
- ๐ Ingin mengotomasi provisioning VM, container, dan server
- ๐ฆ Ingin deploy stack Prometheus, Grafana, NGINX dengan cepat
- ๐งโโ๏ธ Ingin konfigurasi konsisten dan bebas human error
- ๐ง Ingin belajar praktik DevOps dengan tool yang real