diff --git a/.gitignore b/.gitignore index 6c20424..8472125 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,5 @@ vault_pass # ---> vagrant .vagrant + +*.swp diff --git a/ansible/playbook.yaml b/ansible/playbook.yaml index b72635f..15e4770 100644 --- a/ansible/playbook.yaml +++ b/ansible/playbook.yaml @@ -21,8 +21,14 @@ - 192.168.56.2 - 192.168.56.3 - 192.168.56.4 + mysql: + replication_user: replica_user + replication_master: web1 + replication_slaves: + - web2 + - web3 pre_tasks: - - debug: var=ansible_all_ipv4_addresses[1] + - debug: var=ansible_all_ipv4_addresses # we need gluster on all servers to configure peering - name: Install gluster ansible.builtin.apt: @@ -41,6 +47,7 @@ roles: - nginx - mysql + - mysql-replication - php - glusterfs - wordpress diff --git a/ansible/roles/mysql-replication/tasks/main.yaml b/ansible/roles/mysql-replication/tasks/main.yaml new file mode 100644 index 0000000..0bfce75 --- /dev/null +++ b/ansible/roles/mysql-replication/tasks/main.yaml @@ -0,0 +1,67 @@ +--- +- name: Write config + ansible.builtin.template: + src: "mysql.cnf.j2" + dest: "/etc/mysql/mariadb.conf.d/50-server.cnf" + mode: "0644" + become: true + +- name: restart mysql + ansible.builtin.service: + name: mysql + state: restarted + enabled: true + become: true + +- name: Create replication user + community.mysql.mysql_user: + state: present + login_user: root + login_password: "{{ mysql_root_password }}" + name: "{{ mysql.replication_user }}" + password: "{{ mysql_replication_pass }}" + host: "%" + tls_requires: + priv: + "*.*": "REPLICATION SLAVE" + login_unix_socket: /var/run/mysqld/mysqld.sock + become: true + when: mysql.replication_master in ansible_all_ipv4_addresses + +- name: Get master replication status + community.mysql.mysql_replication: + login_user: root + login_password: "{{ mysql_root_password }}" + mode: getmaster + register: repl_stat + when: ansible_hostname == mysql.replication_master + +- debug: var=repl_stat + +- name: Check if slave is already configured as replica + community.mysql.mysql_replication: + login_user: root + login_password: "{{ mysql_root_password }}" + mode: getslave + ignore_errors: true + register: slave + when: ansible_hostname in mysql.replication_slaves + +- name: Setup slaves + community.mysql.mysql_replication: + login_user: root + login_password: "{{ mysql_root_password }}" + mode: changemaster + master_host: "{{ mysql.replication_master }}" + master_log_file: "{{ hostvars['web1'].repl_stat.File }}" + master_log_pos: "{{ hostvars['web1'].repl_stat.Position }}" + master_user: "{{ mysql.replication_user }}" + master_password: "{{ mysql_replication_pass }}" + when: ansible_hostname in mysql.replication_slaves and not slave.Is_Slave + +- name: Start slave replication + community.mysql.mysql_replication: + login_user: root + login_password: "{{ mysql_root_password }}" + mode: startslave + when: ansible_hostname in mysql.replication_slaves and not slave.Is_Slave diff --git a/ansible/roles/mysql-replication/templates/mysql.cnf.j2 b/ansible/roles/mysql-replication/templates/mysql.cnf.j2 new file mode 100644 index 0000000..18a15ed --- /dev/null +++ b/ansible/roles/mysql-replication/templates/mysql.cnf.j2 @@ -0,0 +1,15 @@ +[mysqld] +bind-address = "0.0.0.0" +{% if ansible_hostname == mysql.replication_master %} +server-id = 1 +expire_logs_days = 10 +max_binlog_size = 100M +log-bin = /var/log/mysql/mysql-bin.log +binlog_do_db = {{ wordpress.db_name }} +{% endif %} +{% for item in mysql.replication_slaves %} +{% if ansible_hostname == item %} +server-id = {{ loop.index0 + 2}} +relay-log = /var/log/mysql/mysql-relay-bin.log +{% endif %} +{% endfor %} diff --git a/ansible/roles/mysql/tasks/main.yaml b/ansible/roles/mysql/tasks/main.yaml index 66bd8be..e9154fe 100644 --- a/ansible/roles/mysql/tasks/main.yaml +++ b/ansible/roles/mysql/tasks/main.yaml @@ -36,7 +36,6 @@ dest: "/root/.my.cnf" mode: "0400" become: true - notify: restart mysql - name: Setup database for wordpress community.mysql.mysql_db: diff --git a/ansible/vault.yaml b/ansible/vault.yaml index 47bdeb5..0a4d918 100644 --- a/ansible/vault.yaml +++ b/ansible/vault.yaml @@ -1,45 +1,47 @@ $ANSIBLE_VAULT;1.1;AES256 -63396462343032353537643534623762366664343334356561636262623263643463393066323336 -3161366363313663623733646333386266333338343162650a376432326466636664656631313431 -65353463653532643862363731313162646432326139303366656532626339306464363766353137 -3838356632333062660a396239653337633736363737303463386564373636613164363961376564 -31303636366534373764653839303739393733353535343337303365613734373465396231626239 -35376538353665343530663730336663303039633539333131306231346631626239303535333733 -33613538356261633631303964373763376563633466653636303132663863346338643032663265 -63633264653262363235333636363866636530313838633561623066653735383832383965656433 -33623863633837626238326466363336643962383265326232303035663333316663633932653265 -63333932616334383432306665303338356436313765626435306466613134303530363165653761 -61666265396433643138353761366164383135373336383831643333363866303634626135343638 -62336237633963666663663132366265373939303331363236646334323762376665303731663338 -66643633383064316537623130333639373262303236313932656663666230353364313565623938 -66666465306532343837383135636436323439623137663164386234336331316561653565666234 -65623735633163666465383066363934326463643839613563306134336265343137363331393164 -62373363336262653562646464633666383563303333363261626638323538633438373936386230 -30616634323466363661663335303539373837343561316638333839343937313433313164313166 -32613764633439666530663235363233313661326233656362343339623163663536333730636461 -65633939616539643137663165616263643662393164303564626231326630623061393265663031 -32386666663232623262333631633834363663663063356639303532353036343134396235633862 -35653934373732653931363465346536313563383238646133636233376136303562623862613332 -36626136613231373663646564343066326533623730356538303231633833353562376366306432 -62303338346430316439336631613938623334663561323032653764343836633332666464656365 -35303963353830643939633966343732666333646565656263626561663566623334636236313130 -31303166376664396436303435333939626530376263663931663865666138336230353539333037 -32343362643763623136373431666435656434366139393732386432613735623061333332326462 -63643764613230393862396531653534643365396538303637636366316265313434633535613934 -65383161653463636536393364653538373436316333373137653938663135373035353132646362 -35336132393830323337633463626635623762333732393139396630336434663263313939373964 -66656431666634373834303861666162666231373063386532666666653163633530663737346365 -31653138333862363238653833653938666264316161313039363332663235613037636134343939 -34643430373766373030346664313639353137306266653634316530346438333232333537653130 -30323563613762306234376537626539343039656136316164613966383136643039646234623635 -63333165343838663762363265333530303836346330316636303336303864383963326463616362 -30393637373730656362353835353562663261626530623634323732333537333430313730613933 -31616532303733366664306532646630303134333564386337623333643931653835363031363033 -38363939333133626436386539353436386433313833393439313762613362303232353133343762 -64313163316430663461653237646131336334633631306238316463373930393736336537366139 -61643831303031656666383866656565653861646563386631616438616566306563636131316531 -64303763383530633336316636643238653664396636616565663936313966313033306635346335 -39376662333630353330396435613031366462333463636434323134396332386564666438346234 -66656365336635626237623736623235613663363961616139363362313436396234656136386637 -66373733313138646537373366363864343034363461383561633436613932663662383839356565 -31343336346632313363 +35643435643566346466373336656430653736616533613430303662613532633737643436656565 +6464366565333938376361393565353965383265386631620a303038376162333866323035643239 +62343237643233643135633137633564633464613736656532383336633566633832373066343039 +3039313331343065610a626135326638306363643538383337656632613561333561643664623861 +34373261356537363739663263623264343663383565643062323333303138373066363135363132 +62646437636332323634333739303839306532653733626332316234313935633132656463363937 +32653865653166646566613965373434656437633531643138376539656166346233653531613363 +32643736616231646534303738653864303735346339396139623833376163383030643538373034 +34663438343633333765346236393832333433373633353466386536653539316435306134636533 +61333637656431316230636135633835653031326665303364653766383832303430626436663936 +32356261386534306562626235336338326530373635633730663263373733336630383836343461 +37363737646165643736636637633063666562303332353466313739386635666163353165636337 +36326637343261393361303266343332643436386565333330656432636363316138303464643339 +34386165666633366439643937376139663530306631633737636537313862346562353139326665 +36666166333139393337353266626562653136623965343563383238373935653835326335363262 +31303037646663373062316432346439666230643966663039616266633930653139613163396131 +36346438316363383434363739653637393633303330666531626362326465616638653964313731 +31316533663030616339383465363135633463653733636230333462323562333866323437646264 +35633631383463656134363338633430386163393230353938373262306436656563343031363562 +36633762633666343835376261383361363536373836656466613338313633383961396566343763 +32396533643231316666366532636361376238303466653539666439326633363830366234336231 +32366663633436663436626262666633633739366234376334643139633666326639363362643263 +62326337643536323433613938613236306538316532643261383134396333646562613163343062 +33346230363232363935313531383866646232326162396264666162653362633666333333366562 +61383065653039646461306338663235303437313634666636353336663238336363393236663664 +30306532633061613966303334363934663232646339653534616539633839646466373664363830 +39386538626230306531333136653762666366306532303739363962333231373962653263653039 +62356536636537613930623438323934623631316334386665643065326562393065306664626639 +38333664393861653530396533373761353336363131306539333032643235386332326336393666 +34353039653234346334383338396664393535653835323733363038353062666339383533363938 +34303234393731643837313262616236613061663365316161396536363164396565663533373530 +37623833343364653963626131666235383462653964633937356161363830333737383635373163 +62666362353234633830653230306638363238666361356135653362356237613764346337663536 +65343234306530376434646532633833373365316166303536633330323366393335383035316332 +33616263656432633033353733626562633135396434633135623962303661393537366566306132 +61356663623237636432373733336261653262303038383063363838636638346134376437613539 +33313132636465666130653634623361363930383535393736613463626532366639343139393133 +33396432613834373732306439396561623839666630343731393765616431376631353834646239 +34316434313535626538306430323231363639383861323030656339376563386539303563623265 +36313933626538353333326266353233393336653434346437383834376136376337663466623561 +36633037643864326164646262383534333434373338323834653135613135396637376239663264 +38633330366130363930313932356430623862323737343165393035646364386436613365613564 +34306564643331343039643236396232336332326333636264623632386432393865666538313034 +32636361396663356637336435626135363661663536376136366632616635643233623762316463 +39343964323639323832323963373264303162663334393163396361356234356336633538623764 +6132343932373562653931613737366635323931346264633233