1. 程式人生 > 資料庫 >Ansible 之 自動化部署redis主從(單機)

Ansible 之 自動化部署redis主從(單機)

一、playbook檔案redis.yaml

---
- hosts: test
  vars_files:
    - vars.yml
  remote_user: "{{user}}"
  become: yes
  tasks:
    - name: "安裝epel源"
      command: yum -y install epel-release
    - name: "安裝redis"
      yum:
        name: redis
        state: latest
    - name: "修改redis配置檔案監聽IP為本機"
      lineinfile:
        path: /etc/redis.conf
        regexp: '^bind 127'
        line: bind {{master_redis}}
    - name: "設定開機啟動"
      service:
        name: redis
        enabled: yes
    - name: "啟動主庫redis6379埠"
      service:
        name: redis
        state: started
    - name: "製作從庫配置檔案"
      command: cp /etc/redis.conf /etc/redis_slave.conf
    - name: "建立從庫的資料存放位置"
      command: mkdir -p /var/lib/redis_slave
    - name: "編輯從庫配置檔案的繫結IP"
      lineinfile:
        path: /etc/redis_slave.conf
        regexp: '^bind 127'
        line: bind {{master_redis}}
    - name: "編輯從庫配置檔案的資料存放位置"
      lineinfile:
        path: /etc/redis_slave.conf
        regexp: '^dir /var/lib/redis'
        line: dir /var/lib/redis_slave
    - name: "編輯從庫配置檔案的監聽埠"
      lineinfile:
        path: /etc/redis_slave.conf
        regexp: '^port 6379'
        line: port 7777
    - name: "編輯從庫的配置檔案,設定為從庫"
      lineinfile:
        path: /etc/redis_slave.conf
        line: slaveof "{{master_redis}}" 6379
    - name: "啟動從庫"
      shell: nohup redis-server /etc/redis_slave.conf &

二、外部變數檔案vars.yml

[root@Ansible playbook]# cat vars.yml 
user: cedar
master_redis: 10.3.153.8

三、安裝結果

[root@Ansible playbook]# ansible-playbook redis.yaml 

PLAY [test] *****************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************
ok: [10.3.153.8]

TASK [安裝epel源] **************************************************************************************************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'.  If you need to use command because yum 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: [10.3.153.8]

TASK [安裝redis] **************************************************************************************************************************************************
ok: [10.3.153.8]

TASK [修改redis配置檔案監聽IP為本機] ***************************************************************************************************************************************
ok: [10.3.153.8]

TASK [設定開機啟動] ***************************************************************************************************************************************************
ok: [10.3.153.8]

TASK [啟動主庫redis6379埠] ******************************************************************************************************************************************
ok: [10.3.153.8]

TASK [製作從庫配置檔案] *************************************************************************************************************************************************
changed: [10.3.153.8]

TASK [建立從庫的資料存放位置] **********************************************************************************************************************************************
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'.  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: [10.3.153.8]

TASK [編輯從庫配置檔案的繫結IP] ********************************************************************************************************************************************
ok: [10.3.153.8]

TASK [編輯從庫配置檔案的資料存放位置] ******************************************************************************************************************************************
changed: [10.3.153.8]

TASK [編輯從庫配置檔案的監聽埠] ********************************************************************************************************************************************
changed: [10.3.153.8]

TASK [編輯從庫的配置檔案,設定為從庫] ******************************************************************************************************************************************
changed: [10.3.153.8]

TASK [啟動從庫] *****************************************************************************************************************************************************
changed: [10.3.153.8]

PLAY RECAP ******************************************************************************************************************************************************
10.3.153.8                 : ok=13   changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

四、驗證部署情況

Ansible 之 自動化部署redis主從(單機)