1. 使用ansible在redis配置文件中写入内容
在ansible主机上安装redis
tar zxf redis-5.0.3.tar.gz
cd redis-5.0.3
yum install gcc
make MALLOC=libc
make install
cd utils/
./install_server.sh
scp -r redis-5.0.3 root@172.25.60.3:/root/
在另一台主机上安装redis
make install
cd utils/
./install_server.sh
用ansible获取目标主机的ip
[root@server4 ~]# ansible testB -m debug -a "msg={{ansible_host}}"
172.25.60.3 | SUCCESS => {
"msg": "172.25.60.3"
}
在6379文件中写入ansible_host
[root@server4 ~]# vim 6379.conf
bind {{ansible_host}} 127.0.0.1
[root@server4 ~]# ansible testB -m template -a "src=/root/6379.conf dest=/etc/redis/6379.conf"
测试:
[root@server3 redis]# vim 6379.conf # 成功在testB这台主机中写入本机ip地址
bind 172.25.60.3 127.0.0.1
2. 使用playbook实现更改redis的6379.conf配置文件并重起redis
[root@server4 ~]# cat redis.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- template:
src: /root/6379.conf
dest: /etc/redis/6379.conf
- name: restart redis
shell: /etc/init.d/redis_6379 restart
测试:
[root@server4 ~]# ansible-playbook redis.yml
PLAY [testB] *******************************************************************
TASK [template] ****************************************************************
ok: [172.25.60.3]
TASK [restart redis] ***********************************************************
changed: [172.25.60.3]
[root@server3 redis]# netstat -antlpe|grep redis
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 0 44288 1730/redis-server 1
tcp 0 0 172.25.60.3:6379 0.0.0.0:* LISTEN 0 44287 1730/redis-server 1