1、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 &
2、外部變量文件vars.yml
[root@Ansible playbook]# cat vars.yml
user: cedar
master_redis: 10.3.153.8
3、安裝結果
[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
4、驗證部署狀況