1.ansible离线安装
ansible离线安装手册:https://blog.csdn.net/xzm5708796/article/details/89357434
# 下载安装包:
[root@ansible ~]# tar -xzvf ansible-2.4-rpms.el7.tar.gz
[root@ansible ~]# cd ansible-2.4-rpms.el7
[root@ansible ~]# rpm -ivh PyYAML*rpm libyaml*rpm python-babel*rpm python-backports*rpm python-backports-ssl_match_hostname*rpm python-cffi*rpm python-enum34*rpm python-httplib2*rpm python-idna*rpm python-ipaddress*rpm python-jinja2*rpm python-markupsafe*rpm python-paramiko*rpm python-passlib*rpm python-ply*rpm python-pycparser*rpm python-setuptools*rpm python-six*rpm python2-cryptography*rpm python2-jmespath*rpm python2-pyasn1*rpm sshpass*rpm --nodeps --force
[root@ansible ~]# rpm -ivh ansible-2.4.2.0-2.el7.noarch.rpm
# 测试安装是否成功
[root@ansible ~]# ansible --version
2.环境准备
(1)关闭所有节点防火墙和selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
getenforce
(2)准备playbook需要的文件
安装vsftpd,安装mariadb服务,修改域名解析配置文件,设置yum源配置文件(ftp方式)。
修改数据库配置文件my.cnf,在数据库配置文件中加入以下配置:
[mysqld]
log_bin = mysql-bin
binlog_ignore_db = mysql
server_id = 110 ##(一般是ip地址的最后一位)
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
相关配置文件可复制到工作目录供后续剧本使用:
[root@ansible ~]# ls
anaconda-ks.cfg ansible_work local.repo my.cnf
编写主机清单并设置ssh免密登录。
[root@ansible ~]# cat /etc/ansible/hosts | tail -5
## db-[99:101]-node.example.com
[host1]
192.168.100.111
[host2]
192.168.100.112
3.创建main文件和入口文件
[root@ansible ~]# mkdir /root/ansible_work # 创建ansible工作根目录
[root@ansible ~]# mkdir -p /root/ansible_work/roles/mariadb/tasks # 创建playbook的mariadb的role目录
[root@ansible ~]# touch /root/ansible_work/roles/mariadb/tasks/main.yaml # 创建mariadb的main.yaml文件
[root@ansible ~]# touch /root/ansible_work/wxit_install.yaml # 创建playbook-book入口文件
#验证目录结构
[root@ansible ~]# tree
.
├── anaconda-ks.cfg
├── ansible_work
│ ├── roles
│ │ └── mariadb
│ │ └── tasks
│ │ └── main.yaml
│ ├── wxit_install.retry
│ └── wxit_install.yaml
├── local.repo
└── my.cnf
编辑入口文件wxit_install.yaml
[root@ansible ~]# cat ansible_work/wxit_install.yaml
- hosts: host1
remote_user: root
roles:
- mariadb
- hosts: host2
remote_user: root
roles:
- mariadb
编辑main.yaml剧本文件
[root@ansible ~]# cat ansible_work/roles/mariadb/tasks/main.yaml
- name: cp hosts
copy: src=/etc/hosts dest=/etc/hosts
- name: clear repo
shell: rm -rf /etc/yum.repos.d/*
- name: cp repo
copy: src=/root/local.repo dest=/etc/yum.repos.d/
- name: install mariadb
shell: yum install mariadb mariadb-server -y
- name: start mariadb
shell: systemctl start mariadb
- name: password mariadb
shell: mysqladmin password 123456
- name: copy
copy: src=/root/my.cnf dest=/etc/my.cnf
when: ansible_hostname=='host1'
- name: restart
shell: systemctl restart mariadb
- name: grant all
shell: mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'%' identified by '123456';"
when: ansible_hostname=='host1'
- name: create user
shell: mysql -uroot -p123456 -e "grant replication slave on *.* to 'user'@'host2' identified by '123456';"
when: ansible_hostname=='host1'
- name: change master
shell: mysql -uroot -p123456 -e "change master to master_host='host1',master_user='user',master_password='123456';"
when: ansible_hostname=='host2'
- name: set server id
shell: mysql -uroot -p123456 -e " SET GLOBAL server_id=112;"
when: ansible_hostname=='host2'
#测试剧本
[root@ansible ~]# ansible-playbook -C /root/ansible_work/wxit_install.yaml
PLAY RECAP **********************************************************************************************************************************
192.168.100.111 : ok=4 changed=0 unreachable=0 failed=0
192.168.100.112 : ok=3 changed=0 unreachable=0 failed=0
#运行剧本(虚拟机可提前做好快照方便失败倒退)
[root@ansible ~]# ansible-playbook /root/ansible_work/wxit_install.yaml
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.100.111 : ok=11 changed=9 unreachable=0 failed=0
192.168.100.112 : ok=10 changed=9 unreachable=0 failed=0
4.登录数据库从节点运行命令查看结果
show slave status \G
[root@host2 ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> show slave stauts\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: host1
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000004
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 245
Relay_Log_Space: 1109
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 111
1 row in set (0.00 sec)
参考文档:
https://blog.csdn.net/weixin_42104231/article/details/87129068
https://blog.csdn.net/liumiaocn/article/details/95351475
https://blog.csdn.net/yanggd1987/article/details/78612249