ansible部署mysql一主一從
阿新 • • 發佈:2018-12-10
1.首先安裝ansible,倒騰了好幾天離線安裝,結果都是安失敗
yum install ansible
2.查詢安裝的ansible
find / -name ansible
安裝在/etc/ansible 3.進入ansible目錄,檢視安裝好的目錄結構
cd /etc/ansible
4.由於之前已經安裝過mysql,對於自動化部署的過程也就清晰了(先手動-ansible部署-寫程式碼) 目錄結構
roles —mysql_repl ——files ————mysql-5.7.23-1.el7.x86_64.rpm-bundle.tar ——handles ——tasks ————main.yml ——templates ——vars ————main.yml ansible.cfg hosts(存放機器的ip) test.yml(主入口) 5.編輯host
最後一行加上
[mysql_slave]
192.168.236.9 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=xuqin1993
[mysql_master]
192.168.236.10 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=xuqin1993
6.編輯test.yml
- hosts: mysql_master roles: - role: mysql_repl - hosts: mysql_slave roles: - role: mysql_repl
7.在/etc/ansible/roles/mysql_repl/files下放mysql的.tar檔案 8.在/etc/ansible/roles/mysql_repl/tasks的main.yml編輯
- name: tar mysql.tar shell: tar -xvf {{mysql_dir}}/{{mysql_version}}.tar -C {{ mysql_dir }}/ - name: install community shell: rpm -ivh {{mysql_dir}}/mysql-community-common-5.7.23-1.el7.x86_64.rpm - name: install libs shell: rpm -ivh {{mysql_dir}}/mysql-community-libs-5.7.23-1.el7.x86_64.rpm - name: install libs-compat shell: rpm -ivh {{mysql_dir}}/mysql-community-libs-compat-5.7.23-1.el7.x86_64.rpm - name: install client shell: rpm -ivh {{mysql_dir}}/mysql-community-client-5.7.23-1.el7.x86_64.rpm - name: install service shell: rpm -ivh {{mysql_dir}}/mysql-community-server-5.7.23-1.el7.x86_64.rpm --force --nodeps - name: start mysqld service: name: mysqld state: started
9./etc/ansible/roles/mysql_repl/vars下的main.yml
mysql_version: mysql-5.7.23-1.el7.x86_64.rpm-bundle
mysql_dir: /etc/ansible/roles/mysql_repl/files
10.執行ansible-playbook test.yml 11.成功結果 補充:不刪除centos 7預設是mariadb資料庫,安裝libs會報錯,先手動刪除兩臺機器的mariadb(版本不一樣,無法統一執行)