├── templates ├── my.cnf.j2 ├── user-my.cnf.j2 ├── mariadb-galera.repo.j2 ├── clustercheck.conf.j2 └── galera.cnf.j2 ├── ansible.cfg ├── vars ├── main.yml └── RedHat.yml ├── tasks ├── config.yml ├── users.yml ├── setup-RedHat.yml ├── main.yml ├── clustercheck.yml ├── secure.yml └── galera.yml ├── handlers └── main.yml ├── meta └── main.yml ├── files └── galera-monitor ├── test.yml ├── defaults └── main.yml └── README.md /templates/my.cnf.j2: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | roles_path = ../ 3 | -------------------------------------------------------------------------------- /vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for ansible-role-mariadb-galera 3 | -------------------------------------------------------------------------------- /templates/user-my.cnf.j2: -------------------------------------------------------------------------------- 1 | [client] 2 | user={{ mariadb_root_username }} 3 | password={{ mariadb_root_password }} 4 | -------------------------------------------------------------------------------- /templates/mariadb-galera.repo.j2: -------------------------------------------------------------------------------- 1 | [mariadb-galera] 2 | name = MariaDB Galera 3 | baseurl = {{ mariadb_repo_baseurl }} 4 | gpgcheck=0 5 | enabled=1 6 | -------------------------------------------------------------------------------- /templates/clustercheck.conf.j2: -------------------------------------------------------------------------------- 1 | MYSQL_USERNAME="{{ galera_clustercheck_username }}" 2 | MYSQL_PASSWORD="{{ galera_clustercheck_password }}" 3 | MYSQL_HOST="localhost" 4 | MYSQL_PORT="3306" 5 | -------------------------------------------------------------------------------- /tasks/config.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for config 3 | 4 | - name: Ensure MariaDB is started and enabled on boot. 5 | service: 6 | name: "{{ mariadb_daemon }}" 7 | state: started 8 | enabled: yes 9 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # handlers file for ansible-role-mariadb-galera 3 | 4 | - name: restart mariadb 5 | service: name={{ mariadb_daemon }} state=restarted 6 | 7 | - name: bootstrap node 8 | shell: /usr/libexec/mysqld --wsrep-new-cluster --user={{ mariadb_daemon_user }} & 9 | -------------------------------------------------------------------------------- /vars/RedHat.yml: -------------------------------------------------------------------------------- 1 | # vars file RedHat.yml 2 | 3 | mariadb_daemon: mariadb 4 | __mariadb_packages: 5 | - mariadb-galera-server 6 | - MySQL-python 7 | - rsync 8 | - xinetd 9 | mariadb_daemon_user: mysql 10 | mariadb_config_dir: /etc/my.cnf.d/ 11 | mariadb_socket: /var/lib/mysql/mysql.sock 12 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: z 4 | description: An Ansible Role that installs MariaDB-Galera on RedHat/CentOS 5 | company: 6 | license: license (BSD, MIT) 7 | min_ansible_version: 1.6 8 | platforms: 9 | - name: EL 10 | versions: 11 | - 7 12 | categories: 13 | - clustering 14 | - database 15 | - database:sql 16 | dependencies: [] 17 | -------------------------------------------------------------------------------- /files/galera-monitor: -------------------------------------------------------------------------------- 1 | service galera-monitor 2 | { 3 | port = 9200 4 | disable = no 5 | socket_type = stream 6 | protocol = tcp 7 | wait = no 8 | user = root 9 | group = root 10 | groups = yes 11 | server = /usr/bin/clustercheck 12 | type = UNLISTED 13 | per_source = UNLIMITED 14 | log_on_success = 15 | log_on_failure = HOST 16 | flags = REUSE 17 | } 18 | -------------------------------------------------------------------------------- /tasks/users.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file users.yml 3 | 4 | - name: Ensure MariaDB users are present. 5 | mysql_user: 6 | name: "{{ item.name }}" 7 | host: "{{ item.host | default('localhost') }}" 8 | password: "{{ item.password }}" 9 | priv: "{{ item.priv | default('*.*:USAGE') }}" 10 | state: present 11 | with_items: 12 | - "{{ mariadb_users }}" 13 | run_once: True -------------------------------------------------------------------------------- /tasks/setup-RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file setup-RedHat 3 | 4 | - name: Adding MariaDB Galera Repo. 5 | template: 6 | src: mariadb-galera.repo.j2 7 | dest: /etc/yum.repos.d/mariadb-galera.repo 8 | mode: 0644 9 | owner: root 10 | group: root 11 | when: mariadb_repo 12 | 13 | - name: "[CentOS] Ensure MariaDB packages are installed." 14 | yum: 15 | name: "{{ item }}" 16 | state: installed 17 | with_items: 18 | - "{{ __mariadb_packages }}" 19 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for ansible-role-mariadb-galera 3 | 4 | # Include variables and define needed variables. 5 | - name: Include OS-specific variables. 6 | include_vars: "{{ ansible_os_family }}.yml" 7 | 8 | # Setup/install tasks. 9 | - include: setup-RedHat.yml 10 | when: ansible_os_family == 'RedHat' 11 | 12 | # Configure MariaDB. 13 | - include: config.yml 14 | - include: secure.yml 15 | - include: galera.yml 16 | - include: users.yml 17 | 18 | # Configure clustercheck. 19 | - include: clustercheck.yml 20 | when: galera_clustercheck_enable 21 | -------------------------------------------------------------------------------- /test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # test file 3 | 4 | - name: Install MariaDB Galera. 5 | hosts: servers 6 | roles: 7 | - role: ansible-role-mariadb-galera 8 | mariadb_root_username: root 9 | mariadb_root_password: root 10 | mariadb_users: 11 | - { name: "root", host: "%", password: "root", priv: "*.*:GRANT,ALL" } 12 | galera_clustercheck_enable: true 13 | galera_clustercheck_username: clustercheck 14 | galera_clustercheck_password: clustercheck 15 | galera_wsrep_cluster_address: ['192.168.100.11', '192.168.100.12', '192.168.100.13'] 16 | galera_bootstrap_node: "192.168.100.11" 17 | -------------------------------------------------------------------------------- /tasks/clustercheck.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for clustercheck 3 | 4 | - name: clustercheck sysconfig file. 5 | template: 6 | src: clustercheck.conf.j2 7 | dest: /etc/sysconfig/clustercheck 8 | owner: root 9 | group: root 10 | mode: 0644 11 | 12 | - name: galera monitor file 13 | copy: 14 | src: galera-monitor 15 | dest: /etc/xinetd.d/galera-monitor 16 | owner: root 17 | group: root 18 | mode: 0600 19 | 20 | - name: Ensure xinetd service is started and enabled on boot. 21 | service: 22 | name: xinetd 23 | state: started 24 | enabled: yes 25 | 26 | - name: Ensure clustercheck users are present. 27 | mysql_user: 28 | name: "{{ galera_clustercheck_username }}" 29 | host: "localhost" 30 | password: "{{ galera_clustercheck_password }}" 31 | priv: "*.*:ALL" 32 | state: present 33 | run_once: True 34 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # defaults file for ansible-role-mariadb-galera 3 | mariadb_repo: "True" 4 | mariadb_repo_baseurl: "http://yum.mariadb.org/5.5/centos7-amd64/" 5 | mariadb_root_username: root 6 | mariadb_root_password: root 7 | mariadb_users: [] 8 | 9 | # Clustercheck Configure 10 | galera_clustercheck_enable: false 11 | galera_clustercheck_username: clustercheck 12 | galera_clustercheck_password: clustercheck 13 | 14 | # Galera Configure 15 | galera_bootstrap_node: "" 16 | 17 | galera_wsrep_on: On 18 | galera_mysql_datadir: "/var/lib/mysql" 19 | galera_wsrep_provider: /usr/lib64/galera/libgalera_smm.so 20 | galera_wsrep_cluster_name: galera_cluster 21 | galera_wsrep_cluster_address: [] 22 | galera_wsrep_slave_threads: 1 23 | galera_wsrep_certify_nonPK: 1 24 | galera_wsrep_max_ws_rows: 131072 25 | galera_wsrep_max_ws_size: 1073741824 26 | galera_wsrep_debug: 0 27 | galera_wsrep_convert_lock_to_trx: 0 28 | galera_wsrep_retry_autocommit: 1 29 | galera_wsrep_auto_increment_control: 1 30 | galera_wsrep_drupal_282555_workaround: 0 31 | galera_wsrep_causal_reads: 0 32 | galera_wsrep_notify_cmd: "" 33 | galera_wsrep_sst_method: rsync 34 | -------------------------------------------------------------------------------- /tasks/secure.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file secure 3 | 4 | - name: Get list of hosts for the root user. 5 | command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = "root" ORDER BY (Host="localhost") ASC' 6 | register: mysql_root_hosts 7 | changed_when: false 8 | 9 | # 'localhost' needs to be last for idempotency. 10 | - name: Update MySQL root password for localhost root account. 11 | mysql_user: 12 | name: "root" 13 | host: "{{ item }}" 14 | password: "{{ mariadb_root_password }}" 15 | with_items: 16 | - "{{ mysql_root_hosts.stdout_lines }}" 17 | 18 | # Has to be after the root password assignment, for idempotency. 19 | - name: Copy .my.cnf file with root password credentials. 20 | template: 21 | src: "user-my.cnf.j2" 22 | dest: "~/.my.cnf" 23 | owner: root 24 | group: root 25 | mode: 0600 26 | 27 | - name: Get list of hosts for the anonymous user. 28 | command: mysql -NBe 'SELECT Host FROM mysql.user WHERE User = ""' 29 | register: mysql_anonymous_hosts 30 | changed_when: false 31 | 32 | - name: Remove anonymous MySQL users. 33 | mysql_user: 34 | name: "" 35 | host: "{{ item }}" 36 | state: absent 37 | with_items: 38 | - "{{ mysql_anonymous_hosts.stdout_lines}}" 39 | 40 | - name: Remove MySQL test database. 41 | mysql_db: 42 | name: "test" 43 | state: absent 44 | -------------------------------------------------------------------------------- /tasks/galera.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for galera 3 | 4 | - name: stop mariadb. 5 | service: 6 | name: "{{ mariadb_daemon }}" 7 | state: stopped 8 | 9 | - name: Adding Galera Configuration. 10 | template: 11 | src: galera.cnf.j2 12 | dest: "{{ mariadb_config_dir }}/galera.cnf" 13 | owner: root 14 | group: root 15 | mode: 0644 16 | 17 | - name: Bootstraping the Cluster. 18 | shell: | 19 | #/usr/libexec/mysqld --wsrep-new-cluster --user={{ mariadb_daemon_user }} & 20 | /bin/sh /usr/bin/mysqld_safe --datadir={{ galera_mysql_datadir }} --pid-file=/var/run/{{ mariadb_daemon }}/{{ mariadb_daemon }}.pid --wsrep-new-cluster --user={{ mariadb_daemon_user }} & 21 | when: inventory_hostname == "{{ galera_bootstrap_node }}" 22 | 23 | - name: Start the other nodes. 24 | service: 25 | name: "{{ mariadb_daemon }}" 26 | state: restarted 27 | when: inventory_hostname != "{{ galera_bootstrap_node }}" 28 | 29 | - name: Wait for the cluster running and bootstrapped. 30 | command: mysql --silent --skip-column-names -e 'SHOW STATUS LIKE "wsrep_cluster_size"' 31 | register: galera_check_wait 32 | until: galera_check_wait | success 33 | run_once: True 34 | 35 | - name: Finalizing the Installation. 36 | shell: | 37 | pkill -9 mysqld 38 | systemctl restart "{{ mariadb_daemon }}" 39 | when: 40 | - galera_check_wait.stdout | search("3") 41 | - inventory_hostname == "{{ galera_bootstrap_node }}" 42 | -------------------------------------------------------------------------------- /templates/galera.cnf.j2: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | datadir={{ galera_mysql_datadir }} 3 | skip-name-resolve=1 4 | binlog_format=ROW 5 | default-storage-engine=innodb 6 | innodb_autoinc_lock_mode=2 7 | innodb_locks_unsafe_for_binlog=1 8 | max_connections=2048 9 | query_cache_size=0 10 | query_cache_type=0 11 | {% for ipaddr in ansible_all_ipv4_addresses %} 12 | {%- if ipaddr in galera_wsrep_cluster_address %} 13 | bind-address={{ ipaddr }} 14 | {%- endif %} 15 | {% endfor %} 16 | 17 | wsrep_on={{ galera_wsrep_on }} 18 | wsrep_provider={{ galera_wsrep_provider }} 19 | wsrep_cluster_name={{ galera_wsrep_cluster_name }} 20 | wsrep_cluster_address="gcomm://{{ ",".join(galera_wsrep_cluster_address) }}" 21 | wsrep_slave_threads={{ galera_wsrep_slave_threads }} 22 | wsrep_certify_nonPK={{ galera_wsrep_certify_nonPK }} 23 | wsrep_max_ws_rows={{ galera_wsrep_max_ws_rows }} 24 | wsrep_max_ws_size={{ galera_wsrep_max_ws_size }} 25 | wsrep_debug={{ galera_wsrep_debug }} 26 | wsrep_convert_LOCK_to_trx={{ galera_wsrep_convert_lock_to_trx }} 27 | wsrep_retry_autocommit={{ galera_wsrep_retry_autocommit }} 28 | wsrep_auto_increment_control={{ galera_wsrep_auto_increment_control }} 29 | wsrep_drupal_282555_workaround={{ galera_wsrep_drupal_282555_workaround }} 30 | wsrep_causal_reads={{ galera_wsrep_causal_reads }} 31 | wsrep_notify_cmd={{ galera_wsrep_notify_cmd }} 32 | wsrep_sst_method={{ galera_wsrep_sst_method }} 33 | {% for ipaddr in ansible_all_ipv4_addresses %} 34 | {%- if ipaddr in galera_wsrep_cluster_address %} 35 | wsrep_node_address={{ ipaddr }} 36 | {%- endif %} 37 | {% endfor %} 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Role Name: MariaDB-Galera 2 | 3 | An Ansible Role that installs MariaDB-Galera on RedHat/CentOS. 4 | 5 | ## Requirements 6 | 7 | None. 8 | 9 | ## Role Variables 10 | 11 | ### `defaults/main.yml` 12 | 13 | * `mariadb_repo_baseurl: "http://yum.mariadb.org/5.5/centos7-amd64/"` 14 | * `mariadb_root_username: root` 15 | * `mariadb_root_password: root` 16 | * `galera_clustercheck_enable: false` 17 | * `galera_clustercheck_username: clustercheck` 18 | * `galera_clustercheck_password: clustercheck` 19 | * `galera_bootstrap_node: ""` 20 | * `galera_wsrep_provider: /usr/lib64/galera/libgalera_smm.so` 21 | * `galera_wsrep_cluster_name: galera_cluster` 22 | * `galera_wsrep_cluster_address: []` 23 | * `galera_wsrep_slave_threads: 1` 24 | * `galera_wsrep_certify_nonPK: 1` 25 | * `galera_wsrep_max_ws_rows: 131072` 26 | * `galera_wsrep_max_ws_size: 1073741824` 27 | * `galera_wsrep_debug: 0` 28 | * `galera_wsrep_convert_LOCK_to_trx: 0` 29 | * `galera_wsrep_retry_autocommit: 1` 30 | * `galera_wsrep_auto_increment_control: 1` 31 | * `galera_wsrep_drupal_282555_workaround: 0` 32 | * `galera_wsrep_causal_reads: 0` 33 | * `galera_wsrep_notify_cmd: ""` 34 | * `galera_wsrep_sst_method: rsync` 35 | 36 | ## Dependencies 37 | 38 | None. 39 | 40 | ## Example Playbook 41 | 42 | Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: 43 | 44 | - hosts: servers 45 | roles: 46 | - role: ansible-role-mariadb-galera 47 | mariadb_root_username: root 48 | mariadb_root_password: root 49 | mariadb_users: 50 | - { name: "root", host: "%", password: "root", priv: "*.*:GRANT,ALL" } 51 | galera_clustercheck_enable: true 52 | galera_clustercheck_username: clustercheck 53 | galera_clustercheck_password: clustercheck 54 | galera_wsrep_cluster_address: ['192.168.100.11', '192.168.100.12', '192.168.100.13'] 55 | galera_bootstrap_node: "192.168.100.11" 56 | 57 | ## Author Information 58 | 59 | z 60 | --------------------------------------------------------------------------------