Skip to content

Instantly share code, notes, and snippets.

@b-mc
Last active August 2, 2021 08:09
Show Gist options
  • Save b-mc/b79f94c45f0d7b1a437d013533cdbe64 to your computer and use it in GitHub Desktop.
Save b-mc/b79f94c45f0d7b1a437d013533cdbe64 to your computer and use it in GitHub Desktop.
Ansible Playbook: Upgrade MariaDB sequentially (RHEL/CentOS 7)

Ansible Playbook: Upgrade MariaDB sequentially

About

I have created this playbook to perform an in-place upgrade of an old MariaDB instance from 5.5.68 to 10.5, sequentially (this is the recommended upgrade path). The playbook works with RHEL7/CentOS7 only and assumes the default installation directories. It doesn't migrate the database settings. It saves the upgrade results locally on the host.

Prerequisites

  • community.mysql.mysql_variables installed on the controller
  • enabled SCL repos on the host
  • MariaDB root password defined in a secret vault file

Run

On each run, specify the old version and the new one, e.g.:

ansible-playbook mariadb-upgrade.yml --vault-password-file ~/vault.pwd --extra-vars "old_ver=5 new_ver=100"
ansible-playbook mariadb-upgrade.yml --vault-password-file ~/vault.pwd --extra-vars "old_ver=100 new_ver=101"

Playbook

- name: upgrade mariadb
  hosts: ...
  become: yes
  pre_tasks:
  - name: check versions
    assert:
      that: old_ver in allowed_versions and new_ver in allowed_versions
      fail_msg: No supported MariaDB version(s) provided
  - name: check version sanity
    assert:
      that: new_ver|int > old_ver|int
      fail_msg: Old version is greater than the new one
  - name: include password from vault
    include_vars: secrets/mariadb-root-webserver.yaml
  vars:
    allowed_versions: [ '5', '100', '101', '102', '103', '105' ]
    utils_for: [ '102', '103', '105' ]
    old_svc_name: "{{ 'mariadb' if old_ver == '5' else 'rh-mariadb' + old_ver + '-mariadb'}}"
    new_svc_name: "rh-mariadb{{ new_ver }}-mariadb"
    old_datadir: "{{ '/var/lib/mysql' if old_ver == '5' else '/var/opt/rh/rh-mariadb' + old_ver + '/lib/mysql' }}"
    new_datadir: "/var/opt/rh/rh-mariadb{{ new_ver }}/lib/"

  tasks:
  - name: populate service facts
    service_facts:
  - name: check if old service exists
    fail:
      msg: The service {{ old_svc_name }} is not installed
    when: ansible_facts.services[old_svc_name + '.service'] is not defined
  - name: install required packages
    package:
      name:
      - python2-PyMySQL
      - rh-mariadb{{ new_ver }}-mariadb-server
  - name: install database utils
    package:
      name:
      - rh-mariadb{{ new_ver }}-mariadb-server-utils
    when: new_ver in utils_for
  - name: disable fast shutdown
    community.mysql.mysql_variables:
      variable: innodb_fast_shutdown
      value: '0'
      login_user: root
      login_password: "{{ mariadb_root }}"
  - name: stop and disable old mariadb service
    service:
      name: "{{ old_svc_name }}"
      state: stopped
      enabled: no
  - name: copy mysql contents
    copy:
      src: "{{ old_datadir }}"
      remote_src: yes
      dest: "{{ new_datadir }}"
      owner: mysql
      group: mysql
  - name: start and enable new mariadb service
    service:
      name: "{{ new_svc_name }}"
      state: started
      enabled: yes
  - name: upgrade db
    command: /opt/rh/rh-mariadb{{ new_ver }}/root/usr/bin/mysql_upgrade --verbose --verbose -u root -p{{ mariadb_root }}
    register: upgrade
    no_log: True
  - name: save upgrade result
    copy:
      content: "{{ upgrade.stdout }}"
      dest: output/mariadb_upgrade_{{ ansible_hostname }}_{{ ansible_date_time.date }}_{{ ansible_date_time.time }}.txt
    become: no
    delegate_to: localhost
  - name: restart new mariadb service
    service:
      name: "{{ new_svc_name }}"
      state: restarted
      enabled: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment