---
# task bundle simply copied from main.yml
# TODO: migrate to https://github.com/cloudalchemy/ansible-mysqld_exporter
- name: "Install prometheus-mysqld-exporter"# noqa package-latest
  package:
    name: "{{ item }}"
    state: latest
  with_items:
    - prometheus-mysqld-exporter

- name: Ensure prometheus user for prometheus-mysqld-exporter exists
  community.mysql.mysql_user:
    name: "prometheus"
    priv: "*.*:PROCESS,REPLICATION CLIENT,SELECT"
    login_unix_socket: /run/mysqld/mysqld.sock
    login_password: "{{ mysql_root_password }}"
  register: mysql_exporter_user_creds
  notify: prometheus-mysqld-exporter restart

- name: Ensure is prometheus-mysqld-exporter configured
  lineinfile:
    regex: "^DATA_SOURCE_NAME="
    line: 'DATA_SOURCE_NAME="prometheus@unix(/run/mysqld/mysqld.sock)/"'
    path: /etc/default/prometheus-mysqld-exporter
  register: mysql_exporter_data_source
  notify: prometheus-mysqld-exporter restart

- name: Setup prometheus-mysqld-exporter interface bind
  lineinfile:
    path: /etc/default/prometheus-mysqld-exporter
    regex: "^ARGS="
    line: "ARGS=\"--web.listen-address='{{ stage_private_server_ip }}:{{ monitor_port_maria }}'\""
  register: mysql_exporter_args
  notify: prometheus-mysqld-exporter restart

- name: "Ensure prometheus-mysqld-exporter is running"
  service:
    name: prometheus-mysqld-exporter
    state: started
    enabled: yes
