利用ansible的playbook在centos7上批量部署Mysql5.7或8.0

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用ansible的playbook在centos7上批量部署Mysql5.7或8.0相关的知识,希望对你有一定的参考价值。

利用register注册变量批量部署mysql5.7或8.0

[root@cent7_yzil ~]# mkdir -p /data/ansible/files

[root@cent7_yzil ~]# cd /data/ansible

[root@cent7_yzil ansible]# tree
.
├── files
│   ├── my.cnf
│   ├── mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
│   └── mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
└── install_mysql5.7or8.0.yml

1 directory, 4 files
[root@cent7_yzil ansible]# 

[root@cent7_yzil ansible]# cat files/my.cnf 
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock

log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[client]
socket=/data/mysql/mysql.sock
[root@cent7_yzil ansible]# 
[root@cent7_yzil ansible]# cat install_mysql5.7or8.0.yml 
---
#install mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
#install mysql-8.0.23-linux-glibc2.12-x86_64.tar.xz
- hosts: webservers
  remote_user: root
  gather_facts: no
  vars:
          mysql_version: 5.7.30
          mysql_file: mysql-{{mysql_version}}-linux-glibz2.12-x86_64.tar.gz
          mysql_root_password: redhat

  tasks:
          - name: install packages
            yum:
                    name:
                            - libaio
                            - numactl-libs
                            - MySQL-python
                    state: latest
          - name: create mysql group
            group: name=mysql gid=3306
          - name: create mysql user
            user: name=mysql uid=306 group=mysql shell=/sbin/nologin system=yes crate_home=no home=/data/mysql
          - name: copy tar to remote host and file mode
            unarchive: src=/data/ansible/files/{{mysql_file}} dest=/usr/local/ owner=root group=root
          - name: create linkfile /usr/local/mysql
            file: src=/usr/local/mysql-{{ mysql_version }}-linux-glib2.12-x86_64 dest=/usr/local/mysql state=link
          - name: data dir
            shell: /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/data/mysql
            tags: data
          - name: config my.cnf
            copy: src=/data/ansible/files/my.cnf dest=/etc/my.cnf
          - name: service script
            shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
          - name: PATH variable
            copy: content='PATH=/usr/local/mysql/bin:$PATH' dest=/etc/profile.d/mysql.sh
          - name: enable service
            shell: chkconfig --add mysqld; /etc/init.d/mysqld start
            tags: service
          - name: get password
            shell: awk '/A temporary password/{print $NF}' /data/mysql/mysql.log
            register: password
          - name: change password
            #debug:
            # msg: "{{ password.stdout }}"
            shell: /usr/local/mysql/bin/mysqladmin -uroot -p '{{password.stdout}}' password {{mysql_root_password}}
[root@cent7_yzil ansible]# 
[root@cent7_yzil ansible]# ansible-playbook install_mysql5.7or8.0.yml 

#注意:第一次执行无法修改密码,需要在目标主机上执行下面操作再一次playbook

[root@cent7_yzil ansible]# ansible webservers -m shell -a "service mysqld stop;rm -rf /data/mysql/*"

#再次执行成功

[root@cent7_yzil ansible]# ansible-playbook install_mysql5.7or8.0.yml 

#注意只能在centos7上用,centos8上面缺少 MySQL-python 这个包

[root@cent8_yzl ~]# yum info MySQL-python
Last metadata expiration check: 0:29:10 ago on Tue 01 Jun 2021 07:40:38 AM EDT.
Error: No matching Packages to list

 

以上是关于利用ansible的playbook在centos7上批量部署Mysql5.7或8.0的主要内容,如果未能解决你的问题,请参考以下文章

Ansible-playbook服务器初始化

ansible-playbook剧本初体验

sh 充分利用Ansible和Ansible剧本。请参阅http://docs.ansible.com/ansible/playbooks_best_practices.html

33-5 ansible playbook组件:任务列表handlers案例

利用ansible的playbook实现批量编译安装部署httpd-2.4

Ansible playbook复用 - -01