Day15_Ansible自动化部署
MGR剧本逐段解析
- 新开了三台虚拟机
[mgr]
192.168.146.135 anmaster
192.168.146.136 anslave1
192.168.146.137 anslave2
开始部分
- hosts: mgr #指定目标主机组定义全局变量
1
2
3
4
5
6
7vars:
mgr_group_name: "90676347-4eb2-425b-b12f-d69c739667c4"
mgr_group_seeds: "anmaster:33061,anslave1:33061,anslave2:33061"
mgr_server_id: "{{ (ansible_hostname | hash('md5') | int(16) | abs)%4294967295 + 1 }}"
mysql_repl_user: "mgruser"
mysql_repl_password: "123456"
mysql_new_password: '123456QWer~~'打印目标主机操作系统信息,包括操作系统家族、发行版、版本、内核、架构和主机名
1
2
3
4
5
6
7
8
9
10- name: 打印被控端主机信息
debug:
msg: |
OS_Family: {{ ansible_os_family }}
Distribution: {{ ansible_distribution }}
Version: {{ ansible_distribution_version }}
Major_Version: {{ ansible_distribution_major_version }}
Architecture: {{ ansible_architecture }}
Kernel: {{ ansible_kernel }}
Hostname: {{ ansible_hostname }}配置多种版本系统的mysql8.0的yum源(RockyLinux9,8,centos7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23- name: RockyLinux9配置mysql80的yum源
yum:
name: https://repo.mysql.com/mysql80-community-release-el9.rpm
state: present
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version == '9'
- name: RockyLinux8配置mysql80的yum源
yum:
name: https://repo.mysql.com/mysql80-community-release-el8.rpm
state: present
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version == '8'
- name: CentOS7配置mysql80的yum源
yum:
name: https://repo.mysql.com/mysql80-community-release-el7.rpm
state: present
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version == '7'安装Mysql8.0社区版
1
2
3
4- name: 安装mysql8.0社区版
yum:
name: 'mysql-community-server'
state: present在mysql配置文件/etc/my.cnf末尾插入一行,引入其他配置文件,mysql默认不引用,这个配置文件的上下一行都不能有数据,必须是空行
1
2
3
4
5
6
7
8
9
10
11
12
13- name: mysql主配置中引入子配置选项
lineinfile:
path: /etc/my.cnf
line: '!includedir /etc/my.cnf.d/'
insertafter: EOF
backup: yes
- name: mysql主配置中引入子配置选项 (注意前面一点要添加空行, !includedir 需要单独起一行)
lineinfile:
path: /etc/my.cnf
line: " "
insertafter: '!includedir /etc/my.cnf.d/'
backup: yes将mgr的模板配置文件,mgr.cnf.js复制到目标主机的/etc/my.cnf.d/30-mgr.cnf,剧本会默认读取剧本所在路径下的这个模板文件,所以这个模板文件要放在和剧本一样的目录下
1
2
3
4
5
6
7
8
9- name: 增加mgr配置文件
template:
src: mgr.cnf.j2
dest: /etc/my.cnf.d/30-mgr.cnf
owner: root
group: root
mode: 0644
backup: yes启动mysql服务,并开机自启
1
2
3
4
5- name: 启动mysql并开机自启
service:
name: mysqld
state: started
enabled: yes获取临时密码
1
2
3
4
5
6
7
8
9
10
11
12- name: 获取初始密码
shell: |
if [ -f /var/log/mysqld.log ];then
grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}'
elif [ -f /var/log/mysqld/error.log ]; then
grep 'temporary password' /var/log/mysqld/error.log | awk '{print $NF}'
else
sudo grep 'temporary password' /var/log/mysql/error.log | awk '{print $NF}'
fi
register: mysql_temp_password
changed_when: false
ignore_errors: yes打印临时密码
1
2
3
4- name: 打印临时密码
debug:
msg: '临时密码是: {{ mysql_temp_password.stdout }}'
when: mysql_temp_password.stdout != ""安装修改mysql密码所需要的python依赖库
1
2
3
4
5
6
7
8- name: 安装修改mysql密码的python依赖库
yum:
name:
- python3-pip
- mysql-connector-python3
- python3-PyMySQL
state: present
when: ansible_os_family == 'RedHat'修改密码并验证是否有效
1
2
3
4
5
6
7- name: 修改Mysql root密码
shell: |
mysql -uroot -p'{{ mysql_temp_password.stdout }}' -e 'set password="{{ mysql_new_password }}";' --connect-expired-password
mysql -uroot -p'{{ mysql_new_password }}' -e 'show databases;'
register: mysql_show_info
密码并验证是否有效创建复制用户并赋予权限
1
2
3
4
5
6
7
8
9
10
11
12- name: 创建MGR同步账号
mysql_user:
login_user: root
login_password: "{{mysql_new_password }}"
login_unix_socket: "/var/lib/mysql/mysql.sock"
name: "{{ mysql_repl_user }}"
host: "%"
password: "{{ mysql_repl_password }}"
plugin: "mysql_native_password"
priv: "*.*:REPLICATION SLAVE,REPLICATION CLIENT"
state: present
when: inventory_hostname == groups['mgr'][0]设置引导模式,允许组复制
1
2
3
4
5
6
7- name: 引导组复制-设置引导模式
mysql_query:
login_user: root
login_password: "{{ mysql_new_password }}"
login_unix_socket: "/var/lib/mysql/mysql.sock"
query: "set global group_replication_bootstrap_group=on;"
when: inventory_hostname == groups['mgr'][0]启动组复制
1
2
3
4
5
6
7- name: 引导组复制-启动组复制
mysql_query:
login_user: root
login_password: "{{ mysql_new_password }}"
login_unix_socket: "/var/lib/mysql/mysql.sock"
query: "start group_replication;"
when: inventory_hostname == groups['mgr'][0]关闭引导模式
1
2
3
4
5
6
7- name: 引导组复制-关闭引导模式
mysql_query:
login_user: root
login_password: "{{ mysql_new_password }}"
login_unix_socket: "/var/lib/mysql/mysql.sock"
query: "set global group_replication_bootstrap_group=off;"
when: inventory_hostname == groups['mgr'][0]设置从节点,配置恢复通道
1
2
3
4
5
6
7
8
9
10
11
12
13- name: 其他节点加入mgr
mysql_query:
login_user: root
login_password: "{{ mysql_new_password }}"
login_unix_socket: "/var/lib/mysql/mysql.sock"
query: "{{ item }}"
with_items:
- 'stop group_replication;'
- 'reset replica all;'
- 'reset master;'
- 'change replication source to source_user="mgruser",source_password="123456" for channel "group_replication_recovery";'
- 'start group_replication;'
when: inventory_hostname != groups['mgr'][0]获取MGR状态信息并打印
1
2
3
4
5
6
7
8
9
10
11
12- name: 获取MGR集群信息
mysql_query:
login_user: root
login_password: "{{ mysql_new_password }}"
login_unix_socket: "/var/lib/mysql/mysql.sock"
query: 'select member_host,member_state,member_role from performance_schema.replication_group_members;'
register: mysql_mgr_info
when: inventory_hostname == groups['mgr'][0]
- name: 显示MGR信息
debug:
var: "NGR集群信息:{{ mysql_mgr_info }}"
when: inventory_hostname == groups['mgr'][0]触发器,当其他模块使用notify: restart mysql时,会触发重启服务
1
2
3
4
5
6handlers:
- name: restart mysql
systemd:
name: "{{ 'mysql' if ansible_os_family == 'Debian' else 'mysqld' }}"
state: restarted
全部组合
1 | - hosts: mgr |
哨兵剧本解析
playbook
1 | - name: 哨兵模式部署 |






