2.Playbook安装MariaDB
2020-06-08 本文已影响0人
Stone_説
目录:
1.Ansible基本配置
2.Mysql编译安装回顾
3.Playbook编写及实现
1.Ansible基本配置
1.1 Ansible安装
[root@centosmin7 ~]# hostnamectl set-hostname ansible
[root@ansible ~]# yum install ansible
[root@ansible ~]# rpm -q ansible
ansible-2.9.9-1.el7.noarch
1.2 修改配置文件
添加主机组,添加两个主机组:
[root@ansible ~]# vim /etc/ansible/hosts
[websrvs]
192.168.177.133
192.168.177.134
[appsrvs]
192.168.177.137
192.168.177.138
[root@ansible ~]# vim /etc/ansible/ansible.cfg
[defaults]
host_key_checking = False # 检查对应服务器的host_key,建议取消注释,修改完配置文件之后,则没有输入yes界面出现
1.3 配置基于key验证
[root@ansible ~]# ansible all --list-hosts 查看主机
hosts (4):
192.168.177.133
192.168.177.134
192.168.177.137
192.168.177.138
[root@ansible ~]# ssh-keygen 配置key验证
[root@ansible ~]# ssh-copy-id 192.168.177.133
[root@ansible ~]# ssh-copy-id 192.168.177.134
[root@ansible ~]# ssh-copy-id 192.168.177.137
[root@ansible ~]# ssh-copy-id 192.168.177.138
1.4 验证Ansible配置
[root@ansible ~]# ansible all -m ping
192.168.177.133 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.177.138 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.177.134 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.177.137 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2.Mysql编译安装回顾
https://www.jianshu.com/p/4c00cc4c10b6
3.Playbook编写及实现
3.1 准备工作
[root@ansible ~]# mkdir -p /data/playbook
[root@ansible ~]# cd /data/playbook/
[root@ansible playbook]# tar xvf mariadb-10.2.25-linux-x86_64.tar.gz
[root@ansible playbook]# cd mariadb-10.2.25-linux-x86_64
[root@ansible mariadb-10.2.25-linux-x86_64]# pwd
/data/playbook/mariadb-10.2.25-linux-x86_64
[root@ansible mariadb-10.2.25-linux-x86_64]# cp support-files/my-huge.cnf /data/playbook/
[root@ansible mariadb-10.2.25-linux-x86_64]# vim /data/playbook/my-huge.cnf 准备配置文件
[mysqld]
datadir=/data/mysql
3.2 playbook
[root@ansible playbook]# vim install_mariadb.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: user
user: name=mysql system=yes home=/data/mysql create_home=no shell=/sbin/nologin
- name: unarchive
unarchive: src=/data/playbook/mariadb-10.2.25-linux-x86_64.tar.gz dest=/usr/local owner=root group=root
- name: mysql link
file: src=/usr/local/mariadb-10.2.25-linux-x86_64 dest=/usr/local/mysql state=link
- name: mysql datadir
shell: mkdir /data/mysql
- name: mysql datadir owner group
file: path=/data/mysql owner=mysql group=mysql
- name: mysql database
shell: chdir=/usr/local/mysql/ scripts/mysql_install_db --datadir=/data/mysql --user=mysql
- name: path var
copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
- name: config
copy: src=/data/playbook/my-huge.cnf dest=/etc/my.cnf
- name: service file
shell: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
- name: start service
shell: service mysqld start
3.3 playbook测试及运行
[root@ansible playbook]# ansible-playbook install_mariadb.yml
PLAY [websrvs] ***************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************
ok: [192.168.177.134]
ok: [192.168.177.133]
TASK [user] ******************************************************************************************************
changed: [192.168.177.134]
changed: [192.168.177.133]
TASK [unarchive] *************************************************************************************************
changed: [192.168.177.133]
changed: [192.168.177.134]
TASK [mysql link] ************************************************************************************************
changed: [192.168.177.133]
changed: [192.168.177.134]
TASK [mysql datadir] *********************************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use
command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.177.133]
changed: [192.168.177.134]
TASK [mysql datadir owner group] *********************************************************************************
changed: [192.168.177.133]
changed: [192.168.177.134]
TASK [mysql database] ********************************************************************************************
changed: [192.168.177.133]
changed: [192.168.177.134]
TASK [path var] **************************************************************************************************
changed: [192.168.177.133]
changed: [192.168.177.134]
TASK [config] ****************************************************************************************************
changed: [192.168.177.134]
changed: [192.168.177.133]
TASK [service file] **********************************************************************************************
changed: [192.168.177.133]
changed: [192.168.177.134]
TASK [start service] *********************************************************************************************
[WARNING]: Consider using the service module rather than running 'service'. If you need to use command because
service is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [192.168.177.133]
changed: [192.168.177.134]
PLAY RECAP *******************************************************************************************************
192.168.177.133 : ok=11 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.177.134 : ok=11 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3.4 服务器上测验
192.168.177.133:
[root@centosmin7 mysql]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 :::3306 :::*
[root@centosmin7 ~]# source /etc/profile.d/mysql.sh
[root@centosmin7 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.25-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
192.168.177.134:
[root@centosmin7 mysql]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 :::3306 :::*
[root@centosmin7 ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.25-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye