ansible角色部署mysql主从复制

ansible角色部署mysql主从复制

[root@ansible ansible]# vim hosts 
[mysql]
node1
node4

[mysql_master]
node1

[mysql_slave]
node4

[root@ansible ansible]# cd roles/
[root@ansible roles]# ls
[root@ansible roles]# ansible-galaxy init mysql
- Role mysql was created successfully
[root@ansible roles]# ls
mysql

[root@ansible mysql]# cat templates/my.cnf.j2 
[mysqld]
log_bin=mysqld
server_id=20

[root@ansible templates]# cat master.cnf.j2 
[mysqld]
log_bin=mysql-bin
server_id=10
[root@ansible templates]# cat slave.cnf.j2 
[myslqd]
log_bin=mysql-bin
server_id=20

[root@ansible mysql]# cat tasks/main.yml 
---
# tasks file for mysql
- name: mount
  mount: 
    src: /dev/cdrom
    path: /mnt
    fstype: iso9660
    state: mounted

- name: clear repo 
  shell: 
    cmd: rm -rf /etc/yum.repos.d.*

- name: set repo1
  yum_repository: 
    file: server 
    name: aa
    description: aa1
    baseurl: file:///mnt/BaseOS
    enabled: yes
    gpgcheck: no


- name: set repo2
  yum_repository: 
    file: server 
    name: bb
    description: bb2
    baseurl: file:///mnt/AppStream
    enabled: yes
    gpgcheck: no

- name: stop selinux
  lineinfile: 
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=disabled

- name: stop selinux2
  shell: 
    cmd: setenforce 0

- name: install mariadb
  dnf: 
    name: 
      - mariadb
      - mariadb-server
    state: present

- name: cp config
  template: 
    src: master.cnf.j2
    dest: /etc/my.cnf
  when: inventory_hostname in {{ groups.mysql_master }}

- name: cp config2
  template: 
    src: slave.cnf.j2
    dest: /etc/my.cnf
  when: inventory_hostname in {{ groups.mysql_slave }}

- name: start mariadb
  service: 
    name: mariadb
    state: restarted
    enabled: yes

- name: grant root
  shell: 
    cmd: mysql -uroot -e "grant all privileges on *.* to root@'%' identified by 'redhat';"

- name: master
  shell: 
    cmd: mysql -uroot -e "grant replication slave on *.* to 'user'@'slave' identified by 'redhat';"
  when: inventory_hostname in {{ groups.mysql_master }}


- name: slave
  shell: 
    cmd: mysql -uroot -e "change master to master_host='master',master_user='user',master_password='redhat';"
  when: inventory_hostname in {{ groups.mysql_slave }}

- name: start slave
  shell: 
    cmd: mysql -uroot -e "start slave;"
  when: inventory_hostname in {{ groups.mysql_slave }}


[root@ansible ansible]# ansible-playbook mysql.yml 

PLAY [mysql] *******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [node4]
ok: [node1]

TASK [mysql : mount] ***********************************************************
ok: [node4]
ok: [node1]

TASK [mysql : clanr repo] ******************************************************
[WARNING]: Consider using the file module with state=absent rather than running
'rm'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
changed: [node4]
changed: [node1]

TASK [mysql : set repo1] *******************************************************
ok: [node4]
ok: [node1]

TASK [mysql : set repo2] *******************************************************
ok: [node4]
ok: [node1]

TASK [mysql : stop selinux] ****************************************************
changed: [node4]
ok: [node1]

TASK [mysql : stop selinux2] ***************************************************
changed: [node4]
changed: [node1]

TASK [mysql : install mariadb] *************************************************
ok: [node1]
changed: [node4]

TASK [mysql : cp config] *******************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_master }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_master }}
skipping: [node4]
ok: [node1]

TASK [mysql : cp config2] ******************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
skipping: [node1]
changed: [node4]

TASK [mysql : start mariadb] ***************************************************
changed: [node1]
changed: [node4]

TASK [mysql : grant root] ******************************************************
changed: [node4]
changed: [node1]

TASK [mysql : master] **********************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_master }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_master }}
skipping: [node4]
changed: [node1]

TASK [mysql : slave] ***********************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
skipping: [node1]
changed: [node4]

TASK [mysql : start slave] *****************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: inventory_hostname in {{
groups.mysql_slave }}
skipping: [node1]
changed: [node4]

PLAY RECAP *********************************************************************
node1                      : ok=12   changed=5    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0   
node4                      : ok=13   changed=9    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Connecting to master
                   Master_Host: master
                   Master_User: user
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: 
           Read_Master_Log_Pos: 4
                Relay_Log_File: node4-relay-bin.000001
                 Relay_Log_Pos: 4
         Relay_Master_Log_File: 
              Slave_IO_Running: Connecting
             Slave_SQL_Running: Yes
               Replicate_Do_DB: 
           Replicate_Ignore_DB: 
            Replicate_Do_Table: 
        Replicate_Ignore_Table: 
       Replicate_Wild_Do_Table: 
   Replicate_Wild_Ignore_Table: 
                    Last_Errno: 0
                    Last_Error: 
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 0
               Relay_Log_Space: 256
               Until_Condition: None
                Until_Log_File: 
                 Until_Log_Pos: 0
            Master_SSL_Allowed: No
            Master_SSL_CA_File: 
            Master_SSL_CA_Path: 
               Master_SSL_Cert: 
             Master_SSL_Cipher: 
                Master_SSL_Key: 
         Seconds_Behind_Master: NULL
 Master_SSL_Verify_Server_Cert: No
                 Last_IO_Errno: 2005
                 Last_IO_Error: error connecting to master 'user@master:3306' - retry-time: 60  maximum-retries: 86400  message: Unknown MySQL server host 'master' (-2)
                Last_SQL_Errno: 0
                Last_SQL_Error: 
   Replicate_Ignore_Server_Ids: 
              Master_Server_Id: 0
                Master_SSL_Crl: 
            Master_SSL_Crlpath: 
                    Using_Gtid: No
                   Gtid_IO_Pos: 
       Replicate_Do_Domain_Ids: 
   Replicate_Ignore_Domain_Ids: 
                 Parallel_Mode: conservative
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
              Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
    Slave_Transactional_Groups: 0
1 row in set (0.000 sec)

posted @ 2022-11-10 20:23  罗家龙  阅读(223)  评论(0编辑  收藏  举报