TrashNotes

2025-03-12 21:24:06
---
- name: Configure MariaDB logging on CentOS 7
  hosts: all
  become: yes
  vars:
    mariadb_log_error: /var/log/mariadb/error.log
    mariadb_general_log: /var/log/mariadb/general_query.log
    mariadb_slow_query_log: /var/log/mariadb/slow_query.log
    mariadb_binlog: mysql-bin

  tasks:
    - name: Ensure MariaDB is installed
      yum:
        name: mariadb-server
        state: present

    - name: Create directories for logs if they don't exist
      file:
        path: "{{ item }}"
        state: directory
        owner: mysql
        group: mysql
        mode: '0750'
      loop:
        - /var/log/mariadb

    - name: Ensure log files are present
      touch:
        path: "{{ item }}"
        state: file
        owner: mysql
        group: mysql
        mode: '0640'
      loop:
        - "{{ mariadb_log_error }}"
        - "{{ mariadb_general_log }}"
        - "{{ mariadb_slow_query_log }}"

    - name: Configure MariaDB to enable logging
      lineinfile:
        path: /etc/my.cnf
        regexp: "^#?"
        line: "{{ item }}"
        backrefs: yes
      loop:
        - "log_error={{ mariadb_log_error }}"
        - "general_log = 1"
        - "general_log_file = {{ mariadb_general_log }}"
        - "slow_query_log = 1"
        - "slow_query_log_file = {{ mariadb_slow_query_log }}"
        - "long_query_time = 2"
        - "log_bin = {{ mariadb_binlog }}"
        - "binlog_format = ROW"
        - "expire_logs_days = 7"
      notify:
        - restart MariaDB

    - name: Restart MariaDB to apply changes
      service:
        name: mariadb
        state: restarted

  handlers:
    - name: restart MariaDB
      service:
        name: mariadb
        state: restarted
← Previous Next →
Back to list