├── Disaster Recovery.md ├── LICENSE ├── README.md ├── Vagrantfile ├── drc.sh ├── inventory.ini ├── mariadb-master-slave.yml ├── mariadb-multi-master.yml └── mariadb-single.yml /Disaster Recovery.md: -------------------------------------------------------------------------------- 1 | # Disaster Recovery Manual 2 | 3 | In an event/disaster where Master Database Server can't be accessed, down, or deleted we need to promote a Slave Server into a Master Server to minimize down time 4 | 5 | In this step by step case, we have 3 Database Server. 1 Master(Server A) and 2 Slave(Server B and Server C). Server A is currently down and we want to promote Server B from Slave Server into Master Server 6 | 7 | ![image](https://user-images.githubusercontent.com/67664879/212128421-aba57700-a20d-4a48-bfad-f518fbe1eeaf.png) 8 | 9 | 10 | ## Step 1 11 | SSH into Server B 12 | ``` 13 | ssh ubuntu@server-b-ip 14 | ``` 15 | 16 | ## Step 2 17 | Login into MySQL 18 | ``` 19 | sudo mysql -u root 20 | ``` 21 | 22 | If you configure root with password, enter this instead 23 | ``` 24 | sudo mysql -u root -p 25 | ``` 26 | 27 | ## Step 3 28 | Stop and reset slave replication 29 | ``` 30 | stop slave; 31 | reset slave; 32 | ``` 33 | 34 | ## Step 4 35 | By default, slave server still can write. But if you configure read only on slave server, enable write on server by adding/editing this line on `/etc/mysql/my.cnf` 36 | ``` 37 | [mysqld] 38 | read-only = 0 39 | ``` 40 | 41 | Or run this query on mysql 42 | ``` 43 | SET read_only false; 44 | ``` 45 | 46 | Your new master server is now ready 47 | 48 | ## Step 5(and so on is optional) 49 | If want other slave server to still run replication, you can point them to your new master server 50 | In your new Master Server(Server B), run this 51 | 52 | ``` 53 | SHOW MASTER STATUS; 54 | ``` 55 | 56 | You will get something like this 57 | ``` 58 | +------------------+----------+--------------+------------------+ 59 | | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | 60 | +------------------+----------+--------------+------------------+ 61 | | mysql-bin.000001 | 786 | | | 62 | +------------------+----------+--------------+------------------+ 63 | ``` 64 | Note down the `File`, `Position`, and your server IP 65 | 66 | ## Step 6 67 | 68 | SSH into your other Slave Server(Server C) 69 | ``` 70 | ssh ubuntu@server-c-ip 71 | ``` 72 | 73 | ## Step 7 74 | Login into MySQL 75 | ``` 76 | sudo mysql -u root 77 | ``` 78 | 79 | If you configure root with password, enter this instead 80 | ``` 81 | sudo mysql -u root -p 82 | ``` 83 | 84 | ## Step 8 85 | Stop and reset slave replication 86 | ``` 87 | stop slave; 88 | reset slave; 89 | ``` 90 | 91 | ## Step 9 92 | Run this query, change the variable into what you note before 93 | ``` 94 | CHANGE MASTER TO MASTER_HOST = '', MASTER_USER = '', MASTER_PASSWORD = '', MASTER_LOG_FILE = '', MASTER_LOG_POS = ; 95 | ``` 96 | 97 | ## Step 10 98 | Run replication with this query 99 | ``` 100 | start slave; 101 | ``` 102 | 103 | Repeat step 6 - 10 for each of your slave server 104 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Creatif Studio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MariaDB Deployer 2 | 3 | This project provides a set of Ansible, Terraform and Vagrant configuration files to deploy a docker application to a local/cloud environment. The deployment process includes setting up the necessary infrastructure, configuring servers, installing software, and starting the application. 4 | 5 | ## Prerequisites 6 | 7 | To use this project, you will need the following: 8 | 9 | - A cloud provider account with appropriate permissions to create and manage resources. 10 | - Ansible, Terraform, Vagrant, VirtualBox installed on your local machine. 11 | - A valid SSH key pair for authenticating with the remote servers. 12 | 13 | ## Getting Started 14 | 15 | To get started, follow these steps: 16 | 17 | 1. Clone the repository to your local machine: 18 | 19 | `git clone https://github.com/creatif-studio/boilerplate-deployer.git` 20 | 21 | 2. Navigate to the `ansible` directory and create the necessary environment-specific variable files for your deployment. See the README.md file in that directory for more information. 22 | 23 | 3. Navigate to the `terraform` directory and create the necessary `terraform.tfvars` file for your deployment. See the README.md file in that directory for more information. 24 | 25 | 4. Initialize the Terraform working directory: 26 | 27 | `terraform init` 28 | 29 | 5. Provision the infrastructure: 30 | 31 | `terraform apply` 32 | 33 | 6. Once the infrastructure is provisioned, run the Ansible playbook to configure the servers and deploy the application: 34 | 35 | `ansible-playbook terraform.yml` or `ansible-playbook vagrant.yml` 36 | 37 | ## Directory Structure 38 | 39 | Here is an overview of the directory structure of this project: 40 | 41 | ``` 42 | elastic-search-deployer/ 43 | ├── README.md 44 | ├── ansible/ 45 | │ ├── group_vars/ 46 | │ ├── host_vars/ 47 | │ ├── inventory/ 48 | │ ├── main.yml 49 | │ └── roles/ 50 | │ ├── common/ 51 | │ ├── nginx/ 52 | │ └── web/ 53 | └── terraform/ 54 | ├── main.tf 55 | ├── terraform.tfvars 56 | └── variables.tf 57 | ``` 58 | 59 | ## Contributing 60 | 61 | If you'd like to contribute to this project, please follow these steps: 62 | 63 | 1. Fork this repository. 64 | 2. Create a branch for your changes. 65 | 3. Make your changes and commit them to your branch. 66 | 4. Push your branch to your forked repository. 67 | 5. Open a pull request to merge your changes into the main repository. 68 | 69 | ## License 70 | 71 | This project is licensed under the MIT License. See the `LICENSE` file for details. 72 | -------------------------------------------------------------------------------- /Vagrantfile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby -*- 2 | # vi: set ft=ruby : 3 | 4 | # All Vagrant configuration is done below. The "2" in Vagrant.configure 5 | # configures the configuration version (we support older styles for 6 | # backwards compatibility). Please don't change it unless you know what 7 | # you're doing. 8 | Vagrant.configure("2") do |config| 9 | 10 | config.ssh.insert_key = false 11 | 12 | config.vm.define "ubuntu" do |ubuntu| 13 | ubuntu.vm.box = "generic/ubuntu2004" 14 | end 15 | 16 | config.vm.define "ubuntu2" do |ubuntu| 17 | ubuntu.vm.box = "generic/ubuntu2004" 18 | end 19 | config.vm.network "public_network" 20 | 21 | # The most common configuration options are documented and commented below. 22 | # For a complete reference, please see the online documentation at 23 | # https://docs.vagrantup.com. 24 | 25 | # Every Vagrant development environment requires a box. You can search for 26 | # boxes at https://vagrantcloud.com/search. 27 | config.vm.box = "base" 28 | 29 | # Disable automatic box update checking. If you disable this, then 30 | # boxes will only be checked for updates when the user runs 31 | # `vagrant box outdated`. This is not recommended. 32 | # config.vm.box_check_update = false 33 | 34 | # Create a forwarded port mapping which allows access to a specific port 35 | # within the machine from a port on the host machine. In the example below, 36 | # accessing "localhost:8080" will access port 80 on the guest machine. 37 | # NOTE: This will enable public access to the opened port 38 | # config.vm.network "forwarded_port", guest: 80, host: 8080 39 | 40 | # Create a forwarded port mapping which allows access to a specific port 41 | # within the machine from a port on the host machine and only allow access 42 | # via 127.0.0.1 to disable public access 43 | # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" 44 | 45 | # Create a private network, which allows host-only access to the machine 46 | # using a specific IP. 47 | # config.vm.network "private_network", ip: "192.168.33.10" 48 | 49 | # Create a public network, which generally matched to bridged network. 50 | # Bridged networks make the machine appear as another physical device on 51 | # your network. 52 | # config.vm.network "public_network" 53 | 54 | # Share an additional folder to the guest VM. The first argument is 55 | # the path on the host to the actual folder. The second argument is 56 | # the path on the guest to mount the folder. And the optional third 57 | # argument is a set of non-required options. 58 | # config.vm.synced_folder "../data", "/vagrant_data" 59 | 60 | # Provider-specific configuration so you can fine-tune various 61 | # backing providers for Vagrant. These expose provider-specific options. 62 | # Example for VirtualBox: 63 | # 64 | config.vm.provider "hyperv" do |vb| 65 | 66 | # Customize the amount of memory on the VM: 67 | vb.memory = "512" 68 | vb.maxmemory = "1024" 69 | vb.cpus = 1 70 | end 71 | # 72 | # View the documentation for the provider you are using for more 73 | # information on available options. 74 | 75 | # Enable provisioning with a shell script. Additional provisioners such as 76 | # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the 77 | # documentation for more information about their specific syntax and use. 78 | # config.vm.provision "shell", inline: <<-SHELL 79 | # apt-get update 80 | # apt-get install -y apache2 81 | # SHELL 82 | 83 | config.vm.provision "shell", inline: <<-SHELL 84 | # apt-get update 85 | echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO+JyAQHdjnMLd6Hg5Pblet6L83Eetfu/ZeDoNlgPrr9 eddsa-key-20221230" >> /home/vagrant/.ssh/authorized_keys 86 | SHELL 87 | end 88 | -------------------------------------------------------------------------------- /drc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Variables 4 | slave1_host="slave1_host" 5 | slave1_username="slave1_username" 6 | slave1_password="slave1_password" 7 | slave2_host="slave2_host" 8 | slave2_username="slave2_username" 9 | slave2_password="slave2_password" 10 | 11 | # Stop the replication process on both slaves 12 | mysql -h $slave1_host -u $slave1_username -p$slave1_password -e "STOP SLAVE;" 13 | mysql -h $slave2_host -u $slave2_username -p$slave2_password -e "STOP SLAVE;" 14 | 15 | # Reset the master logs on both slaves 16 | mysql -h $slave1_host -u $slave1_username -p$slave1_password -e "RESET MASTER;" 17 | mysql -h $slave2_host -u $slave2_username -p$slave2_password -e "RESET MASTER;" 18 | 19 | # Choose one slave to be the new master 20 | mysql -h $slave1_host -u $slave1_username -p$slave1_password -e "CHANGE MASTER TO MASTER_HOST='', MASTER_USER='', MASTER_PASSWORD='';" 21 | 22 | # Start the replication process on the new master 23 | mysql -h $slave1_host -u $slave1_username -p$slave1_password -e "START SLAVE;" 24 | 25 | # Configure the other slave to replicate from the new master 26 | mysql -h $slave2_host -u $slave2_username -p$slave2_password -e "CHANGE MASTER TO MASTER_HOST='$slave1_host', MASTER_USER='$slave1_username', MASTER_PASSWORD='$slave1_password';" 27 | 28 | # Start the replication process on the second slave 29 | mysql -h $slave2_host -u $slave2_username -p$slave2_password -e "START SLAVE;" 30 | -------------------------------------------------------------------------------- /inventory.ini: -------------------------------------------------------------------------------- 1 | [master] 2 | 10.10.10.1 3 | 4 | [slave] 5 | 10.10.10.2 6 | 7 | [all:vars] 8 | ansible_connection='ssh' 9 | ansible_ssh_port='22' 10 | ansible_user='ubuntu' 11 | ansible_ssh_private_key_file=/home/yuuzukatsu/.ssh/yzk -------------------------------------------------------------------------------- /mariadb-master-slave.yml: -------------------------------------------------------------------------------- 1 | - name: MariaDB Master Slave Setup 2 | hosts: all 3 | vars: 4 | - root_password: rootpswd 5 | - db_username: user 6 | - db_password: password 7 | - database: newdb 8 | become: true 9 | 10 | tasks: 11 | 12 | - name: Run Apt Update 13 | ansible.builtin.apt: 14 | update_cache: true 15 | 16 | - name: Install Python and Pip for Ansible Script 17 | ansible.builtin.apt: 18 | name: 19 | - python3 20 | - python3-pip 21 | 22 | - name: Install Python Module for Ansible MariaDB Automation 23 | ansible.builtin.pip: 24 | name: 25 | - PyMySQL 26 | 27 | - name: MariaDB Installation 28 | ansible.builtin.apt: 29 | name: 30 | - mariadb-server 31 | - mariadb-client 32 | 33 | - name: add [mysqld] in my.cnf 34 | ansible.builtin.lineinfile: 35 | path: /etc/mysql/my.cnf 36 | regexp: "\\[mysqld]" 37 | line: "[mysqld]" 38 | state: present 39 | 40 | - name: Add configuration in my.cnf 41 | ansible.builtin.lineinfile: 42 | path: /etc/mysql/my.cnf 43 | regexp: "{{item}}" 44 | line: "{{item}}" 45 | state: present 46 | with_items: 47 | - "bind-address = 0.0.0.0" 48 | - "log_bin = /var/log/mysql/mysql-bin.log" 49 | - "max_binlog_size = 100M" 50 | - "relay_log = /var/log/mysql/mysql-relay-bin" 51 | - "relay_log_index = /var/log/mysql/mysql-relay-bin.index" 52 | 53 | - name: Delete Anonymous user 54 | community.mysql.mysql_user: 55 | name: '' 56 | host: localhost 57 | state: absent 58 | login_unix_socket: /run/mysqld/mysqld.sock 59 | 60 | - name: Set Root Password with defined variable 61 | community.mysql.mysql_user: 62 | name: "root" 63 | password: "{{root_password}}" 64 | host: "{{item}}" 65 | login_unix_socket: /run/mysqld/mysqld.sock 66 | with_items: 67 | - localhost 68 | 69 | ### RUN ON MASTER SERVER ONLY 70 | # when: inventory_hostname in groups["master[0]"][0] 71 | 72 | - name: Add server-id line in Master Server my.cnf 73 | ansible.builtin.lineinfile: 74 | path: /etc/mysql/my.cnf 75 | regexp: "{{item}}" 76 | line: "{{item}}" 77 | state: present 78 | with_items: 79 | - "server-id = 1" 80 | when: inventory_hostname in groups["master"][0] 81 | 82 | - name: Restart and Enable Master Server MariaDB 83 | ansible.builtin.service: 84 | name: mysql 85 | state: restarted 86 | enabled: true 87 | when: inventory_hostname in groups["master"][0] 88 | 89 | - name: "Create user dbrep for replication in Master Server" 90 | community.mysql.mysql_user: 91 | name: "dbrep" 92 | password: "dbrep" 93 | priv: "*.*:REPLICATION SLAVE" 94 | host: "%" 95 | login_user: root 96 | login_password: "{{root_password}}" 97 | state: present 98 | when: inventory_hostname in groups["master"][0] 99 | 100 | - name: Get Master Status 101 | community.mysql.mysql_info: 102 | login_user: root 103 | login_password: "{{root_password}}" 104 | login_unix_socket: /run/mysqld/mysqld.sock 105 | register: master_status 106 | when: inventory_hostname in groups["master"][0] 107 | 108 | - name: Get Master Host 109 | command: echo "{{hostvars[inventory_hostname].inventory_hostname}}" 110 | register: master_host 111 | when: inventory_hostname in groups["master"][0] 112 | 113 | ### RUN ON SLAVE ONLY 114 | # when: inventory_hostname in groups["slave"] 115 | 116 | - name: Add server-id and read only line in Slave Server my.cnf 117 | ansible.builtin.lineinfile: 118 | path: /etc/mysql/my.cnf 119 | regexp: "{{item}}" 120 | line: "{{item}}" 121 | state: present 122 | with_items: 123 | - "server-id = 2" 124 | - "read-only" = 1" 125 | when: inventory_hostname in groups["slave"] 126 | 127 | - name: Restart and Enable Slave Server MariaDB 128 | ansible.builtin.service: 129 | name: mysql 130 | state: restarted 131 | enabled: true 132 | when: inventory_hostname in groups["slave"] 133 | 134 | - name: Stop and then Reset Slave Server 135 | community.mysql.mysql_replication: 136 | mode: "{{item}}" 137 | login_user: root 138 | login_password: "{{root_password}}" 139 | login_host: localhost 140 | when: inventory_hostname in groups["slave"] 141 | with_items: 142 | - stopreplica 143 | - resetreplica 144 | 145 | - name: Config Slave Server 146 | community.mysql.mysql_replication: 147 | mode: changeprimary 148 | login_user: root 149 | login_password: "{{root_password}}" 150 | login_host: localhost 151 | primary_host : "{{ hostvars[groups['master'].0].master_host.stdout }}" 152 | # primary_host : 172.17.137.109 153 | primary_user : "dbrep" 154 | primary_password : "dbrep" 155 | primary_log_file : "{{ hostvars[groups['master'].0].master_status.master_status.File }}" 156 | primary_log_pos : "{{ hostvars[groups['master'].0].master_status.master_status.Position }}" 157 | when: inventory_hostname in groups["slave"] 158 | 159 | - name: Start Slave Server 160 | community.mysql.mysql_replication: 161 | mode: startreplica 162 | login_user: root 163 | login_password: "{{root_password}}" 164 | login_host: localhost 165 | when: inventory_hostname in groups["slave"][0] 166 | 167 | - name: Create Database "{{database}}" 168 | community.mysql.mysql_db: 169 | name: "{{database}}" 170 | login_user: root 171 | login_password: "{{root_password}}" 172 | login_host: localhost 173 | when: inventory_hostname in groups["master"][0] 174 | 175 | - name: "Create user {{db_username}} for database {{database}}" 176 | community.mysql.mysql_user: 177 | name: "{{db_username}}" 178 | password: "{{db_password}}" 179 | priv: "{{database}}.*:ALL,GRANT" 180 | host: "%" 181 | login_user: root 182 | login_password: "{{root_password}}" 183 | state: present 184 | when: inventory_hostname in groups["master"][0] 185 | -------------------------------------------------------------------------------- /mariadb-multi-master.yml: -------------------------------------------------------------------------------- 1 | - name: MariaDB Multi Master Setup 2 | hosts: master 3 | vars: 4 | - root_password: rootpswd 5 | - db_username: user 6 | - db_password: password 7 | - database: newdb 8 | become: true 9 | 10 | tasks: 11 | 12 | - name: Run Apt Update 13 | ansible.builtin.apt: 14 | update_cache: true 15 | 16 | - name: Install Python and Pip for Ansible Script 17 | ansible.builtin.apt: 18 | name: 19 | - python3 20 | - python3-pip 21 | 22 | - name: Install Python Module for Ansible MariaDB Automation 23 | ansible.builtin.pip: 24 | name: 25 | - PyMySQL 26 | 27 | - name: MariaDB Installation 28 | ansible.builtin.apt: 29 | name: 30 | - mariadb-server 31 | - mariadb-client 32 | 33 | - name: add [mysqld] in my.cnf 34 | ansible.builtin.lineinfile: 35 | path: /etc/mysql/my.cnf 36 | regexp: "\\[mysqld]" 37 | line: "[mysqld]" 38 | state: present 39 | 40 | - name: Add configuration in my.cnf 41 | ansible.builtin.lineinfile: 42 | path: /etc/mysql/my.cnf 43 | regexp: "{{item}}" 44 | line: "{{item}}" 45 | state: present 46 | with_items: 47 | - "bind-address = 0.0.0.0" 48 | - "log_bin = /var/log/mysql/mysql-bin.log" 49 | - "max_binlog_size = 100M" 50 | - "relay_log = /var/log/mysql/mysql-relay-bin" 51 | - "relay_log_index = /var/log/mysql/mysql-relay-bin.index" 52 | 53 | - name: Delete Anonymous user 54 | community.mysql.mysql_user: 55 | name: '' 56 | host: localhost 57 | state: absent 58 | login_unix_socket: /run/mysqld/mysqld.sock 59 | 60 | - name: Set Root Password with defined variable 61 | community.mysql.mysql_user: 62 | name: "root" 63 | password: "{{root_password}}" 64 | host: "{{item}}" 65 | login_unix_socket: /run/mysqld/mysqld.sock 66 | with_items: 67 | - localhost 68 | 69 | - name: Add server-id line in my.cnf[Server 0] 70 | ansible.builtin.lineinfile: 71 | path: /etc/mysql/my.cnf 72 | regexp: "{{item}}" 73 | line: "{{item}}" 74 | state: present 75 | with_items: 76 | - "server-id = 1" 77 | when: inventory_hostname in groups["master"][0] 78 | 79 | - name: Add server-id line in my.cnf[Server 1] 80 | ansible.builtin.lineinfile: 81 | path: /etc/mysql/my.cnf 82 | regexp: "{{item}}" 83 | line: "{{item}}" 84 | state: present 85 | with_items: 86 | - "server-id = 2" 87 | when: inventory_hostname in groups["master"][1:] 88 | 89 | - name: "Create user dbrep for replication" 90 | community.mysql.mysql_user: 91 | name: "dbrep" 92 | password: "dbrep" 93 | priv: "*.*:REPLICATION SLAVE" 94 | host: "%" 95 | login_user: root 96 | login_password: "{{root_password}}" 97 | state: present 98 | 99 | - name: Restart and Enable MariaDB 100 | ansible.builtin.service: 101 | name: mysql 102 | state: restarted 103 | enabled: true 104 | 105 | - name: Get Master Status 106 | community.mysql.mysql_info: 107 | login_user: root 108 | login_password: "{{root_password}}" 109 | login_unix_socket: /run/mysqld/mysqld.sock 110 | register: master_status 111 | 112 | - name: Get Master Host 113 | command: echo "{{hostvars[inventory_hostname].inventory_hostname}}" 114 | register: master_host 115 | 116 | - name: Stop and then Reset Replication 117 | community.mysql.mysql_replication: 118 | mode: "{{item}}" 119 | login_user: root 120 | login_password: "{{root_password}}" 121 | login_host: localhost 122 | when: inventory_hostname in groups["master"] 123 | with_items: 124 | - stopreplica 125 | - resetreplica 126 | 127 | - name: Config Replication[Server 0] 128 | community.mysql.mysql_replication: 129 | mode: changeprimary 130 | login_user: root 131 | login_password: "{{root_password}}" 132 | login_host: localhost 133 | primary_host : "{{ hostvars[groups['master'].1].master_host.stdout }}" 134 | # primary_host : "172.17.132.107" 135 | primary_user : "dbrep" 136 | primary_password : "dbrep" 137 | primary_log_file : "{{ hostvars[groups['master'].1].master_status.master_status.File }}" 138 | primary_log_pos : "{{ hostvars[groups['master'].1].master_status.master_status.Position }}" 139 | when: inventory_hostname in groups["master"][0] 140 | 141 | - name: Config Replication[Server 1] 142 | community.mysql.mysql_replication: 143 | mode: changeprimary 144 | login_user: root 145 | login_password: "{{root_password}}" 146 | login_host: localhost 147 | primary_host : "{{ hostvars[groups['master'].0].master_host.stdout }}" 148 | # primary_host : "172.17.133.26" 149 | primary_user : "dbrep" 150 | primary_password : "dbrep" 151 | primary_log_file : "{{ hostvars[groups['master'].0].master_status.master_status.File }}" 152 | primary_log_pos : "{{ hostvars[groups['master'].0].master_status.master_status.Position }}" 153 | when: inventory_hostname in groups["master"][1:] 154 | 155 | - name: Start Replica on Master 0 156 | community.mysql.mysql_replication: 157 | mode: startreplica 158 | login_user: root 159 | login_password: "{{root_password}}" 160 | login_host: localhost 161 | when: inventory_hostname in groups["master"][0] 162 | 163 | - name: Start Replica on Master 1 164 | community.mysql.mysql_replication: 165 | mode: startreplica 166 | login_user: root 167 | login_password: "{{root_password}}" 168 | login_host: localhost 169 | when: inventory_hostname in groups["master"][1] 170 | 171 | - name: Create Database "{{database}}" 172 | community.mysql.mysql_db: 173 | name: "{{database}}" 174 | login_user: root 175 | login_password: "{{root_password}}" 176 | login_host: localhost 177 | when: inventory_hostname in groups["master"][0] 178 | 179 | - name: "Create user {{db_username}} for database {{database}}" 180 | community.mysql.mysql_user: 181 | name: "{{db_username}}" 182 | password: "{{db_password}}" 183 | priv: "{{database}}.*:ALL,GRANT" 184 | host: "%" 185 | login_user: root 186 | login_password: "{{root_password}}" 187 | state: present 188 | when: inventory_hostname in groups["master"][0] 189 | -------------------------------------------------------------------------------- /mariadb-single.yml: -------------------------------------------------------------------------------- 1 | - name: MariaDB Single Master Server Setup 2 | hosts: master 3 | vars: 4 | - root_password: rootpswd 5 | - db_username: user 6 | - db_password: password 7 | - database: newdb 8 | become: true 9 | 10 | tasks: 11 | 12 | - name: Run Apt Update 13 | ansible.builtin.apt: 14 | update_cache: true 15 | 16 | - name: Install Python and Pip for Ansible Script 17 | ansible.builtin.apt: 18 | name: 19 | - python3 20 | - python3-pip 21 | 22 | - name: Install Python Module for Ansible MariaDB Automation 23 | ansible.builtin.pip: 24 | name: 25 | - PyMySQL 26 | 27 | - name: MariaDB Installation 28 | ansible.builtin.apt: 29 | name: 30 | - mariadb-server 31 | - mariadb-client 32 | 33 | - name: add [mysqld] in my.cnf 34 | ansible.builtin.lineinfile: 35 | path: /etc/mysql/my.cnf 36 | regexp: "\\[mysqld]" 37 | line: "[mysqld]" 38 | state: present 39 | 40 | - name: Edit my.cnf 41 | ansible.builtin.lineinfile: 42 | path: /etc/mysql/my.cnf 43 | regexp: 'bind-address = 0.0.0.0' 44 | line: 'bind-address = 0.0.0.0' 45 | state: present 46 | 47 | - name: Restart and Enable MariaDB 48 | ansible.builtin.service: 49 | name: mysql 50 | state: restarted 51 | enabled: true 52 | 53 | - name: Delete Anonymous user 54 | community.mysql.mysql_user: 55 | name: '' 56 | host: localhost 57 | state: absent 58 | login_unix_socket: /run/mysqld/mysqld.sock 59 | 60 | - name: Set Root Password with defined variable 61 | community.mysql.mysql_user: 62 | name: "root" 63 | password: "{{root_password}}" 64 | host: "{{item}}" 65 | login_unix_socket: /run/mysqld/mysqld.sock 66 | with_items: 67 | - localhost 68 | 69 | - name: Create Database "{{database}}" 70 | community.mysql.mysql_db: 71 | name: "{{database}}" 72 | login_user: root 73 | login_password: "{{root_password}}" 74 | login_host: localhost 75 | 76 | - name: "Create user {{db_username}} for database {{database}}" 77 | community.mysql.mysql_user: 78 | name: "{{db_username}}" 79 | password: "{{db_password}}" 80 | priv: "{{database}}.*:ALL,GRANT" 81 | login_user: root 82 | login_password: "{{root_password}}" 83 | state: present 84 | 85 | --------------------------------------------------------------------------------