ansible-playbook實現MySQL的二進位制部署
阿新 • • 發佈:2021-12-23
1、ansible伺服器配置
1.1 安裝ansible
# yum -y install ansible
1.2 配置主機清單檔案
# vi /etc/ansible/hosts
[local]
10.0.0.7 ansible_connection=local #指定連線型別為本地,無需通過ssh連線
[mysql]
10.0.0.17
10.0.0.27
10.0.0.37
1.3 mysql配置檔案
# cat /apps/mysql/my.cnf [mysqld] user=mysql datadir=/data/mysql socket=/data/mysql/mysql.sock innodb_file_per_table=on skip_name_resolve = on #禁止主機名解析,建議使用 [client] port=3306 socket=/data/mysql/mysql.sock [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/data/mysql/mysql.pid
2、ssh免密授權
2.1 使用指令碼實現
# bash ssh_key.sh Generating public/private rsa key pair. Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:laHw87m60FI35AeBGdv5NhU8PW4Ol77WFPssLZK+LEY root@7-1 The key's randomart image is: +---[RSA 2048]----+ | . .+o ... | | oo+ = oo.| | = B o.o| | * + o * | | S * = * o| | o .E= . +.| | o ... . ++| | o .o.o oo=| | oo o+o.o | +----[SHA256]-----+ sshpass-1.06-2.el7.x86_64 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys Number of key(s) added: 1 Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking=no' '10.0.0.7'" and check to make sure that only the key(s) you wanted were added. ssh: connect to host 10.0.0.3 port 22: Connection refused lost connection ssh: connect to host 10.0.0.3 port 22: Connection refused lost connection known_hosts 100% 1195 619.5KB/s 00:00 known_hosts 100% 1195 1.1MB/s 00:00 known_hosts 100% 1195 604.3KB/s 00:00 known_hosts 100% 1195 1.8MB/s 00:00 known_hosts 100% 1195 1.6MB/s 00:00 known_hosts 100% 1195 1.5MB/s 00:00 known_hosts
2.2 ssh健康性檢查
# ansible mysql -m ping 10.0.0.7 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 10.0.0.17 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 10.0.0.37 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 10.0.0.27 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"
3、批量安裝mysql
# cat install-bin-mysql5.6.yml
---
# 批量安裝二進位制mysql5.6
# 將配置檔案my.cnf放到目錄/apps/mysql下
- hosts: mysql
remote_user: root
gather_facts: no
tasks:
- name: install packages
yum : name=libaio,perl-Data-Dumper,autoconf state=installed
- name: create group mysql
group:
name: mysql
gid: 306
system: yes
- name: create user mysql
user:
name: mysql
uid: 306
group: mysql
shell: /sbin/nologin
system: yes
home: /data/mysql
- name: download mysql_file
unarchive :
src: "http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz"
dest: "/usr/local"
owner: root
remote_src: yes
- name: prepare Soft links
shell: ln -s mysql-5.6.51-linux-glibc2.12-x86_64/ mysql
args:
chdir: "/usr/local"
- name: bash mysql_instll_db
shell: ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql/
args:
chdir: "/usr/local/mysql"
- name: prepare my.cnf
copy:
src: "/apps/mysql/my.cnf"
dest: "/etc/my.cnf"
- name: prepare service file
shell: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && chkconfig --add mysqld && chkconfig mysqld on
- name: add path
shell: echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh && . /etc/profile.d/mysql.sh
- name: start mysql
shell: service mysqld start
# ansible-playbook --syntax-check install-bin-mysql5.6.yml #檢查語法
# ansible-playbook install-bin-mysql5.6.yml #執行
PLAY [mysql] ******************************************************************************
TASK [install packages] *******************************************************************
changed: [10.0.0.17]
changed: [10.0.0.37]
changed: [10.0.0.27]
TASK [create group mysql] *****************************************************************
changed: [10.0.0.17]
changed: [10.0.0.27]
changed: [10.0.0.37]
TASK [create user mysql] ******************************************************************
changed: [10.0.0.37]
changed: [10.0.0.17]
changed: [10.0.0.27]
TASK [download mysql_file] ****************************************************************
changed: [10.0.0.17]
changed: [10.0.0.27]
changed: [10.0.0.37]
TASK [prepare Soft links] *****************************************************************
[WARNING]: Consider using the file module with state=link rather than running 'ln'. 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: [10.0.0.27]
changed: [10.0.0.37]
changed: [10.0.0.17]
TASK [bash mysql_instll_db] ***************************************************************
changed: [10.0.0.37]
changed: [10.0.0.27]
changed: [10.0.0.17]
TASK [prepare my.cnf] *********************************************************************
changed: [10.0.0.37]
changed: [10.0.0.17]
changed: [10.0.0.27]
TASK [prepare service file] ***************************************************************
changed: [10.0.0.17]
changed: [10.0.0.27]
changed: [10.0.0.37]
TASK [add path] ***************************************************************************
changed: [10.0.0.27]
changed: [10.0.0.17]
changed: [10.0.0.37]
TASK [start mysql] ************************************************************************
[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: [10.0.0.37]
changed: [10.0.0.17]
changed: [10.0.0.27]
PLAY RECAP ********************************************************************************
10.0.0.17 : ok=10 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.27 : ok=10 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.0.0.37 : ok=10 changed=10 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0