---
- name: Live vhost setup
  hosts: live
  tags: live
  gather_facts: false
  tasks:
    - name: resolve domain name to localhost
      ansible.builtin.lineinfile:
        path: /etc/hosts
        line: 127.0.1.1 {{ live_domain }}
        backup: true

    - name: fill the vhost file
      notify: Restart nginx
      ansible.builtin.replace:
        path: /etc/nginx/sites-available/live-rtmp.conf
        regexp: ^(\s+server_name)\s+.*(;)$
        replace: \1 {{ live_domain }}\2

    - name: Activating the live vhost configuration
      notify: Restart nginx
      ansible.builtin.file:
        src: /etc/nginx/sites-available/live-rtmp.conf
        dest: /etc/nginx/sites-enabled/live-rtmp.conf
        state: link

  handlers:
    - name: Restart nginx
      ansible.builtin.systemd:
        name: nginx
        state: restarted

- import_playbook: ../functions/create-live-app.yml
  vars:
    live_app_name: msuser
    rtmp_hls_url: https://{{ hostvars[groups['live'][0]].live_domain }}/streaming-rtmp/%(rtmp_name)s/%(stream_id)s.m3u8
    rtmp_pub_url: rtmp://{{ hostvars[groups['live'][0]].live_domain }}/%(rtmp_app)s/%(stream_id)s
    deploy_case: ha

- hosts: mediaserver
  tags: live
  gather_facts: false
  tasks:
    - name: Check the existence of the rtmp configuration folder
      ansible.builtin.stat:
        path: /etc/nginx/rtmp.d
      register: rtmp_conf_dir

    - name: Remove unused MediaServer(s) rtmp configurations
      ansible.builtin.command:
        cmd: /bin/rm -r /etc/nginx/rtmp.d
        removes: /etc/nginx/rtmp.d
      args:
        warn: false
      when: rtmp_conf_dir.stat.exists
