...

Redis Exporter Setup through Ansible Using Role in 6 Easy Steps

Rate this post

Introduction

Simplify redis exporter setup through Ansible using role. Redis exporter exports redis metrics for Prometheus.

Generally, we install redis_exporter on the redis server. Prometheus service periodically pulls redis metrics through redis_exporter. Ansible facilitates and automates redis_exporter setup at scale.

Please Review this post to learn about redis_exporter setup with ansible using role.

Redis exporter setup through ansible using role
Redis exporter setup with Ansible

Prerequisite:

Redis exporter is an agent installed on the redis server through which Prometheus scrapes metrics for redis.

Major steps in the redis exporter setup through Ansible using Role are below

  • Create redis-exporter role
  • Download redis_exporter
  • Extract the redis_exporter binary and move to /usr/bin or /usr/local/bin
  • Create redis_exporter user
  • Customise redis_exporter service conf
  • Restart redis_exporter service

Let us understand the above sub-tasks in more detail.

Create redis_exporter role

cd ansible
# creates roles dir if if does not exists as below
mkdir roles
cd roles
ansible-galaxy role init redis_exporter

Download redis_exporter

Note: version is variable in above task, please add version in defaults/main.yml as below version: 1.55.0

Add the below task in redis_exporter/tasks/main.yml

- name: Download redis exporter
  command: wget https://github.com/oliver006/redis_exporter/releases/download/v{{version}}/redis_exporter-v{{version}}.linux-amd64.tar.gz

Extract the redis_exporter binary and move to /usr/local/bin

# Extract the archive

- name: Extract redis exporter archieve
  command: tar xvfz redis_exporter-v{{version}}.linux-amd64.tar.gz

# Move binary to /usr/local/bin (Make sure /usr/local/bin path is added into PATH)

- name: Move redis-exporter to /usr/bin
  command: mv redis_exporter-v{{version}}.linux-amd64/redis_exporter /usr/local/bin

# Delete archive and extracted directory

- name: Delete redis_exporter-v{{version}}.linux-amd64.tar.gz
  file:
    name: redis_exporter-v{{version}}.linux-amd64.tar.gz
    state: absent

- name: Delete redis_exporter-v{{version}}.linux-amd64 directory
  file:
    name: redis_exporter-v{{version}}.linux-amd64
    state: absent

Create redis_exporter user

- name: Create redis_exporter user
  user:
    name: redis_exporter
    system: yes
    create_home: no
    shell: /bin/false

Customise redis_exporter service conf

Create redis_exporter service template
touch templates/redis_exporter.service.j2
Add below conf in templates/redis_exporter.service.j2
######################################
[Unit]
Description=Redis Exporter
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=redis_exporter
ExecStart=/usr/bin/redis_exporter \
    -web.listen-address ":9121" \
    -redis.addr "redis://127.0.0.1:{{redis_port}}" 

[Install]
WantedBy=multi-user.target
######################################
Note: Add redis_port value in  defaults/main.yml as 6379
Also, if redis is password protected then change ExecStart as below and add password value defaults/main.yml

ExecStart=/usr/bin/redis_exporter \
    -web.listen-address ":9121" \
    -redis.addr "redis://127.0.0.1:{{redis_port}}" \
    -redis.password "{{password}}"

Add below task in tasks/main.yml

- name: Copy redis_exporter.service file to /etc/systemd/system
   template:
     src: redis_exporter.service.j2
     dest: /etc/systemd/system/redis_exporter.service
   notify:     - Restart redis_exporter

Restart redis_exporter service

Add below handler in handlers/main.yml

- name: Restart redis_exporter
  systemd:
    name: redis_exporter
    state: restarted
    enabled: yes
    daemon_reload: yes

Finally, tasks/main.yml will be as below

---
# tasks file for redis_exporter

- name: Extract redis exporter archieve
  command: tar xvfz redis_exporter-v{{version}}.linux-amd64.tar.gz

- name: Move redis-exporter to /usr/bin
  command: mv redis_exporter-v{{version}}.linux-amd64/redis_exporter /usr/bin/

- name: Delete redis_exporter-v{{version}}.linux-amd64.tar.gz
  file:
    name: redis_exporter-v{{version}}.linux-amd64.tar.gz
    state: absent

- name: Delete redis_exporter-v{{version}}.linux-amd64 directory
  file:
    name: redis_exporter-v{{version}}.linux-amd64
    state: absent

- name: Create redis_exporter user
  user:
    name: redis_exporter
    system: yes
    create_home: no
    shell: /bin/false

- name: Restart redis_exporter
  systemd:
    name: redis_exporter
    state: restarted
    enabled: yes
    daemon_reload: yes

the final version of defaults/main.yml

---
# defaults file for redis_exporter

version: 1.55.0
redis_port: 6379
password: myredispass

Inventory setup

Create inventory/prod/redis_servers.yml

Add below contents

all:
  children:
    redis_servers:
     hosts:
       redis_server_01:
         private_ip: 10.15.0.2
       redis_server_02:
         private_ip: 10.15.0.3

Master playbook setup

Create the redis_servers.yml file in the playbooks folder and add the below content.

# redis_servers.yml

- name: Redis server setup
  hosts: redis_servers
  become: yes
  roles:
    - ../roles/redis_exporter

Run the playbook

cd playbooks
ansible-playbook redis_servers.yml  -i inventory/prod/redis_servers.yml

Upon successful execution of the above command, you can check redis metrics in Prometheus or Grafana.

Conclusion

In this post, we learned about the redis exporter setup with Ansible.

In conclusion, the streamlined deployment of the Redis Exporter setup with Ansible, distilled into a straightforward three-step process, exemplifies the elegance of automation. This approach emphasizes the potency of Ansible’s role-based orchestration and underscores its user-friendly design.

By simplifying the deployment journey, users can seamlessly integrate Redis Exporter into their systems efficiently and easily, allowing them to focus more on broader aspects of system management. This method encapsulates the core philosophy of Ansible—delivering powerful automation that is both accessible and precise, ensuring that users can effortlessly enhance their systems with the advanced capabilities of Redis Exporter.

In essence, this streamlined deployment method is a testament to Ansible’s commitment to user-centric automation, where complexity is abstracted, and efficiency is prioritized, facilitating a more intuitive and productive system management experience for all.

Please enjoy the post!!

Spread the love

1 thought on “Redis Exporter Setup through Ansible Using Role in 6 Easy Steps”

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.