# Setup File Playbook etc

sebelum ke pembuatan playbook kita akan membuat config ansible dan inventory didalam folder

1. buat folder challenge-1

```bash
mkdir -p challenge-1/

cd challenge-1/
```

2. buat file ansible.cfg dengan isi file sebagai berikut

sedikit penjelasan mengenai konfigurasi berikut:

* inventory menunjukan file inventory yang dipakai dalam hal ini kita memakai nama inventory untuk filenya
* remote user untuk ansible login kedalam vm yang akan dijalankan task yaitu menggunakan user student.
* ask\_become\_pass digunakan untuk saat user ansible menggunakan sudo atau become itu akan meminta password tergantung dari nilai booleannya true atau false jika true akan diminta

```yaml
[defaults]
inventory=inventory
remote_user=student
ask_become_pass = false
```

3. konfigurasi file inventory

```yaml
[managed]
pod-robiansya-managed1
```

4. konfigurasi playbook, buat playbook dengan nama file laravel.yaml

```yaml
- name: challenge laravel
  hosts: managed
  remote_user: student
  vars:
    required_package:
      - php8.0
      - npm
      - git
      - apache2
      - php-mbstring
      - mysql-server
      - php-bcmath
    db_socket: /var/run/mysqld/mysqld.sock 
#semua pacakge harus versi 8.0 disesuaikan dengan file composer.lock
  
  tasks:
    - name: Install required packages
      apt:
        name: "{{ required_package }}"
        state: latest
      become: true

    - name: Restart MariaDB
      service: 
        name: mariadb 
        state: restarted
      become: true

    - name: create directory
      file:
        path: /var/www/laravel
        state: directory
        owner: student
        group: student
        mode: '755'
      become: true

    - name: Git clone repository
      git:
        repo: https://github.com/kunal254/laravel-8-ecommerce.git
        dest: /var/www/laravel
        force: true

    - name: Install Laravel Dependencies
      composer:
        command: install
        working_dir: /var/www/laravel

    - name: Setup database
      mysql_db:
        name: beyond
        state: present
        login_unix_socket: "{{ db_socket }}"
        login_user: robi1
        login_password: "robi123"

    - name: Set DB_DATABASE in .env
      lineinfile: 
        path: /var/www/laravel/.env 
        regexp: '^DB_DATABASE=' 
        line: 'DB_DATABASE=beyond'

    - name: Set DB_USERNAME in .env
      lineinfile: 
        path: /var/www/laravel/.env 
        regexp: '^DB_USERNAME=' 
        line: 'DB_USERNAME=robi1'

    - name: Set DB_USERNAME in .env
      lineinfile: 
        path: /var/www/laravel/.env 
        regexp: '^DB_PASSWORD=' 
        line: 'DB_PASSWORD=robi123'

    - name: Generate application key
      shell: "php artisan key:generate"
      args:
        chdir: /var/www/laravel

    - name: storage link
      shell: "php artisan storage:link"
      args:
        chdir: /var/www/laravel

    - name: Run artisan migrate
      shell: "php artisan migrate --seed"
      args:
        chdir: /var/www/laravel
      notify: restart webserver

    - name: enable rewrite
      command: a2enmod rewrite
      become: true
      notify: restart webserver

  handlers:
    - name: restart webserver
      service: 

```

5. cek syntax dan jalankan playbook

```
ansible-playbook --syntax-check laravel.yaml

ansible-playbook laravel.yaml
```

6. lihat proses berjalannya task dari playbook, pastikan tidak ada yang failed.

```sh
student@pod-robiansya-controller:~/challenge-1$ ansible-playbook laravel.yaml 

PLAY [challenge laravel] ******************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [pod-robiansya-managed1]

TASK [Install required packages] **********************************************************************************************************************************************************************************
ok: [pod-robiansya-managed1]

TASK [Restart MariaDB] ********************************************************************************************************************************************************************************************
changed: [pod-robiansya-managed1]

TASK [create directory] *******************************************************************************************************************************************************************************************
ok: [pod-robiansya-managed1]

TASK [Git clone repository] ***************************************************************************************************************************************************************************************
changed: [pod-robiansya-managed1]

TASK [Install Laravel Dependencies] *******************************************************************************************************************************************************************************
ok: [pod-robiansya-managed1]

TASK [Setup database] *********************************************************************************************************************************************************************************************
ok: [pod-robiansya-managed1]

TASK [Set DB_DATABASE in .env] ************************************************************************************************************************************************************************************
ok: [pod-robiansya-managed1]

TASK [Set DB_USERNAME in .env] ************************************************************************************************************************************************************************************
changed: [pod-robiansya-managed1]

TASK [Set DB_USERNAME in .env] ************************************************************************************************************************************************************************************
changed: [pod-robiansya-managed1]

TASK [Generate application key] ***********************************************************************************************************************************************************************************
changed: [pod-robiansya-managed1]

TASK [storage link] ***********************************************************************************************************************************************************************************************
changed: [pod-robiansya-managed1]

TASK [Run artisan migrate] ****************************************************************************************************************************************************************************************
changed: [pod-robiansya-managed1]

RUNNING HANDLER [restart webserver] *******************************************************************************************************************************************************************************
changed: [pod-robiansya-managed1]

PLAY RECAP ********************************************************************************************************************************************************************************************************
pod-robiansya-managed1     : ok=14   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ansivel.vaniiw.my.id/setup-file-playbook-etc.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
