---
- name: Create MySQL Exporter Directory
  file:
    path: /opt/mysqld_exporter
    state: directory
    mode: 0755

- name: Set Architecture
  set_fact:
    arch: "{{'amd64' if (ansible_architecture == 'x86_64') else 'arm64'}}"

- name: Download MySQL Exporter Archive
  unarchive:
    src: "http://git.jingrow.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-{{ arch }}.tar.gz"
    dest: /tmp
    remote_src: yes

- name: Copy MySQL Exporter Binary
  copy:
    src: "/tmp/mysqld_exporter-0.15.1.linux-{{ arch }}/mysqld_exporter"
    dest: /opt/mysqld_exporter/mysqld_exporter
    remote_src: yes
    mode: 0755
    owner: root
    group: root

- name: Create MySQL Exporter Systemd Service File
  template:
    src: mysqld_exporter.service
    dest: /etc/systemd/system/mysqld_exporter.service
    owner: root
    group: root
    mode: 0644

- name: Restart MySQL Exporter Service
  systemd:
    daemon_reload: true
    name: mysqld_exporter
    enabled: yes
    state: restarted
