Hi Readers, this document deals with how to setup ansible role for redis server.
Ansible is open source software that automates software provisioning, configuration management, and application deployment. Ansible connects via SSH, remote PowerShell or via other remote APIs.

If you haven’t installed Ansible check here/

Here is the setup.

Step 1: initialize role set up for Redis server using ansible galaxy

ansible-galaxy init redisserver

once the command is executed, folder structures are automatically created for the role.

└──╼ $tree redisserver/
redisserver/
├── defaults
│   └── main.yml
├── files
│   ├── redisCentos.sh
│   └── redisdebian.sh
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   ├── centos.yml
│   ├── main.yml
│   └── ubuntu.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
└── main.yml

Step 2: We are installing Redis from source, here is the script for both Debian and redhat flavor. Now create scripts for both the files

redisserver/files/redisCentos.sh

vi redisCentos.sh

#!/usr/bin/bash
yum install -y wget gcc make tcl
cd /tmp
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar -xvzf redis-5.0.0.tar.gz
cd redis-5.0.0/
make
make test
make install
mkdir /etc/redis
mkdir -p /var/redis
cp redis.conf /etc/redis/


cat <<EOT >> /etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=root
Group=root
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
Type=Forking
[Install]
WantedBy=multi-user.target
EOT

service redis start
service redis status

redisserver/files/redisdebian.sh

vi redisdebian.sh

 

#!/usr/bin/bash
sudo apt-get -y install build-essential tcl
cd /tmp
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar -xvzf redis-5.0.0.tar.gz
cd redis-5.0.0/
make
make test
make install
mkdir /etc/redis
mkdir -p /var/redis
cp redis.conf /etc/redis/


cat <<EOT >> /etc/systemd/system/redis.service
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=root
Group=root
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
Type=Forking
[Install]
WantedBy=multi-user.target
EOT
service redis start
service redis status

Step 3: Add the handler to restart the service

redisserver/handlers/main.yml

- name: start
  service: name=redis state=restarted

Step 4: Create task main.yml

redisserver/tasks/main.yml

# tasks file for redisserver
 - include_tasks: ubuntu.yml
   when: ansible_os_family == 'Debian'
 - include_tasks: centos.yml
   when: ansible_os_family == 'RedHat'  
   

Step 5: create a centos.yml file in tasks
redisserver/tasks/centos.yml

 - name: copy the rpm file /tmp
   copy: src=files/redisCentos.sh dest=/tmp/redis.sh mode=0777
 - name: Execute the source installer script
   command: sh /tmp/redis.sh
 - name: change the bind
   replace:
     path: /etc/redis/redis.conf
     regexp: 'bind 127.0.0.1'
     replace: 'bind 0.0.0.0'
   notify:
   - start

Step 6: redisserver/tasks/ubuntu.yml

# tasks file for rediuscentos
 - name: copy the rpm file /tmp
   copy: src=files/redisdebian.sh dest=/tmp/redis.sh mode=0777
 - name: Execute the source installer script
   command: sh /tmp/redis.sh
 - name: change the bind
   replace:
     path: /etc/redis/redis.conf
     regexp: 'bind 127.0.0.1'
     replace: 'bind 0.0.0.0'
   notify:
   - start

That’s it, now we have completed role creation for Redis server.

Now create a file for playbook for a host using role.

Step 7:redis.yml

---
 - hosts: local
   gather_facts: true
   become: true
   become_user: root
   become_method: sudo
   roles:
    - redisserver

 

Now run your play book by executing the below command,

ansible-playbook redis.yml

© 2018, Techrunnr. All rights reserved.

#1
#2
#3
Questions Answered
Articles Written
Overall Points

Prabhin Prabharkaran

He is Technical professional. He is a person who loves to share tricks and tips on the Internet. He Posts what he does!!

Leave a Reply

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.