Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
ismoilovdevml committed Sep 17, 2024
1 parent 2322135 commit 8842f77
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 75 deletions.
99 changes: 99 additions & 0 deletions Ansible/redis/install_redis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
- name: Install and configure Redis on multiple OS
hosts: all
become: yes
tasks:
- name: Gather OS facts
ansible.builtin.setup:
filter: ansible_distribution*

# Install Redis on Debian/Ubuntu
- name: Install prerequisites (Debian/Ubuntu)
apt:
name:
- lsb-release
- curl
- gpg
state: present
when: ansible_distribution in ['Debian', 'Ubuntu']

- name: Add GPG key for Redis (Debian/Ubuntu)
ansible.builtin.shell: |
curl -fsSL https://packages.redis.io/gpg | gpg --dearmor > /usr/share/keyrings/redis-archive-keyring.gpg
when: ansible_distribution in ['Debian', 'Ubuntu']

- name: Add Redis APT repository (Debian/Ubuntu)
ansible.builtin.shell: |
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list
when: ansible_distribution in ['Debian', 'Ubuntu']

- name: Update APT and install Redis (Debian/Ubuntu)
apt:
name: redis-server
update_cache: yes
state: present
when: ansible_distribution in ['Debian', 'Ubuntu']

# Install Redis on RHEL/CentOS
- name: Add Redis YUM repository and GPG key (RHEL/CentOS)
block:
- name: Add Redis YUM repository
copy:
content: |
[Redis]
name=Redis
baseurl=https://packages.redis.io/rpm/rhel8
enabled=1
gpgcheck=1
gpgkey=https://packages.redis.io/gpg
dest: /etc/yum.repos.d/redis.repo
mode: '0644'

- name: Install Redis
yum:
name: redis
state: present
update_cache: yes
when: ansible_distribution in ['CentOS', 'RedHat']

# Ensure Redis is enabled and started
- name: Ensure Redis service is started and enabled
systemd:
name: "{{ 'redis-server' if ansible_distribution in ['Debian', 'Ubuntu'] else 'redis' }}"
state: started
enabled: yes

# Ensure Redis configuration file exists on RHEL/CentOS
- name: Create Redis configuration directory (RHEL/CentOS)
file:
path: /etc/redis
state: directory
when: ansible_distribution in ['CentOS', 'RedHat']

- name: Ensure Redis configuration file exists (RHEL/CentOS)
file:
path: /etc/redis/redis.conf
state: touch
when: ansible_distribution in ['CentOS', 'RedHat']

# Configure Redis to listen on all interfaces
- name: Configure Redis to listen on all interfaces
lineinfile:
path: /etc/redis/redis.conf
regexp: "^bind 127.0.0.1"
line: "bind 0.0.0.0"
notify: Restart Redis

# Set protected-mode to no
- name: Set protected-mode to no
lineinfile:
path: /etc/redis/redis.conf
regexp: "^protected-mode"
line: "protected-mode no"
notify: Restart Redis

handlers:
- name: Restart Redis
systemd:
name: "{{ 'redis-server' if ansible_distribution in ['Debian', 'Ubuntu'] else 'redis' }}"
state: restarted
75 changes: 0 additions & 75 deletions Ansible/redis/redis_install.yml

This file was deleted.

95 changes: 95 additions & 0 deletions Ansible/redis/uninstall_redis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
- name: Uninstall and clean up Redis on multiple OS
hosts: all
become: yes
tasks:
# Check if Redis is installed
- name: Check if Redis is installed
ansible.builtin.command: which redis-server
register: redis_installed
ignore_errors: true
failed_when: false
changed_when: false

# Stop Redis service only if installed
- name: Stop Redis service if installed
ansible.builtin.systemd:
name: "{{ 'redis-server' if ansible_distribution in ['Debian', 'Ubuntu'] else 'redis' }}"
state: stopped
when: redis_installed.rc == 0
ignore_errors: yes

# Uninstall Redis packages (Debian/Ubuntu)
- name: Remove Redis packages (Debian/Ubuntu)
ansible.builtin.apt:
name: redis-server
state: absent
purge: yes
when: redis_installed.rc == 0 and ansible_distribution in ['Debian', 'Ubuntu']

# Uninstall Redis packages (RHEL/CentOS)
- name: Remove Redis packages (RHEL/CentOS)
ansible.builtin.yum:
name: redis
state: absent
when: redis_installed.rc == 0 and ansible_distribution in ['CentOS', 'RedHat']

# Remove Redis user only if it exists
- name: Remove Redis user
ansible.builtin.user:
name: redis
state: absent
ignore_errors: yes

# Remove Redis group only if it exists
- name: Remove Redis group
ansible.builtin.group:
name: redis
state: absent
ignore_errors: yes

# Remove Redis data, logs, and configuration directories only if they exist
- name: Remove Redis data, logs, and configuration directories
ansible.builtin.file:
path: "{{ item }}"
state: absent
recurse: yes
loop:
- /var/lib/redis
- /var/log/redis
- /etc/redis
- /var/run/redis
when: redis_installed.rc == 0
ignore_errors: yes

# Remove Redis APT repository list
- name: Remove Redis APT repository list (Debian/Ubuntu)
ansible.builtin.file:
path: /etc/apt/sources.list.d/redis.list
state: absent
when: ansible_distribution in ['Debian', 'Ubuntu']
ignore_errors: yes

# Remove Redis GPG key (Debian/Ubuntu)
- name: Remove Redis GPG key (Debian/Ubuntu)
ansible.builtin.apt_key:
id: "9DC858229FC7DD38854AE2D88D81803C0EBFCD88"
state: absent
when: ansible_distribution in ['Debian', 'Ubuntu']
ignore_errors: yes

# Remove Redis YUM repository (RHEL/CentOS/Fedora)
- name: Remove Redis YUM repository
ansible.builtin.file:
path: /etc/yum.repos.d/redis.repo
state: absent
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']
ignore_errors: yes

# Remove Redis GPG key (RHEL/CentOS/Fedora)
- name: Remove Redis GPG key (RHEL/CentOS/Fedora)
ansible.builtin.rpm_key:
key: https://packages.redis.io/gpg
state: absent
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']
ignore_errors: yes

0 comments on commit 8842f77

Please sign in to comment.