Operations 4 min read

Step-by-Step Guide to Building an Ansible Role for Redis Installation

This tutorial walks through creating the directory structure, defining variables, writing task files, and executing an Ansible playbook to automate the installation, compilation, configuration, and startup of Redis on target hosts.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Step-by-Step Guide to Building an Ansible Role for Redis Installation

The article demonstrates how to set up an Ansible role for Redis by first creating the required directory hierarchy (

mkdir redis/{default,files,handlers,meta,tasks,templates,vars} -p

) and showing the resulting paths for files and templates directories.

Next, it provides a sample playbook redis_install.yml that targets hosts, uses the redis role, and specifies remote_user: root:

---
- hosts: ms
  remote_user: root
  roles:
    - role: redis

The role’s vars/main.yml file defines key variables such as redis_data_dir: /data/redis/data, redis_version: redis-7.2.0, var_dir: /data/redis, and source_dir: /tmp.

In tasks/main.yml the role includes install.yml, which contains a series of tasks: creating the data directory, copying the Redis tarball, unarchiving it, compiling the source, creating bin and conf directories, copying binaries, templating redis.conf, and finally starting the Redis server. Example task snippets:

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

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

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

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

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

- name: 拷贝文件到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"

The role is executed with Ansible using a dry‑run command ( ansible-playbook -C redis_install.yml) followed by the actual run ( ansible-playbook redis_install.yml), with screenshots illustrating the output.

Finally, the article includes a brief promotional section encouraging readers to like, share, and follow the author.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

AutomationredisDevOpsYAMLInfrastructureAnsible
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.