---
- hosts: all
  roles:
    - ../roles/go
    - role: ../roles/cgrates
      vars:
        cgrates_dbs:
          - mysql
        cgrates_dependencies:
          - git
          - redis-server
          - mariadb-server
          - xxd # needed for unix2ntp script (added here for convenience)

    - role: ../roles/opensips
      vars:
        opensips_cfg_path: /usr/share/cgrates/tutorials/osips_diam/etc/opensips/opensips_ccr_prepaid.cfg
        opensips_dict_path: /usr/share/cgrates/tutorials/osips_diam/etc/opensips/dictionary.opensips
        opensips_version: '3.5'
        opensips_release_type: nightly
        opensips_modules:
          - opensips-auth-modules
          - opensips-diameter-module
          - opensips-json-module
          - opensips-mysql-module
        opensips_cfg_replacements:
          - before: '^socket=udp:enp0s3:5060.*$'
            after: 'socket=udp:{{ ansible_default_ipv4.interface }}:5060'

    - role: ../roles/freediameter
      vars:
        fd_dns_entries:
          - ip: '{{ ansible_default_ipv4.address }}'
            hostname: '{{ fd_client_identity }}'
          - ip: '{{ ansible_default_ipv4.address }}'
            hostname: '{{ fd_server_identity }}'

    - role: ../roles/pjsua
      vars:
        pjsua_helper_scripts: true

  post_tasks:
    - name: Configure diameter listen address in CGRateS cfg
      become: true
      ansible.builtin.lineinfile:
        path: /usr/share/cgrates/tutorials/osips_diam/etc/cgrates/cgrates.json
        regexp: '^\s*"listen": "192\.168\.122\.42:3868",\s*$'
        line: '"listen": "{{ ansible_default_ipv4.address }}:3868",'

    - name: Ensure opensips is allowed to access privkey.pem 
      become: true
      ansible.builtin.file:
        path: /etc/freeDiameter/tls/privkey.pem
        mode: 0644

    - name: Move unix2ntp script to /usr/local/bin 
      become: true
      ansible.builtin.copy:
        src: /usr/share/cgrates/tutorials/osips_diam/etc/opensips/unix2ntp
        dest: /usr/local/bin
        owner: root
        group: root
        remote_src: yes
        mode: 0755