---
- name: Install NGINX
  become: yes
  apt:
    name: nginx

- name: Default Configuration
  become: yes
  template:
    src: default.j2
    dest: /etc/nginx/sites-available/default

- name: Link Default NGINX Configuration
  become: yes
  file:
    src: "/etc/nginx/sites-available/default"
    dest: "/etc/nginx/sites-enabled/default"
    state: link

- name: Configure Reverse Proxies
  become: yes
  template:
    src: reverse_proxy.conf.j2
    dest: /etc/nginx/sites-available/{{item.server_name}}.conf
  loop: "{{ reverse_services }}"

- name: Link NGINX Reverse Proxies
  become: yes
  file:
    src: "/etc/nginx/sites-available/{{item.server_name}}.conf"
    dest: "/etc/nginx/sites-enabled/{{item.server_name}}.conf"
    state: link
  loop: "{{ reverse_services }}"

- name: Configure FPM Services
  become: yes
  template:
    src: fpm_service.conf.j2
    dest: /etc/nginx/sites-available/{{item.server_name}}.conf
  loop: "{{ fpm_services }}"


- name: Link NGINX FPM Services
  become: yes
  file:
    src: "/etc/nginx/sites-available/{{item.server_name}}.conf"
    dest: "/etc/nginx/sites-enabled/{{item.server_name}}.conf"
    state: link
  loop: "{{ fpm_services }}"  

- name: Make sure NGINX Service is running
  become: yes
  service:
    name: nginx
    state: restarted
    enabled: yes

- name: Run Certbot if needed
  include: certbot.yml
  when: with_certbot | bool
