1、hosts文件添加主机组

[db_server]
192.168.1.148

2、创建roles目录

cd /etc/ansible/roles/
mkdir -p redis_install/{default,files,handlers,meta,tasks,templates,vars}

3、移动安装到到

cd redis_install/files/

wget http://download.redis.io/releases/redis-5.0.8.tar.gz

4、创建redis的配置模板文件

vim /etc/ansible/roles/redis_install/templates/redis.conf.j2

bind 127.0.0.1  {{ ansible_default_ipv4.address }}
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes 
supervised no
pidfile {{ redis_data_dir }}/redis_6379.pid
loglevel notice
logfile {{ redis_data_dir }}/redis.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir {{ redis_data_dir }} 
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events KEA
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

5、创建调用roles的playbook作业 (注意host的角色分组是哪个)

cd /etc/ansible/roles/
vim redis_install.yml

- hosts: db_server
  remote_user: root
  roles:
    - redis_install

6、创建变量vars定义文件

vim redis_install/vars/main.yml

redis_version: redis-5.0.8
redis_data_dir: /data/redis/data
var_dir: /data/redis
source_dir: /tmp

7、创建tasks的main.yml文件

vim redis_install/tasks/main.yml


- include: install.yml

8、创建redis安装作业

vim redis_install/tasks/install.yml


- name: 创建redis数据目录
  file: path={{ redis_data_dir }} state=directory

- name: 复制redis包到远程主机
  copy: src={{ item }} dest={{ source_dir }}
  with_fileglob:
    - /etc/ansible/roles/redis_install/files/*

- name: 解压包
  unarchive:
    src: /tmp/redis-5.0.8.tar.gz
    dest: "{{ source_dir }}"
    copy: no

- name: 安装
  shell: cd {{ source_dir }}/redis-5.0.8 && make

- name: 创建二进制目录配置文件目录
  shell: cd {{ var_dir }} && mkdir -pv bin conf

- name: cp 编译完成的二进制文件到bin和conf下配置文件
  shell: cp {{ source_dir }}/{{ redis_version }}/src/redis-* {{ var_dir }}/bin && rm -rf {{ var_dir }}/bin/*.c && rm -rf {{ var_dir }}/bin/*.o

- name: 拷贝编译完成的redis.conf文件到conf下
  template: src=redis.conf.j2 dest={{ var_dir }}/conf/redis.conf

- name: 启动redis
  shell: "{{ var_dir }}/bin/redis-server {{ var_dir }}/conf/redis.conf"

- name: 删除redis解压包
  file: path={{ source_dir }}/{{ redis_version }} state=absent

9、运行 redis_install.yml 作业

检查playbook作业

ansible-playbook -C redis_install.yml

运行playbook作业

ansible-playbook redis_install.yml

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐