安装Mysql之Playbook

时间:2022-10-07 14:01:08
---
- name: install mysql
hosts: all
vars:
mysql: mysql-8.0.28-linux-glibc2.12-x86_64
tasks:
- name: install package
yum:
name:
- libaio
- numactl-libs
state: present
when: ansible_distribution == "CentOS" or ansible_distribution == "Rocky"
- name: apt package
apt:
name: libtinfo5
update_cache: yes
state: present
when: ansible_distribution == "Ubuntu"
- name: create group
group:
name: mysql
system: yes
gid: 336
state: present
- name: create user
user:
name: mysql
group: mysql
system: yes
uid: 336
create_home: no
shell: /sbin/nologin
state: present
- name: copy package to remote
unarchive:
src: /data/ansible/files/{{ mysql }}.tar.xz
dest: /usr/local
owner: mysql
group: mysql
- name: create link
file:
src: /usr/local/{{ mysql }}
dest: /usr/local/mysql
state: link
owner: mysql
group: mysql
- name: path
copy:
content: "PATH=/usr/local/mysql/bin:$PATH"
dest: /etc/profile.d/mysql.sh
- name: copy configfile to remote
copy:
src: /data/ansible/files/my.cnf
dest: /etc/my.cnf
- name: create datadir
file:
path: /data/mysql
state: directory
owner: mysql
group: mysql
- name: data
shell: rm -rf /data/mysql/* ; /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql
- name: copy service to remote
copy:
src: /data/ansible/files/mysql.server
dest: /etc/init.d/mysqld
mode: '755'
- name: centos service
shell: "chkconfig --add mysqld ; systemctl start mysqld"
when: ansible_distribution == "CentOS" or ansible_distribution == "Rocky"
- name: ubuntu service
shell: "systemctl enable mysqld ; systemctl start mysqld"
when: ansible_distribution == "Ubuntu"