|
31 | 31 | - mariadb-client |
32 | 32 | # - mariadb-common |
33 | 33 |
|
34 | | - - name: Edit my.cnf |
| 34 | + - name: Add line in my.cnf |
35 | 35 | ansible.builtin.lineinfile: |
36 | 36 | path: /etc/mysql/my.cnf |
37 | | - regexp: 'bind-address = 0.0.0.0' |
38 | | - line: 'bind-address = 0.0.0.0' |
| 37 | + regexp: "{{item}}" |
| 38 | + line: "{{item}}" |
39 | 39 | state: present |
| 40 | + with_items: |
| 41 | + - "[mysqld]" |
| 42 | + - "bind-address = 0.0.0.0" |
| 43 | + - "log_bin = /var/log/mysql/mysql-bin.log" |
| 44 | + - "max_binlog_size = 100M" |
| 45 | + - "relay_log = /var/log/mysql/mysql-relay-bin" |
| 46 | + - "relay_log_index = /var/log/mysql/mysql-relay-bin.index" |
40 | 47 |
|
41 | | - - name: Restart and Enable MariaDB |
42 | | - ansible.builtin.service: |
43 | | - name: mysql |
44 | | - state: restarted |
45 | | - enabled: true |
46 | | - |
47 | | - - name: Delete Anonymous user |
48 | | - community.mysql.mysql_user: |
49 | | - name: '' |
50 | | - host: localhost |
51 | | - state: absent |
52 | | - login_unix_socket: /run/mysqld/mysqld.sock |
| 48 | + # - name: Delete Anonymous user |
| 49 | + # community.mysql.mysql_user: |
| 50 | + # name: '' |
| 51 | + # host: localhost |
| 52 | + # state: absent |
| 53 | + # login_unix_socket: /run/mysqld/mysqld.sock |
53 | 54 |
|
54 | | - - name: Set Root Password with defined variable |
55 | | - community.mysql.mysql_user: |
56 | | - name: "root" |
57 | | - password: "{{root_password}}" |
58 | | - host: "{{item}}" |
59 | | - login_unix_socket: /run/mysqld/mysqld.sock |
60 | | - with_items: |
61 | | - - localhost |
62 | | - # - 127.0.0.1 |
63 | | - # - ::1 |
| 55 | + # - name: Set Root Password with defined variable |
| 56 | + # community.mysql.mysql_user: |
| 57 | + # name: "root" |
| 58 | + # password: "{{root_password}}" |
| 59 | + # host: "{{item}}" |
| 60 | + # login_unix_socket: /run/mysqld/mysqld.sock |
| 61 | + # with_items: |
| 62 | + # - localhost |
| 63 | + # # - 127.0.0.1 |
| 64 | + # # - ::1 |
64 | 65 |
|
65 | 66 | ### RUN ON MASTER SERVER ONLY |
66 | | -# when: inventory_hostname in groups["master[0]"] |
| 67 | +# when: inventory_hostname in groups["master[0]"][0] |
| 68 | + |
67 | 69 | - name: Add line in my.cnf |
68 | 70 | ansible.builtin.lineinfile: |
69 | 71 | path: /etc/mysql/my.cnf |
70 | 72 | regexp: "{{item}}" |
71 | 73 | line: "{{item}}" |
72 | 74 | state: present |
73 | | - when: inventory_hostname in groups["master"] |
74 | 75 | with_items: |
75 | 76 | - "server-id = 1" |
76 | | - - "log_bin = /var/log/mysql/mysql-bin.log" |
77 | | - - "max_binlog_size = 100M" |
78 | | - - "relay_log = /var/log/mysql/mysql-relay-bin" |
79 | | - - "relay_log_index = /var/log/mysql/mysql-relay-bin.index" |
80 | | - |
| 77 | + when: inventory_hostname in groups["master"][0] |
81 | 78 |
|
82 | 79 | - name: Restart and Enable MariaDB |
83 | 80 | ansible.builtin.service: |
84 | 81 | name: mysql |
85 | 82 | state: restarted |
86 | 83 | enabled: true |
87 | | - |
88 | | - - name: Delete Anonymous user |
89 | | - community.mysql.mysql_user: |
90 | | - name: '' |
91 | | - host: localhost |
92 | | - state: absent |
93 | | - login_unix_socket: /run/mysqld/mysqld.sock |
94 | | - |
95 | | - - name: Set Root Password with defined variable |
96 | | - community.mysql.mysql_user: |
97 | | - name: "root" |
98 | | - password: "{{root_password}}" |
99 | | - host: "{{item}}" |
100 | | - login_unix_socket: /run/mysqld/mysqld.sock |
101 | | - with_items: |
102 | | - - localhost |
103 | | - # - 127.0.0.1 |
104 | | - # - ::1 |
| 84 | + when: inventory_hostname in groups["master"][0] |
105 | 85 |
|
106 | 86 | - name: Create Database "{{database}}" |
107 | 87 | community.mysql.mysql_db: |
|
118 | 98 | login_user: root |
119 | 99 | login_password: "{{root_password}}" |
120 | 100 | state: present |
| 101 | + |
| 102 | + - name: "Create user dbrep for replication" |
| 103 | + community.mysql.mysql_user: |
| 104 | + name: "dbrep" |
| 105 | + password: "dbrep" |
| 106 | + priv: "*.*:REPLICATION SLAVE" |
| 107 | + host: "%" |
| 108 | + login_user: root |
| 109 | + login_password: "{{root_password}}" |
| 110 | + state: present |
| 111 | + when: inventory_hostname in groups["master"][0] |
| 112 | + |
| 113 | + - name: Get Master Status |
| 114 | + community.mysql.mysql_info: |
| 115 | + login_user: root |
| 116 | + login_password: "{{root_password}}" |
| 117 | + login_unix_socket: /run/mysqld/mysqld.sock |
| 118 | + register: master_status |
| 119 | + when: inventory_hostname in groups["master"][0] |
| 120 | + |
| 121 | + - name: Get Master Host |
| 122 | + command: echo "{{hostvars[inventory_hostname].inventory_hostname}}" |
| 123 | + register: master_host |
| 124 | + when: inventory_hostname in groups["master"][0] |
| 125 | + |
| 126 | +### RUN ON SLAVE ONLY |
| 127 | +# when: inventory_hostname in groups["slave"] |
| 128 | + |
| 129 | + - set_fact: |
| 130 | + master_host: "{{ hostvars[groups['master'].0].master_host }}" |
| 131 | + master_log_file: "{{ hostvars[groups['master'].0].master_status.master_status.File }}" |
| 132 | + master_log_pos: "{{ hostvars[groups['master'].0].master_status.master_status.Position }}" |
| 133 | + |
| 134 | + - name: Add line in my.cnf |
| 135 | + ansible.builtin.lineinfile: |
| 136 | + path: /etc/mysql/my.cnf |
| 137 | + regexp: "{{item}}" |
| 138 | + line: "{{item}}" |
| 139 | + state: present |
| 140 | + with_items: |
| 141 | + - "server-id = 2" |
| 142 | + when: inventory_hostname in groups["slave"] |
| 143 | + |
| 144 | + - name: Stop and then Reset Slave Server |
| 145 | + community.mysql.mysql_replication: |
| 146 | + mode: "{{item}}" |
| 147 | + login_user: root |
| 148 | + login_password: "{{root_password}}" |
| 149 | + login_host: localhost |
| 150 | + when: inventory_hostname in groups["slave"] |
| 151 | + with_items: |
| 152 | + - stopreplica |
| 153 | + - resetreplica |
| 154 | + |
| 155 | + - name: Config Slave Server |
| 156 | + community.mysql.mysql_replication: |
| 157 | + mode: changeprimary |
| 158 | + login_user: root |
| 159 | + login_password: "{{root_password}}" |
| 160 | + login_host: localhost |
| 161 | + # primary_host : "{{master_host}}" |
| 162 | + MASTER_HOST : 172.17.136.104 |
| 163 | + MASTER_USER : "dbrep" |
| 164 | + MASTER_PASSWORD : "dbrep" |
| 165 | + MASTER_LOG_FILE : "{{master_log_file}}" |
| 166 | + MASTER_LOG_POS : "{{master_log_pos}}" |
| 167 | + when: inventory_hostname in groups["slave"] |
121 | 168 |
|
| 169 | + name: Start Slave Server |
| 170 | + community.mysql.mysql_replication: |
| 171 | + mode: startreplica |
| 172 | + login_user: root |
| 173 | + login_password: "{{root_password}}" |
| 174 | + login_host: localhost |
| 175 | + when: inventory_hostname in groups["slave"] |
0 commit comments