---

- name: Create nginx conf dir
  file: path=/etc/nginx/{{ item }} state=directory
  loop:
    - ansible_location.d
    - ansible_conf.d
    - ansible_modules.d
  tags: web

- name: Check if lua module is installed
  stat: path=/usr/lib64/nginx/modules/ndk_http_module.so
  register: llng_mod_lua
  tags: web

- name: Deploy module conf
  copy: content={{ item.content }} dest=/etc/nginx/ansible_modules.d/{{ item.dest }}
  loop:
    - content: 'load_module "/usr/lib64/nginx/modules/ndk_http_module.so";'
      dest: 09-ndk.conf
    - content: 'load_module "/usr/lib64/nginx/modules/ngx_http_lua_module.so";'
      dest: 11-lua.conf
  when: llng_mod_lua.stat.exists
  notify: reload nginx
  tags: web

- name: Remove module conf
  file: path=/etc/nginx/ansible_modules.d/{{ item }} state=absent
  loop:
    - 09-ndk.conf
    - 11-lua.conf
  when: not llng_mod_lua.stat.exists
  notify: reload nginx
  tags: web

- name: Deploy handler's nginx configuration
  template: src={{ item.src }} dest={{ item.dest }}
  loop:
    - src: nginx_handler.conf.j2
      dest: /etc/nginx/ansible_location.d/10-llng_handler.conf
    - src: llng_headers.inc.j2
      dest: /etc/nginx/ansible_conf.d/llng_headers.inc
  when: llng_handler
  notify: reload nginx
  tags: web

- name: Remove handler's nginx configuration
  file: path={{ item }} state=absent
  loop:
    - /etc/nginx/ansible_location.d/10-llng_handler.conf
    - /etc/nginx/ansible_conf.d/llng_headers.inc
  when: not llng_handler
  notify: reload nginx
  tags: web

- name: Deploy portal's nginx configuration
  template: src=nginx_portal.conf.j2 dest=/etc/nginx/ansible_conf.d/22-llng_portal.conf
  when: llng_portal
  notify: reload nginx
  tags: web

- name: Remove portal's nginx configuration
  file: path=/etc/nginx/ansible_conf.d/22-llng_portal.conf state=absent
  when: not llng_portal
  notify: reload nginx
  tags: web

- name: Deploy manager's nginx configuration
  template: src=nginx_manager.conf.j2 dest=/etc/nginx/ansible_conf.d/23-llng_manager.conf
  when: llng_manager and llng_server == 'nginx'
  notify: reload nginx
  tags: web

- name: Remove manager's nginx configuration
  file: path=/etc/nginx/ansible_conf.d/23-llng_manager.conf state=absent
  when: not llng_manager and llng_server == 'nginx'
  notify: reload nginx
  tags: web
