标签:ansible-playbook自动部署mysql的主从复制读写分离
目录结构: Ansible-mysql-replication ├── group_vars │ ├── all │ ├── mysql-proxy │ └── mysql-slave ├── hosts ├── mysql-replication.retry ├── mysql-replication.yml └── roles ├── amoeba │ ├── files │ │ ├── amoeba-mysql-3.0.5-RC-distribution.zip │ │ └── jdk-7u79-linux-x64.tar.gz │ ├── handlers │ ├── tasks │ │ ├── jdk-7u79-linux-x64.tar.gz │ │ └── main.yaml │ └── templates │ ├── amoeba.xml │ ├── dbServers.xml │ ├── jvm.properties │ └── profile ├── base │ ├── files │ │ ├── epel-release-6-8.noarch.rpm │ │ ├── epel-release-latest-7.noarch.rpm │ │ └── remi-release-6.rpm │ └── tasks │ └── main.yaml ├── chpass │ └── tasks │ └── main.yaml ├── mysql-master │ ├── handlers │ │ └── main.yaml │ ├── tasks │ │ └── main.yaml │ └── templates │ └── server.cnf.j2 └── mysql-slave ├── handlers │ └── main.yaml ├── tasks │ └── main.yml └── templates └── server.cnf.j2 mysql-replication.yml --- - name: Base All Server hosts: all remote_user: root tags: base roles: - base - name: Install mysql-master and configure mysqlmaster hosts: mysql-master remote_user: root tags: mysql-master roles: - mysql-master - name: Install mysql-slave and start mysqlslave hosts: mysql-slave remote_user: root tags: mysql-slave roles: - mysql-slave - name: Install Amoeba and configure hosts: mysql-proxy remote_user: root tags: mysql-proxy roles: - amoeba - name: chnage mysql root pass hosts: mysql-master,mysql-slave remote_user: root tags: chpass roles: - mysql-conf roles/amoeba/tasks/main.yml --- - name: Install unzip yum: name=unzip state=installed - name: unzip Amobea and Jave Package unarchive: src={{ item }} dest=/usr/local/ with_items: - amoeba-mysql-3.0.5-RC-distribution.zip - jdk-7u79-linux-x64.tar.gz tags: unzip - name: Copy profile template: src=profile dest=/etc/profile tags: copy - name: Source profile shell: source /etc/profile - name: Copy Amoeba Template Configure File template: src={{ item }} dest=/usr/local/amoeba-mysql-3.0.5-RC/conf/ with_items: - dbServers.xml - amoeba.xml - name: Copy Amoeba jvm Configure File template: src=jvm.properties dest=/usr/local/amoeba-mysql-3.0.5-RC/jvm.properties - name: Start Amoeba shell: /usr/local/amoeba-mysql-3.0.5-RC/bin/launcher & roles\base\tasks\main.yaml -- - name: Copy Centos6&&Crntos7 EPEL copy: src={{ item }} dest=/opt/ with_items: - epel-release-6-8.noarch.rpm - remi-release-6.rpm - epel-release-latest-7.noarch.rpm - name: Install ELEP for Centos6 yum: name=/opt/{{ item }} state=installed with_items: - epel-release-6-8.noarch.rpm - remi-release-6.rpm when: ansible_distribution_major_version == "6" - name: Install ELEP for Centos6 yum: name=/opt/epel-release-latest-7.noarch.rpm state=installed when: ansible_distribution_major_version == "7" - name: Install MariaDB package yum: name={{ item }} state=installed with_items: - mariadb-server - mysql - MySQL-python - name: Create MariaDB log file file: path=/var/log/mysqld.log state=touch owner=mysql group=mysql mode=0755 - name: Create MariaDB PID directory file: path=/var/run/mysqld state=directory owner=mysql group=mysql mode=0755 - name: Start mariadb service: name=mariadb state=started enabled=yes when: ansible_distribution_major_version == "7" - name: Start mariadb service: name=mysql state=started enabled=yes when: ansible_distribution_major_version == "6" roles\mysql-master\tasks\main.yaml - name: Create mysql configure file template: src=server.cnf.j2 dest=/etc/my.cnf.d/server.cnf notify: - restart mariadb #- name: Change root login_password # shell: /usr/bin/mysqladmin -uroot password "mysql" - name: Create Replication User mysql_user: name={{ dbuser }} password={{ dbuserpass }} priv=*.*:ALL host={{ item }} state=present with_items: - ‘%‘ - ‘localhost‘ roles\mysql-master\handlers\main.yaml --- - name: restart mariadb service: name=mariadb state=started enabled=yes when: ansible_distribution_major_version == "7" - name: restart mariadb service: name=mysql state=started enabled=yes when: ansible_distribution_major_version == "6" roles\mysql-slave\handlers\main.yaml --- - name: restart mariadb service: name=mariadb state=started enabled=yes when: ansible_distribution_major_version == "7" - name: restart mariadb service: name=mysql state=started enabled=yes when: ansible_distribution_major_version == "6" roles\mysql-slave\tasks\main.yml --- - name: Create mysql configure file template: src=server.cnf.j2 dest={{ mycnf_pwd }} notify: - restart mariadb - name: Get Mysql-master Status mysql_replication: mode=getmaster delegate_to: "{{ master_host }}" register: master - name: Configure replication on the slave. mysql_replication: mode=changemaster master_host={{ master_host }} master_user={{ dbuser }} master_password={{ dbuserpass }} master_log_file={{ master.File }} master_log_pos={{ master.Position }} - name: Start slave mysql_replication: mode=startslave roles\mysql-conf\tasks\main.yaml -- - name: Create Mysql-Proxy User mysql_user: name={{ mysqluser }} password={{ mysqlpass }} priv=*.*:ALL,GRANT host={{ item }} state=present with_items: - ‘%‘ - ‘localhost‘ - name: Change master&slave mysql root password shell: /usr/bin/mysqladmin -uroot password "{{ root_pass }}" host: [mysql-master] 172.17.0.2 [mysql-slave] 172.17.0.6 [mysql-proxy] 172.17.0.8 在host文件中定义mysql主服务器,从服务器,以及转发服务器ip 在templates放入配置文件模板 ansible-playbook -i host mysql-replication.yml 完整剧本请访问https://github.com/chulinx/Ansible-playbook/tree/master/Ansible-mysql-replication
本文出自 “技术宅私有空间” 博客,请务必保留此出处http://chulinx.blog.51cto.com/4098114/1842061
Ansible-playbook自动部署mysql的主从复制读写分离
标签:ansible-playbook自动部署mysql的主从复制读写分离
原文地址:http://chulinx.blog.51cto.com/4098114/1842061