提交各组件部署Ansible剧本初版
This commit is contained in:
153
MariaDB/10.5.3/mariadb/role/tasks/deploy-cluster.yml
Normal file
153
MariaDB/10.5.3/mariadb/role/tasks/deploy-cluster.yml
Normal file
@@ -0,0 +1,153 @@
|
||||
- name: Setting node_nums variable
|
||||
set_fact: node_nums={{ groups.mariadb|length }}
|
||||
|
||||
- name: To terminate execution
|
||||
fail:
|
||||
msg: "MariaDB in master-master mode. The value must have 2 nodes,please checking configurations/hosts -> mariadb"
|
||||
when: node_nums != '2'
|
||||
|
||||
- name: Creating directory
|
||||
file:
|
||||
state: directory
|
||||
owner: ods
|
||||
group: root
|
||||
path: '{{ deploy_dir }}/{{ container_name }}/{{ item.dir }}'
|
||||
with_items:
|
||||
- { dir: 'config' }
|
||||
- { dir: 'logs' }
|
||||
- { dir: 'monitor' }
|
||||
|
||||
- name: Copying image to {{ deploy_dir }}/{{ container_name }}/
|
||||
copy:
|
||||
src: 'files/{{ image_name }}-{{ image_tag }}.tar'
|
||||
dest: '{{ deploy_dir }}/{{ container_name }}/'
|
||||
force: true
|
||||
notify:
|
||||
- Loading Image
|
||||
|
||||
- name: Copying image to {{ deploy_dir }}/{{ container_name }}/monitor
|
||||
copy:
|
||||
src: 'files/mysqld_exporter-v1.0.tar'
|
||||
dest: '{{ deploy_dir }}/{{ container_name }}/monitor/'
|
||||
force: true
|
||||
notify:
|
||||
- Loading Exporter Image
|
||||
|
||||
- name: Copying Mariadb config files
|
||||
template:
|
||||
src: '{{ item.src }}'
|
||||
dest: '{{ item.dest }}'
|
||||
mode: 0644
|
||||
with_items:
|
||||
- { src: 'my.cnf.j2', dest: '{{ deploy_dir }}/{{ container_name }}/config/my.cnf' }
|
||||
- { src: 'docker-compose.yml.j2', dest: '{{ deploy_dir }}/{{ container_name }}/docker-compose.yml' }
|
||||
notify:
|
||||
- Start Container
|
||||
|
||||
- meta: flush_handlers
|
||||
|
||||
- name: Waitting for MariaDB running,20s
|
||||
shell: sleep 20
|
||||
|
||||
- name: Creating mariadb readonly user
|
||||
shell: mysql -uroot -h{{ inventory_hostname }} -p{{ mariadb_default_pin }} -e "CREATE USER IF NOT EXISTS '{{ mariadb_query_username}}'@'%' IDENTIFIED BY '{{ mariadb_query_pin }}' WITH MAX_USER_CONNECTIONS 3;GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO '{{ mariadb_query_username}}'@'%';FLUSH PRIVILEGES;"
|
||||
|
||||
- name: change mariadb remote authority
|
||||
shell: mysql -uroot -h{{ inventory_hostname }} -p{{ mariadb_default_pin }} -e"use mysql;grant all privileges on *.* to 'root'@'%' identified by '{{ mariadb_default_pin }}' with grant option;FLUSH PRIVILEGES;"
|
||||
|
||||
- name: copy pyMysql.zip
|
||||
unarchive:
|
||||
src: files/pyMysql.zip
|
||||
dest: /tmp/
|
||||
register: copy_info
|
||||
|
||||
- name: pip install pyMysql
|
||||
shell: cd /tmp/pyMysql && pip install --ignore-installed *
|
||||
when: copy_info.changed
|
||||
|
||||
- name: Get mariadb slave_status
|
||||
mysql_info:
|
||||
login_host: '{{ groups.mariadb[0] }}'
|
||||
login_user: root
|
||||
login_password: '{{ mariadb_default_pin }}'
|
||||
filter: slave_status
|
||||
register: mariadb_info
|
||||
|
||||
- name: change_slave_to_master1
|
||||
mysql_replication:
|
||||
login_unix_socket: "{{ deploy_dir }}/{{ container_name }}/data/mysql.sock"
|
||||
login_host: "{{ groups.mariadb[1] }}"
|
||||
login_port: "3306"
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_default_pin }}"
|
||||
master_host: "{{ groups.mariadb[0] }}"
|
||||
master_user: root
|
||||
master_password: "{{ mariadb_default_pin }}"
|
||||
master_port: "3306"
|
||||
mode: changemaster
|
||||
run_once: true
|
||||
delegate_to: '{{ groups.mariadb[1] }}'
|
||||
tags:
|
||||
- change_slave_to_master1
|
||||
|
||||
- name: start_slave1
|
||||
mysql_replication:
|
||||
login_unix_socket: "{{ deploy_dir }}/{{ container_name }}/data/mysql.sock"
|
||||
login_user: root
|
||||
login_host: "{{ inventory_hostname }}"
|
||||
login_port: "3306"
|
||||
login_password: "{{ mariadb_default_pin }}"
|
||||
mode: startslave
|
||||
tags:
|
||||
- start_slave1
|
||||
run_once: true
|
||||
delegate_to: '{{ groups.mariadb[1] }}'
|
||||
|
||||
- name: change_slave_to_master2
|
||||
mysql_replication:
|
||||
login_unix_socket: "{{ deploy_dir }}/{{ container_name }}/data/mysql.sock"
|
||||
login_host: "{{ groups.mariadb[0] }}"
|
||||
login_port: "3306"
|
||||
login_user: root
|
||||
login_password: "{{ mariadb_default_pin }}"
|
||||
master_host: "{{ groups.mariadb[1] }}"
|
||||
master_user: root
|
||||
master_password: "{{ mariadb_default_pin }}"
|
||||
master_port: "3306"
|
||||
mode: changemaster
|
||||
run_once: true
|
||||
delegate_to: '{{ groups.mariadb[0] }}'
|
||||
tags:
|
||||
- change_slave_to_master2
|
||||
|
||||
- name: start_slave2
|
||||
mysql_replication:
|
||||
login_unix_socket: "{{ deploy_dir }}/{{ container_name }}/data/mysql.sock"
|
||||
login_user: root
|
||||
login_host: "{{ inventory_hostname }}"
|
||||
login_port: "3306"
|
||||
login_password: "{{ mariadb_default_pin }}"
|
||||
mode: startslave
|
||||
tags:
|
||||
- start_slave2
|
||||
run_once: true
|
||||
delegate_to: '{{ groups.mariadb[0] }}'
|
||||
|
||||
- name: get_slave_info
|
||||
mysql_replication:
|
||||
login_host: 127.0.0.1
|
||||
login_user: root
|
||||
login_port: "3306"
|
||||
login_password: "{{ mariadb_default_pin }}"
|
||||
mode: getslave
|
||||
register: info
|
||||
tags:
|
||||
- get_slave_info
|
||||
|
||||
- name: Copying Mariadb config files
|
||||
template:
|
||||
src: 'exporter_docker-compose.yml.j2'
|
||||
dest: '{{ deploy_dir }}/{{ container_name }}/monitor/docker-compose.yml'
|
||||
mode: 0644
|
||||
notify:
|
||||
- Start Exporter Container
|
||||
66
MariaDB/10.5.3/mariadb/role/tasks/deploy-standalone.yml
Normal file
66
MariaDB/10.5.3/mariadb/role/tasks/deploy-standalone.yml
Normal file
@@ -0,0 +1,66 @@
|
||||
- name: Setting node_nums variable
|
||||
set_fact: node_nums={{ groups.mariadb|length }}
|
||||
|
||||
- name: To terminate execution
|
||||
fail:
|
||||
msg: "MariaDB standalone mode. The value must have 1 nodes,please checking configurations/hosts -> mariadb"
|
||||
when: node_nums != '1'
|
||||
|
||||
- name: Creating directory
|
||||
file:
|
||||
state: directory
|
||||
owner: ods
|
||||
group: root
|
||||
path: '{{ deploy_dir }}/{{ container_name }}/{{ item.dir }}'
|
||||
with_items:
|
||||
- { dir: 'config' }
|
||||
- { dir: 'logs' }
|
||||
- { dir: 'monitor' }
|
||||
|
||||
- name: Copying image to {{ deploy_dir }}/{{ container_name }}/
|
||||
copy:
|
||||
src: 'files/{{ image_name }}-{{ image_tag }}.tar'
|
||||
dest: '{{ deploy_dir }}/{{ container_name }}/'
|
||||
force: true
|
||||
notify:
|
||||
- Loading Image
|
||||
|
||||
- name: Copying image to {{ deploy_dir }}/{{ container_name }}/monitor
|
||||
copy:
|
||||
src: 'files/mysqld_exporter-v1.0.tar'
|
||||
dest: '{{ deploy_dir }}/{{ container_name }}/monitor/'
|
||||
force: true
|
||||
notify:
|
||||
- Loading Exporter Image
|
||||
|
||||
- name: Copying Mariadb config files
|
||||
template:
|
||||
src: '{{ item.src }}'
|
||||
dest: '{{ item.dest }}'
|
||||
mode: 0644
|
||||
with_items:
|
||||
- { src: 'my.cnf.j2', dest: '{{ deploy_dir }}/{{ container_name }}/config/my.cnf' }
|
||||
- { src: 'docker-compose.yml.j2', dest: '{{ deploy_dir }}/{{ container_name }}/docker-compose.yml' }
|
||||
notify:
|
||||
- Start Container
|
||||
|
||||
- meta: flush_handlers
|
||||
|
||||
- name: Waitting for MariaDB running,20s
|
||||
shell: sleep 20
|
||||
|
||||
- name: Creating mariadb readonly user
|
||||
shell: mysql -uroot -h{{ inventory_hostname }} -p{{ mariadb_default_pin }} -e "CREATE USER IF NOT EXISTS '{{ mariadb_query_username}}'@'%' IDENTIFIED BY '{{ mariadb_query_pin }}' WITH MAX_USER_CONNECTIONS 3;GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO '{{ mariadb_query_username}}'@'%';FLUSH PRIVILEGES;"
|
||||
|
||||
- name: change mariadb remote authority
|
||||
shell: mysql -uroot -h{{ inventory_hostname }} -p{{ mariadb_default_pin }} -e"use mysql;grant all privileges on *.* to 'root'@'%' identified by '{{ mariadb_default_pin }}' with grant option;FLUSH PRIVILEGES;"
|
||||
|
||||
- name: Copying Mariadb config files
|
||||
template:
|
||||
src: 'exporter_docker-compose.yml.j2'
|
||||
dest: '{{ deploy_dir }}/{{ container_name }}/monitor/docker-compose.yml'
|
||||
mode: 0644
|
||||
notify:
|
||||
- Start Exporter Container
|
||||
|
||||
- meta: flush_handlers
|
||||
11
MariaDB/10.5.3/mariadb/role/tasks/main.yml
Normal file
11
MariaDB/10.5.3/mariadb/role/tasks/main.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
- block:
|
||||
- include: uninstall.yml
|
||||
- include: "{{ playbook_name }}"
|
||||
vars:
|
||||
playbook_name: "{{ 'deploy-cluster.yml' if groups.mariadb | length > 1 else 'deploy-standalone.yml' }}"
|
||||
- include: status-check.yml
|
||||
when: (operation) == "install"
|
||||
|
||||
- block:
|
||||
- include: uninstall.yml
|
||||
when: (operation) == "uninstall"
|
||||
14
MariaDB/10.5.3/mariadb/role/tasks/status-check.yml
Normal file
14
MariaDB/10.5.3/mariadb/role/tasks/status-check.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
- name: Check if the MariaDB already exists
|
||||
shell: ps -ef | grep -v mysqld_exporter | grep -v grep | grep mysqld | wc -l
|
||||
register: process_out
|
||||
|
||||
- name: Check if the MariaDB already exists
|
||||
shell: netstat -anlp | egrep "3306" | grep LISTEN | wc -l
|
||||
register: port_out
|
||||
|
||||
- name: To terminate execution
|
||||
fail:
|
||||
msg: "MariaDB on node {{ inventory_hostname }} is not started. Please check"
|
||||
run_once: true
|
||||
delegate_to: 127.0.0.1
|
||||
when: process_out.stdout != '1' or port_out.stdout != '1'
|
||||
27
MariaDB/10.5.3/mariadb/role/tasks/uninstall.yml
Normal file
27
MariaDB/10.5.3/mariadb/role/tasks/uninstall.yml
Normal file
@@ -0,0 +1,27 @@
|
||||
- block:
|
||||
- name: Stopping and removing {{ container_name }} container
|
||||
docker_container:
|
||||
name: '{{ container_name }}'
|
||||
state: absent
|
||||
|
||||
- name: Removing old {{ image_name }} image
|
||||
docker_image:
|
||||
name: '{{ image_name }}'
|
||||
tag: '{{ image_tag }}'
|
||||
state: absent
|
||||
|
||||
- name: Stopping and removing exporter container
|
||||
docker_container:
|
||||
name: 'mysqld_exporter'
|
||||
state: absent
|
||||
|
||||
- name: Removing old exporter image
|
||||
docker_image:
|
||||
name: 'mysqld_exporter'
|
||||
tag: 'v1.0'
|
||||
state: absent
|
||||
|
||||
- name: Ansible delete old {{ deploy_dir }}/{{ container_name }}
|
||||
file:
|
||||
path: '{{ deploy_dir }}/{{ container_name }}'
|
||||
state: absent
|
||||
Reference in New Issue
Block a user