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主从(单机)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章