---
  - name: Uninstall web server packages that may conflict with nginx
    apt:  package="{{ item }}" state=absent
    with_items:
     - apache2
     - lighttpd

  - name: Unhold nginx-related packages for updates
    dpkg_selections:
      name: "{{ item }}"
      selection: install
    with_items:
      - "nginx"
      - "nginx-common"
      - "nginx-extras"

  - name: Remove any older nginx
    apt:  package="{{ item }}" state=absent purge=yes
    with_items:
     - nginx
     - nginx-common
     - nginx-extras

  - name: Install nginx
    apt:  package="{{ item }}" state=latest
    with_items:
     - nginx
     - nginx-common
     - nginx-extras

  - name: Create nginx ssl directory
    file: path="{{ item }}" state=directory owner=root group=root mode=0744
    with_items:
     - "/etc/nginx/ssl"

  - name: Create self-signed SSL cert
    command: openssl req -new -nodes -x509 -subj "/C=US/ST=Texas/L=Austin/O=IT/CN=${ansible_fqdn}" -days 3650 -keyout /etc/nginx/ssl/server.key -out /etc/nginx/ssl/server.crt -extensions v3_ca
    args:
      creates: /etc/nginx/ssl/server.crt

  - name: Remove default nginx site symlink
    file:
      path: "/etc/nginx/sites-enabled/default"
      state: absent

  - name: Add nginx global config
    template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf force=yes backup=yes

  - name: Add app-specific nginx site
    template: src=default.j2 dest=/etc/nginx/sites-available/00-azuracast force=yes

  - name: Link app-specific nginx site
    file:
      path: "/etc/nginx/sites-enabled/00-azuracast"
      state: link
      src: "/etc/nginx/sites-available/00-azuracast"

  - name: Turn sendfile off on nginx for local development
    replace:
      dest: /etc/nginx/nginx.conf
      regexp: 'sendfile on;'
      replace: 'sendfile off;'
    when: app_env == "development"