【实验需求】 1对 dbservers 组部署 MySQL5.7 【实验步骤】 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112# 准备相关文件[root@control ~]# mkdir -p /data/ansible/files/mysql/[root@control ~]# ls /data/ansible/files/mysql/my.cnf mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz[root@control ~]# cat /data/ansible/files/mysql/my.cnf [client]port=3306socket=/data/mysql/mysql.sock[mysql]prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"[mysqld]log-bin=/mysql/binlog/mysql-binserver-id=1datadir=/data/mysqlsocket=/data/mysql/mysql.sockuser=mysqlsymbolic-links=0skip_name_resolve=1log-error=/data/mysql/mysql.logpid-file=/data/mysql/mysql.pid# 准备playbook剧本[root@control ~]# cd /etc/ansible/[root@control ansible]# vim mysql57_install.yml---# install mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz- hosts: dbservers vars: PackageDir: "/data/ansible/files/mysql" PackageName: "mysql-5.7.40-linux-glibc2.12-x86_64" Suffix: ".tar.gz" ConfigFile: "my.cnf" MysqlRootPassword: wuhaolam ID: "{{ ansible_eth0.ipv4.address.split('.')[-1] }}" tasks: - name: install packages yum: name: - libaio - numactl-libs - libncurses* state: present - name: create user and group user: name: mysql uid: 306 shell: /sbin/nologin system: yes create_home: no - name: create mysql data dir file: path: /data/mysql state: directory owner: mysql group: mysql recurse: yes - name: create binlog file directory file: path: /mysql/binlog state: directory owner: mysql group: mysql recurse: yes - name: copy file to remote host unarchive: src: "{{ PackageDir }}/{{ PackageName }}{{ Suffix }}" dest: "/usr/local/" owner: root group: root - name: create linkfile /usr/local/myql file: src: "/usr/local/{{ PackageName }}" dest: /usr/local/mysql owner: root group: root state: link - name: PATH variable copy: content: 'PATH=/usr/local/mysql/bin:$PATH' dest: /etc/profile.d/mysql.sh - name: create config my.cnf copy: src: "{{ PackageDir }}/{{ ConfigFile }}" dest: /etc/my.cnf - name: modify config file lineinfile: path: /etc/my.cnf regexp: '^server-id=' line: "server-id={{ ID }}" - name: service start script shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld - name: initial mysql shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql - name: enables service shell: chkconfig --add mysqld;chkconfig mysqld on;/etc/init.d/mysqld start - name: change password shell: /usr/local/mysql/bin/mysqladmin -uroot password {{ MysqlRootPassword }}