Creates exporter user in mysql – requires parameters “mysql_exporter_password” and “mysql_root_password”.

To run it:
ansible-playbook “myplaybookname.yaml” -i ./hosts -e “mysql_exporter_password=…” -e “mysql_root_password=…”

- name: start prometheus mysql-exporter in docker
  hosts: newinstance
  become: yes
  become_method: sudo
  gather_facts: yes

  tasks:
  - name: show hosts
    debug: msg="play_hosts={{play_hosts}}"

  - name: install pip
    apt:
      name: python-pip
      state: present
      update_cache: yes
    
  - name: install docker-py
    pip:
      name: docker-py
      state: present
      version: 1.7.0

  - name: install docker-compose
    pip:
      name: docker-compose
      state: present
      version: 1.7.0

  - name: create exporter user on mysql
    become: true
    mysql_user: 
      name: exporter 
      host: "%" 
      password: "{{ mysql_exporter_password }}"
      login_user: root
      login_password: "{{ mysql_root_password }}"
      priv: "{{item}}"
      state: present
    with_items:
    - "*.*:PROCESS,REPLICATION\ CLIENT,SELECT"
    - "performance_schema.*:SELECT"

  - name: start mysql exporter
    docker_container:
      name: mysql-exporter
      image: prom/mysqld-exporter
      state: started
      restart_policy: unless-stopped
      pull: true
      ports:
      - 9104:9104
      network_mode: host
      env:
        DATA_SOURCE_NAME="exporter:{{mysql_exporter_password}}({{ansible_default_ipv4.address}}:3306)/upcload"
      command: -collect.info_schema.tables=true