【实验需求】

1
创建MySQL的role,使用role进行批量安装MySQL

【实验步骤】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
[root@control ansible]# mkdir -pv /etc/ansible/roles/mysql/{files,tasks,vars}

[root@control ansible]# tree roles/mysql/
roles/mysql/
├── files
   ├── my.cnf
   └── mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz
├── tasks
   ├── ChangePassword.yml
   ├── configfile.yml
   ├── dir.yml
   ├── INIT_DATA.yml
   ├── install.yml
   ├── linkfile.yml
   ├── main.yml
   ├── modify.yml
   ├── PathVariable.yml
   ├── script.yml
   ├── service.yml
   ├── unarchive.yml
   └── user.yml
└── vars
└── main.yml

[root@control ansible]# cat /etc/ansible/roles/mysql/files/my.cnf
[client]
port=3306
socket=/data/mysql/mysql.sock
[mysql]
prompt="\\r:\\m:\\s(\\u@\\h) [\\d]>\\_"
[mysqld]
log-bin=/mysql/binlog/mysql-bin
server-id=1
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
symbolic-links=0
skip_name_resolve=1
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid

[root@control ansible]# vim /etc/ansible/roles/mysql/vars/main.yml
PackageDir: "/etc/ansible/roles/mysql/files"
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] }}"

[root@control mysql]# vim tasks/install.yml
- name: install packages
yum:
name:
- libaio
- numactl-libs
- libncurses*
state: present

[root@control mysql]# vim tasks/user.yml
- name: create user and group
user:
name: mysql
uid: 306
shell: /sbin/nologin
system: yes
create_home: no

[root@control mysql]# vim tasks/dir.yml
- 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

[root@control mysql]# vim tasks/unarchive.yml
- name: copy file to remote host
unarchive:
src: "{{ PackageDir }}/{{ PackageName }}{{ Suffix }}"
dest: "/usr/local/"
owner: root
group: root

[root@control mysql]# vim tasks/linkfile.yml
- name: create linkfile /usr/local/myql
file:
src: "/usr/local/{{ PackageName }}"
dest: /usr/local/mysql
owner: root
group: root
state: link

[root@control mysql]# vim tasks/PathVariable.yml
- name: PATH variable
copy:
content: 'PATH=/usr/local/mysql/bin:$PATH'
dest: /etc/profile.d/mysql.sh

[root@control mysql]# vim tasks/configfile.yml
- name: create config my.cnf
copy:
src: "{{ PackageDir }}/{{ ConfigFile }}"
dest: /etc/my.cnf

[root@control mysql]# vim tasks/modify.yml
- name: modify config file
lineinfile:
path: /etc/my.cnf
regexp: '^server-id='
line: "server-id={{ ID }}"

[root@control mysql]# vim tasks/script.yml
- name: service start script
shell: /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@control mysql]# vim tasks/INIT_DATA.yml
- name: initial mysql
shell: /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

[root@control mysql]# vim tasks/service.yml
- name: enables service
shell: chkconfig --add mysqld;chkconfig mysqld on;/etc/init.d/mysqld start

[root@control mysql]# vim tasks/ChangePassword.yml
- name: change password
shell: /usr/local/mysql/bin/mysqladmin -uroot password {{ MysqlRootPassword }}

[root@control mysql]# vim tasks/main.yml
- include: install.yml
- include: user.yml
- include: dir.yml
- include: unarchive.yml
- include: linkfile.yml
- include: PathVariable.yml
- include: configfile.yml
- include: modify.yml
- include: script.yml
- include: INIT_DATA.yml
- include: service.yml
- include: ChangePassword.yml

[root@control mysql]# vim /etc/ansible/role_mysql.yml
---
- hosts: dbservers
roles:
- mysql