/
main.yml
124 lines (96 loc) · 3.56 KB
/
main.yml
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
---
# Playbook for MySQL servers with replication
#
# Install packages
#
- name: create directory to store rpms
file: path={{ rpm_download_dir}} state=directory
- name: download mysql rpms
get_url: url={{ mysql_url }}/{{ item }} dest={{ rpm_download_dir }}/{{ item }}
with_items: mysql_rpms
- name: make rpm paths
shell: echo {{ rpm_download_dir }}/{{ item }}
with_items: mysql_rpms
register: rpm_paths
- name: install mysql rpms
yum: name={{ item.stdout }} state=installed
with_items: rpm_paths.results
- name: install MySQL-python module
yum: name=MySQL-python state=installed
- name: copy the my.cnf file
action: template src=my.cnf.j2 dest={{ mysql_conf_dir }}/my.cnf owner=root mode=0644
notify: restart mysql
- name: touch the mysql-slow.log file
file: path={{ mysql_slow_log }} owner=mysql mode=0644 state=touch
- name: start mysql service
service: name={{ mysql_service }} state=started enabled=yes
#
# Change credentials for root
#
- name: check if .mysql_secret does not exist
shell: test ! -e /root/.mysql_secret
register: mysql_secret_not_exist
ignore_errors: True
- name: get mysql initial password
shell: cat /root/.mysql_secret | awk '{print $18}'
register: mysql_default_password
when: mysql_secret_not_exist|failed
- debug: msg="mysql default root password is [{{ mysql_default_password.stdout }}]"
when: mysql_secret_not_exist|failed
- name: update mysql root password
command: /usr/bin/mysqladmin -uroot -p'{{ mysql_default_password.stdout }}' password '{{ mysql_root_db_pass }}'
when: mysql_secret_not_exist|failed
- name: delete .mysql_secret
file: path=/root/.mysql_secret state=absent
when: mysql_secret_not_exist|failed
- name: copy .my.cnf file with root password credentials
template: src=.my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600
#
# Setup users
#
- name: ensure anonymous users are not in the database
mysql_user: name='' state=absent
- name: create the database users
mysql_user: name={{ item.name }} host={{ item.host }} password={{ item.pass }} priv={{ item.priv }} state=present
with_items: mysql_users
when: mysql_users|lower() != 'none'
- name: create the replication users
mysql_user: name={{ item.name }} host='%' password={{ item.pass }}
priv=*.*:"REPLICATION SLAVE" state=present
with_items: mysql_repl_user
when: mysql_repl_role == 'master'
#
# Create database
#
- name: remove the test database
action: mysql_db db=test state=absent
- name: create the databases
mysql_db: name={{ item.name }} state=present
with_items: mysql_db
when: mysql_db|lower() != 'none'
#
# Replication settings
#
- name: check if slave is already configured for replication
mysql_replication: mode=getslave
ignore_errors: true
register: slave
when: mysql_repl_role == 'slave'
- name: get the current master servers replication status
mysql_replication: mode=getmaster
delegate_to: "{{ mysql_repl_master }}"
register: repl_stat
when: slave|failed and mysql_repl_role == 'slave' and mysql_repl_master is defined
- name: change the master in slave to start the replication
mysql_replication: >
mode=changemaster
master_host={{ mysql_repl_master }}
master_user={{ mysql_repl_user[0].name }}
master_password={{ mysql_repl_user[0].pass }}
master_log_file={{ repl_stat.File }}
master_log_pos={{ repl_stat.Position }}
when: slave|failed and mysql_repl_role == 'slave' and mysql_repl_master is defined
ignore_errors: True
- name: start replication
mysql_replication: mode=startslave
when: slave|failed and mysql_repl_role == 'slave' and mysql_repl_master is defined