安装ansible
主控端
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install ansible -y
被控端
yum install libselinux-python -y
编辑hosts文件
[root@kube-master1 ansible]# cat hosts
[node1]
192.168.1.4
[node2]
192.168.1.5
[node0]
192.168.1.3
工程的目录结构

启动文件deploy.yml
[root@kube-master1 ansible]# cat deploy.yml
---
- hosts: all
remote_user: root
roles:
- redis
全局变量
[root@kube-master1 ansible]# cd group_vars/
[root@kube-master1 group_vars]# ll
total 4
-rw-r--r--. 1 root root 93 Mar 2 15:23 all.yml
[root@kube-master1 group_vars]# cat all.yml
master_port: 7000
slave_port: 7001
file_name: 'redis-5.0.14'
install_path: '/u01/app/redis/'
[root@kube-master1 group_vars]#
源文件
[root@kube-master1 ansible]# ll
total 28
-rw-r--r--. 1 root root 19985 Mar 2 12:40 ansible.cfg
-rw-r--r--. 1 root root 58 Mar 2 15:23 deploy.yml
drwxr-xr-x. 2 root root 20 Mar 2 15:47 group_vars
-rw-r--r--. 1 root root 60 Mar 2 14:57 hosts
drwxr-xr-x. 3 root root 18 Mar 2 15:22 roles
drwxr-xr-x. 2 root root 69 Mar 2 12:40 scripts
[root@kube-master1 ansible]# cd roles
[root@kube-master1 roles]# ll
total 0
drwxr-xr-x. 4 root root 30 Mar 2 15:44 redis
[root@kube-master1 roles]# cd redis
[root@kube-master1 redis]# ll
total 0
drwxr-xr-x. 2 root root 32 Mar 2 15:28 files
drwxr-xr-x. 2 root root 21 Mar 2 15:31 tasks
[root@kube-master1 redis]# cd file
-bash: cd: file: No such file or directory
[root@kube-master1 redis]# cd files
[root@kube-master1 files]# ll
total 1956
-rw-r--r--. 1 root root 2000179 Oct 4 2021 redis-5.0.14.tar.gz
[root@kube-master1 files]#
主playbook
[root@kube-master1 redis]# cd tasks
[root@kube-master1 tasks]# ll
total 4
-rw-r--r--. 1 root root 3488 Mar 2 15:31 main.yml
[root@kube-master1 tasks]# cat main.yml
---
- name: createdir
file: dest={{ install_path }}/redis/ state=directory
- name: copy tar
copy: src={{ file_name }}.tar.gz dest=/tmp mode=u+x
- name: tarpackage
unarchive: src=/tmp/{{ file_name }}.tar.gz dest={{ install_path }}/redis/ mode=0755 copy=no
- name: install redis
shell: chdir={{ install_path }}/redis/{{ file_name }}/ make && make install
- name: create master conf dir
file: dest={{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/{{ item }} state=directory
with_items:
- log
- data
- pid
- name: create slave conf dir
file: dest={{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/{{ item }} state=directory
with_items:
- log
- data
- pid
- name: scp redis.conf
copy: src={{ install_path }}/redis/{{ file_name }}/redis.conf dest={{ install_path }}/redis/{{ file_name }}/conf/{{ item }}/
with_items:
- 7000
- 7001
- name: modify master config
replace: path={{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/redis.conf regexp={{ item.regexp }} replace={{ item.line }}
with_items:
- { regexp: 'bind 127.0.0.1', line: 'bind 0.0.0.0' }
- { regexp: 'daemonize no', line: 'daemonize yes' }
- { regexp: 'pidfile /var/run/redis_6379.pid', line: 'pidfile "{{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/pid/redis_{{master_port}}.pid"' }
- { regexp: 'logfile ""', line: 'logfile "{{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/log/{{ master_port }}.log"' }
- { regexp: 'dir ./', line: 'dir "{{ install_path }}/redis/{{ file_name }}/conf/{{ master_port }}/data"' }
- { regexp: '# cluster-enabled yes', line: 'cluster-enabled yes' }
- { regexp: '# cluster-config-file nodes-6379.conf', line: 'cluster-config-file "nodes{{ master_port }}.conf"' }
- { regexp: '# cluster-node-timeout 15000', line: 'cluster-node-timeout 15000' }
- { regexp: 'appendonly no', line: 'appendonly yes' }
- { regexp: '# requirepass foobared', line: 'requirepass redis' }
- { regexp: 'port 6379', line: 'port {{ master_port }}' }
- name: modify slave config
replace: path={{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/redis.conf regexp={{ item.regexp }} replace={{ item.line }}
with_items:
- { regexp: 'bind 127.0.0.1', line: 'bind 0.0.0.0' }
- { regexp: 'daemonize no', line: 'daemonize yes' }
- { regexp: 'pidfile /var/run/redis_6379.pid', line: 'pidfile "{{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/pid/redis_{{slave_port}}.pid"' }
- { regexp: 'logfile ""', line: 'logfile "{{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/log/{{ slave_port }}.log"' }
- { regexp: 'dir ./', line: 'dir "{{ install_path }}/redis/{{ file_name }}/conf/{{ slave_port }}/data"' }
- { regexp: '# cluster-enabled yes', line: 'cluster-enabled yes' }
- { regexp: '# cluster-config-file nodes-6379.conf', line: 'cluster-config-file "nodes{{ slave_port }}.conf"' }
- { regexp: '# cluster-node-timeout 15000', line: 'cluster-node-timeout 15000' }
- { regexp: 'appendonly no', line: 'appendonly yes' }
- { regexp: '# requirepass foobared', line: 'requirepass redis' }
- { regexp: 'port 6379', line: 'port {{ slave_port }}' }
- name: startredis
shell: chdir={{ install_path }}/redis/{{ file_name }}/src/ redis-server {{ install_path }}/redis/{{ file_name }}/conf/{{ item }}/redis.conf
with_items:
- 7000
- 7001
[root@kube-master1 tasks]#
运行
ansible-playbook /etc/ansible/deploy.yml