ansible实现redis角色

 

准备文件夹

压缩包放在了/apps/redis/

[root@centos8 ansible]# tree
.
├── redis.tar.gz
└── roles
    └── redis
        ├── defaults
        │   └── main.yml
        ├── files
        ├── handlers
        │   └── main.yml
        ├── meta
        │   └── main.yml
        ├── README.md
        ├── tasks
        │   └── main.yml
        ├── templates
        │   ├── redis.conf.j2
        │   └── redis.service.j2
        ├── tests
        │   ├── inventory
        │   └── test.yml
        └── vars
            └── main.yml

10 directories, 12 files
[root@centos8 ansible]# vim roles/redis/handlers/main.yml 

---
- name: restart redis
  service:
    name: redis
    state: restarted
    enabled: true
[root@centos8 ansible]# vim roles/redis/meta/main.yml 

galaxy_info:
  author: your name
  description: your role description
  company: your company (optional)

  # If the issue tracker for your role is not on github, uncomment the
  # next line and provide a value
  # issue_tracker_url: http://example.com/issue/tracker

  # Choose a valid license ID from https://spdx.org - some suggested licenses:
  # - BSD-3-Clause (default)
  # - MIT
  # - GPL-2.0-or-later
  # - GPL-3.0-only
  # - Apache-2.0
  # - CC-BY-4.0
  license: license (GPL-2.0-or-later, MIT, etc)

  min_ansible_version: 2.9

  # If this a Container Enabled role, provide the minimum Ansible Container version.
  # min_ansible_container_version:

  #
  # Provide a list of supported platforms, and for each platform a list of versions.
  # If you don't wish to enumerate all versions for a particular platform, use 'all'.
  # To view available platforms and versions (or releases), visit:
  # https://galaxy.ansible.com/api/v1/platforms/
  #
  # platforms:
  # - name: Fedora
  #   versions:
  #   - all
  #   - 25
  # - name: SomePlatform
  #   versions:
  #   - all
  #   - 1.0
  #   - 7
  #   - 99.99

  galaxy_tags: []
    # List tags for your role here, one per line. A tag is a keyword that describes
    # and categorizes the role. Users find roles by searching for tags. Be sure to
    # remove the '[]' above, if you add tags to this list.
    #
    # NOTE: A tag is limited to a single word comprised of alphanumeric characters.
    #       Maximum 20 tags per role.

dependencies: []
  # List your role dependencies here, one per line. Be sure to remove the '[]' above,
  # if you add dependencies to this list.
[root@centos8 ansible]# vim roles/redis/tasks/main.yml 

    state: installed
- name: create redis group
  group:
    name: redis
- name: create redis user
  user:
    name: redis
    group: redis
    shell: /sbin/nologin
- name: copy redis tar
  copy:
    src: redis-5.0.8.tar.gz
    dest: /apps/
- name: tar xf
  shell: |
    tar xf /apps/redis-5.0.8.tar.gz -C /apps/
- name: make
  shell: |
    make
  args:
    chdir: /apps/redis-5.0.8/
- name: create redis dir
  file:
    path: "{{ item }}"
    state: directory
  with_items:
    - /apps/redis
    - /apps/redis/bin
    - /apps/redis/etc
    - /apps/redis/data
    - /apps/redis/log
    - /apps/redis/run
- name: copy redis file
  shell:
    cp -r /apps/redis-5.0.8/src/*  /apps/redis/bin/
- name: reder and copy redis config
  template:
    src: redis.conf.j2
    dest: /apps/redis/etc/redis.conf
    backup: yes
  notify: restart redis
- name: change permission
  file:
    path: /apps/redis/
    owner: redis
    group: redis
    recurse: yes
- name: join system management
  template:
    src: redis.service.j2
    dest: /usr/lib/systemd/system/redis.service
- name: PATH variables
  copy:
    content: 'PATH=/apps/redis/bin:$PATH'
    dest: /etc/profile.d/redis.sh
[root@centos8 ansible]# vim roles/redis/templates/redis.conf.j2
bind {{ bind }}
protected-mode yes
port {{ port }}
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize {{ daemonize }}
supervised no
pidfile {{ pidfile }}
loglevel notice
logfile ""
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 {{ dbfilename }}
dir {{ dir }}
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
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 yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
[root@centos8 ansible]# vim roles/redis/templates/redis.service.j2 

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart= {{ execstart }} {{ conf }} --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
[root@centos8 ansible]# vim roles/redis/tests/inventory 
localhost
[root@centos8 ansible]# vim roles/redis/tests/test.yml 

---
- hosts: localhost
  remote_user: root
  roles:
    - redis
[root@centos8 ansible]# vim roles/redis/vars/main.yml 

---
bind: 0.0.0.0
port: 6379
daemonize: "yes"
pidfile: /apps/redis/run/redis.pid
dbfilename: dumnp.rdb
dir: /apps/redis/data/
execstart: /apps/redis/bin/redis-server
conf: /apps/redis/etc/redis.conf

 

posted @ 2020-10-20 10:32  养了27年的狗  阅读(135)  评论(0编辑  收藏  举报
Live2D