---
# tasks file for ansible-role-mariadb

- name: Set db_main host variable
  set_fact:
    lbsql_main: "{{ groups['db_lbal_servers'][0] }}"
    my_service: {"name": "proxysql.service", "source": "systemd", "state": "unknown", "status": "disabled"}

- name: Include OS specific variables.
  include_vars: "{{ ansible_os_family }}.yml"

- name: collect facts about system services
  service_facts:
  register: services_state

- name: Set db_main host variable
  set_fact:
    my_service: "{{ ansible_facts.services['proxysql.service'] }}"
  when: "'proxysql.service' in ansible_facts.services.keys()"

- name: Check proxySQL status
  debug:
    var: my_service

- name: Install proxySQL
  include_tasks: "setup/{{ ansible_os_family }}.yml"

- name: Install proxysql scripts
  copy:
    src: "{{ item }}"
    dest: /var/lib/proxysql/
    owner: proxysql
    group: proxysql
    mode: 0744
  loop:
    - "{{ role_path }}/files/proxysql_galera_checker"
    - "{{ role_path }}/files/proxysql_node_monitor"

- name: Install proxysql admin config
  template:
    dest: /etc/proxysql-admin.cnf
    src: proxysql-admin.cnf.j2
    mode: 0644

- name: Ensure proxysql_galera_checker.log exists
  copy:
    content: ""
    dest: /var/lib/proxysql/proxysql_galera_checker.log
    force: no
    group: proxysql
    owner: proxysql
    mode: 0644

- name: Ending if proxySQL is already up and running
  meta: end_play
  when: my_service.state != "unknown" and my_service.status != "disabled"

# Configuring ProxySQL through the admin interface

- name: Ensure proxySQL configfile is present
  include_tasks: "config.yml"

# Or Configuring ProxySQL through the config file

    #- name: Ensure proxySQL configfile is present
    #include_tasks: "config_file.yml"

