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

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

- name: Copy Custom Varnish ExecReload script (Debian <=9)
  copy:
    src: "reload-vcl.sh"
    dest: "/etc/varnish/reload-vcl.sh"
    mode: "0700"
    owner: root
    group: root
  when: ansible_distribution_major_version is version('9', '<=')
  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
  template:
    src: varnish.conf.j2
    dest: /etc/systemd/system/varnish.service.d/evolinux.conf
    force: yes
  notify:
    - reload systemd
    - restart varnish
  tags:
  - varnish

- 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'
  with_items:
    - varnishlog
    - varnishncsa
  tags:
  - varnish

- name: Copy Varnish configuration
  template:
    src: "{{ item }}"
    dest: /etc/varnish/default.vcl
    mode: "0644"
    force: yes
  with_first_found:
  - "templates/varnish/default.{{ inventory_hostname }}.vcl.j2"
  - "templates/varnish/default.{{ host_group }}.vcl.j2"
  - "templates/varnish/default.default.vcl.j2"
  - "default.vcl.j2"
  notify: reload varnish
  tags:
  - varnish

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

- 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

- include: munin.yml
