> ## Documentation Index
> Fetch the complete documentation index at: https://docs.skanesgalab.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Penggunaan Dasar Ansible Playbook

> Hands-on menyusun Playbook Ansible: struktur tasks, modul populer (apt, copy, template, service), dan deploy aplikasi web Nginx lengkap.

Jika perintah Ad-Hoc hanya digunakan untuk tugas satu kali eksekusi (seperti ping atau reboot), maka untuk tugas konfigurasi sistem yang berulang kita menggunakan **Ansible Playbook**.

Playbook adalah berkas teks berformat YAML yang mendeskripsikan kondisi akhir server yang kamu inginkan (*Desired State*).

***

## Hands-On: Menyusun Playbook Pertama

Kita akan membuat playbook untuk:

1. Memperbarui cache paket apt.
2. Menginstal web server Nginx.
3. Menyalin halaman HTML kustom.
4. Memastikan layanan Nginx aktif dan berjalan (*enabled & started*).

### 1. Buat Berkas File HTML

Buat file `index.html` di direktori kerjamu:

```html theme={null}
<!DOCTYPE html>
<html>
<head><title>Deployed via Ansible</title></head>
<body>
  <h1>Server Web Ini Berhasil Dikonfigurasi Otomatis via Ansible!</h1>
</body>
</html>
```

### 2. Buat Berkas Playbook (`deploy-web.yml`)

```yaml theme={null}
---
- name: Setup Web Server Nginx Otomatis
  hosts: webservers
  become: yes # Menjalankan perintah dengan hak akses sudo / root

  tasks:
    - name: Update apt cache dan pastikan Nginx terpasang
      apt:
        name: nginx
        state: present
        update_cache: yes

    - name: Salin file HTML kustom ke web root
      copy:
        src: ./index.html
        dest: /var/www/html/index.html
        owner: www-data
        group: www-data
        mode: '0644'
      notify: Restart Nginx Service

    - name: Pastikan service Nginx aktif saat boot
      service:
        name: nginx
        state: started
        enabled: yes

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

***

## Memahami Konsep Handlers

Di playbook di atas terdapat bagian `handlers` dan perintah `notify: Restart Nginx Service`.

* **Handler** adalah tugas khusus yang **hanya akan dijalankan jika ada perubahan nyata (*changed*)** pada task yang memicu notifikasi tersebut.
* Jika file `index.html` tidak mengalami perubahan sama sekali, Ansible tidak akan me-restart Nginx tanpa alasan. Inilah yang disebut prinsip efisiensi dan idempotensi.

***

## Menjalankan Playbook

Sebelum mengeksekusi ke server asli, kamu bisa melakukan uji coba simulasi (*Dry-Run*):

```bash theme={null}
# 1. Pengecekan sintaks YAML
ansible-playbook -i hosts.ini deploy-web.yml --syntax-check

# 2. Simulasi dry-run tanpa mengubah apapun di server (--check)
ansible-playbook -i hosts.ini deploy-web.yml --check

# 3. Eksekusi nyata ke seluruh server di grup webservers
ansible-playbook -i hosts.ini deploy-web.yml
```

### Membaca Hasil Output Eksekusi:

* **`ok` (Hijau):** Konfigurasi sudah sesuai, tidak ada perubahan yang perlu dilakukan.
* **`changed` (Kuning):** Ansible berhasil melakukan tindakan perubahan baru (misal menginstall paket baru).
* **`failed` (Merah):** Terjadi kesalahan (misal koneksi SSH terputus atau hak akses ditolak).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.