---
- name: Check that the somefile.conf exists
  stat:
    path: /etc/nginx/sites-available/{{ hostname }}.conf
  register: nginx_file

- name: Conf Jitsi MultiDomain - Duplication de la conf nginx
  copy:
    src: /etc/nginx/sites-available/{{ inventory_hostname }}.conf
    dest: /etc/nginx/sites-available/{{ hostname }}.conf
    remote_src: true
    mode: 0644
  when: not nginx_file.stat.exists

- name: Conf Jitsi MultiDomain - Change le ServerName des conf nginx
  replace:
    path: /etc/nginx/sites-available/{{ hostname }}.conf
    regexp: "[^?]server_name {{ inventory_hostname }}"
    replace: "server_name {{ hostname }}"

- name: Conf Jitsi MultiDomain - surcharge le header proxy
  replace:
    path: /etc/nginx/sites-available/{{ hostname }}.conf
    regexp: '[^?]\$http_host'
    replace: ' {{ inventory_hostname }}'

- name: Conf Jitsi MultiDomain - suppression du paramètre
    server_names_hash_bucket_size car génère une erreur si en doublon
  lineinfile:
    path: /etc/nginx/sites-available/{{ hostname }}.conf
    state: absent
    regexp: '^server_names_hash_bucket_size'

- name: Activation des nouvelles conf nginx
  file:
    src: /etc/nginx/sites-available/{{ hostname }}.conf
    dest: /etc/nginx/sites-enabled/{{ hostname }}.conf
    state: link
  notify:
    - restart jitsi-videobridge2
    - restart nginx
