---
- name: Install Varnish
  apt:
    name: varnish
    state: present
  tags:
    - varnish

- name: Remove default varnish configuration files
  file:
    path: "{{ item }}"
    state: absent
  loop:
    - /etc/default/varnish
    - /etc/default/varnishncsa
    - /etc/default/varnishlog
  notify: reload varnish
  tags:
    - varnish
    - config

- name: Copy Custom Varnish ExecReload script (Debian <10)
  template:
    src: "reload-vcl.sh.j2"
    dest: "/etc/varnish/reload-vcl.sh"
    mode: "0700"
    owner: root
    group: root
  when: ansible_distribution_major_version is version('10', '<')
  notify: reload varnish
  tags:
    - varnish

- name: Create a system config directory for systemd overrides
  file:
    path: /etc/systemd/system/varnish.service.d
    state: directory
  tags:
    - varnish

- name: Override Varnish systemd unit (Stretch and before)
  template:
    src: varnish.conf.jessie.j2
    dest: /etc/systemd/system/varnish.service.d/evolinux.conf
    force: yes
  when: ansible_distribution_major_version is version('10', '<')
  notify:
    - reload systemd
    - restart varnish
  tags:
    - varnish
    - config
    - update-config

# TODO: verify if it's still necessary for Debian 11
- name: Override Varnish systemd unit (Buster and later)
  template:
    src: varnish.conf.buster.j2
    dest: /etc/systemd/system/varnish.service.d/evolinux.conf
    force: yes
  when: ansible_distribution_major_version is version('10', '>=')
  notify:
    - reload systemd
    - restart varnish
  tags:
    - varnish
    - config
    - update-config

- name: Patch logrotate conf
  replace:
    name: /etc/logrotate.d/varnish
    regexp: '^(\s+)(/usr/sbin/invoke-rc.d {{item}}.*)'
    replace: '\1systemctl -q is-active {{item}} && \2'
  loop:
    - varnishlog
    - varnishncsa
  tags:
    - varnish
    - logrotate

- name: Copy Varnish configuration
  template:
    src: "{{ item }}"
    dest: "{{ varnish_config_file }}"
    mode: "0644"
    force: yes
  loop: "{{ query('first_found', templates) }}"
  vars:
    templates:
      - "templates/varnish/varnish.{{ inventory_hostname }}.vcl.j2"
      - "templates/varnish/default.{{ inventory_hostname }}.vcl.j2"
      - "templates/varnish/varnish.{{ host_group | default('all') }}.vcl.j2"
      - "templates/varnish/default.{{ host_group | default('all') }}.vcl.j2"
      - "templates/varnish/varnish.default.vcl.j2"
      - "templates/varnish/default.default.vcl.j2"
      - "templates/varnish.vcl.j2"
      - "templates/default.vcl.j2"
  notify: reload varnish
  tags:
    - varnish
    - config
    - update-config

- name: Create Varnish config dir
  file:
    path: /etc/varnish/conf.d
    state: directory
    mode: "0755"
  tags:
    - varnish
    - config
    - update-config

- name: Copy included Varnish config
  template:
    src: "{{ item }}"
    dest: /etc/varnish/conf.d/
    force: yes
    mode: "0644"
  with_fileglob:
    - "templates/varnish/conf.d/*.vcl"
  notify: reload varnish
  tags:
    - varnish
    - config
    - update-config

- include: munin.yml
