---
- name: install nginx
  apt:
    name: nginx
    state: present
    update_cache: true

- name: start nginx
  systemd:
    name: nginx
    state: started

- name: Remove pre-existing sites-available and sites-enabled
  file:
    state: absent
    path: "{{ item }}"
  loop:
    - /etc/nginx/sites-available
    - /etc/nginx/sites-enabled

- name: Copy nginx config files
  copy:
    src: "{{ role_path }}/templates/{{ item.src }}"
    dest: "/etc/nginx/{{ item.dest }}"
    owner: root
    group: root
    mode: "{{ item.mode }}"
  loop:
    - { src: nginx.conf,        dest: nginx.conf,       mode: "0644" }
    - { src: mime.types,        dest: mime.types,       mode: "0644" }
    - { src: includes/,         dest: includes,         mode: "0755" }
    - { src: sites-available/,  dest: sites-available,  mode: "0755" }

- name: Make new sites-enabled
  file:
    path: /etc/nginx/sites-enabled
    state: directory
    owner: root
    group: root
    mode: "0755"

- name: enable debian.conf and mirror.conf
  # noqa deprecated-command-syntax
  command: 
    cmd: "ln -s ../sites-available/{{ item }} {{ item }}"
    chdir: "/etc/nginx/sites-enabled"
    creates: "/etc/nginx/sites-enabled/{{ item }}"
  loop:
    - debian.conf
    - mirror.conf

- name: restart and enable nginx
  systemd:
    name: nginx
    state: restarted
    enabled: yes
