---
- name: MEDIACACHE
  hosts: mediacache
  tags: mediacache
  roles:
    - mediacache
  tasks:
    - name: Getting the IP to trust in term of securelink
      ansible.builtin.set_fact:
        securelink_ip: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"

    - name: authorize mediacache on mediaserver
      notify: restart nginx on mediaservers
      ansible.builtin.lineinfile:
        path: /etc/nginx/conf.d/mediaserver-securelink.conf
        line: "{{'\t'}}{{ securelink_ip }} 1;" # noqa no-tabs jinja[spacing]
        insertafter: ^geo
      delegate_to: "{{ item }}"
      delegate_facts: true
      loop: "{{ groups['mediaserver'] }}"

  handlers:
    - name: restart nginx on mediaservers
      ansible.builtin.systemd:
        name: nginx
        state: restarted
      delegate_to: "{{ item }}"
      delegate_facts: true
      loop: "{{ groups['mediaserver'] }}"
