Skip to content

Commit

Permalink
[FIX] bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ismoilovdevml committed Sep 11, 2024
1 parent b2ad2e7 commit f1f8200
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 58 deletions.
4 changes: 4 additions & 0 deletions Ansible/grafana-stack/install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- import_playbook: install_prometheus.yml
- import_playbook: install_node_exporter.yml
- import_playbook: install_grafana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
hosts: grafana
become: true
tasks:

- name: Install dependencies (Debian/Ubuntu)
ansible.builtin.apt:
name: "{{ item }}"
Expand Down Expand Up @@ -32,12 +33,14 @@
state: present
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Install Grafana (CentOS/RHEL/Fedora)
- name: Install dependencies (CentOS/RHEL/Fedora)
block:
- name: Add Grafana GPG key
ansible.builtin.shell: |
wget -q -O /etc/yum.repos.d/grafana.repo https://rpm.grafana.com/gpg.key
rpm --import /etc/yum.repos.d/grafana.repo
- name: Install wget and chkconfig
ansible.builtin.yum:
name:
- wget
- chkconfig
state: present

- name: Add Grafana repository
ansible.builtin.copy:
Expand Down Expand Up @@ -84,4 +87,4 @@
ansible.builtin.systemd:
name: grafana-server
enabled: yes
state: started
state: started
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
- name: Install Node Exporter on Node Exporter Servers
hosts: node_exporter
become: yes
vars_files:
- vars.yml
tasks:

- name: Download Node Exporter archive
ansible.builtin.get_url:
url: "https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz"
dest: "/tmp/node_exporter-1.8.2.linux-amd64.tar.gz"
url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-amd64.tar.gz"
dest: "/tmp/node_exporter-{{ node_exporter_version }}.linux-amd64.tar.gz"

- name: Extract Node Exporter archive
ansible.builtin.unarchive:
src: "/tmp/node_exporter-1.8.2.linux-amd64.tar.gz"
src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-amd64.tar.gz"
dest: "/tmp"
remote_src: yes

- name: Move Node Exporter binary
ansible.builtin.command: mv /tmp/node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/
ansible.builtin.command: mv /tmp/node_exporter-{{ node_exporter_version }}.linux-amd64/node_exporter /usr/local/bin/

- name: Clean up Node Exporter archive
ansible.builtin.command: rm -rf /tmp/node_exporter-1.8.2.linux-amd64*
ansible.builtin.command: rm -rf /tmp/node_exporter-{{ node_exporter_version }}.linux-amd64*

- name: Create node_exporter user
ansible.builtin.user:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,56 @@
- name: Install Prometheus on Prometheus Servers
hosts: prometheus
become: yes
vars_files:
- vars.yml
tasks:

- name: Download Prometheus archive
ansible.builtin.get_url:
url: "https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz"
dest: "/tmp/prometheus-2.54.1.linux-amd64.tar.gz"
url: "https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"
dest: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"

- name: Extract Prometheus archive
ansible.builtin.unarchive:
src: "/tmp/prometheus-2.54.1.linux-amd64.tar.gz"
src: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz"
dest: "/tmp"
remote_src: yes

- name: Create Prometheus directories
- name: Move Prometheus binary
ansible.builtin.command: mv /tmp/prometheus-{{ prometheus_version }}.linux-amd64/prometheus /usr/local/bin/prometheus
notify: reload_prometheus

- name: Ensure Prometheus binary is executable
ansible.builtin.file:
path: "{{ item }}"
state: directory
path: /usr/local/bin/prometheus
mode: '0755'
owner: prometheus
group: prometheus
mode: '0755'
loop:
- /etc/prometheus
- /var/lib/prometheus

- name: Move Prometheus binaries
ansible.builtin.command: mv /tmp/prometheus-2.54.1.linux-amd64/prometheus /usr/local/bin/prometheus
notify: reload_prometheus

- name: Move Promtool
ansible.builtin.command: mv /tmp/prometheus-2.54.1.linux-amd64/promtool /usr/local/bin/promtool
ansible.builtin.command: mv /tmp/prometheus-{{ prometheus_version }}.linux-amd64/promtool /usr/local/bin/promtool

- name: Move prometheus.yml
ansible.builtin.copy:
src: /tmp/prometheus-2.54.1.linux-amd64/prometheus.yml
src: /tmp/prometheus-{{ prometheus_version }}.linux-amd64/prometheus.yml
dest: /etc/prometheus/prometheus.yml
remote_src: yes

- name: Move console files
ansible.builtin.command: mv /tmp/prometheus-2.54.1.linux-amd64/consoles /etc/prometheus/consoles
ansible.builtin.command: mv /tmp/prometheus-{{ prometheus_version }}.linux-amd64/consoles /etc/prometheus/consoles

- name: Move console libraries
ansible.builtin.command: mv /tmp/prometheus-2.54.1.linux-amd64/console_libraries /etc/prometheus/console_libraries

- name: Create prometheus user
ansible.builtin.user:
name: prometheus
shell: /bin/false
system: yes
ansible.builtin.command: mv /tmp/prometheus-{{ prometheus_version }}.linux-amd64/console_libraries /etc/prometheus/console_libraries

- name: Set permissions for Prometheus
ansible.builtin.command: chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
- name: Set permissions for Prometheus directories
ansible.builtin.file:
path: "{{ item }}"
owner: prometheus
group: prometheus
recurse: yes
loop:
- /etc/prometheus
- /var/lib/prometheus

- name: Create Prometheus systemd service
ansible.builtin.copy:
Expand Down Expand Up @@ -89,10 +88,4 @@
ansible.builtin.systemd:
name: prometheus
enabled: yes
state: started

handlers:
- name: reload_prometheus
ansible.builtin.systemd:
name: prometheus
state: restarted
state: started
14 changes: 9 additions & 5 deletions Ansible/grafana-stack/inventory.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[prometheus]
server3 ansible_host=34.170.180.55 ansible_user=ismoilovdev
debian ansible_host=34.170.180.55 ansible_user=ismoilovdev
ubuntu ansible_host=34.27.32.115 ansible_user=ismoilovdev
centos ansible_host=34.69.104.233 ansible_user=ismoilovdev

[node_exporter]
server1 ansible_host=34.69.104.233 ansible_user=ismoilovdev
server2 ansible_host=34.27.32.115 ansible_user=ismoilovdev
server3 ansible_host=34.170.180.55 ansible_user=ismoilovdev
debian ansible_host=34.170.180.55 ansible_user=ismoilovdev
ubuntu ansible_host=34.27.32.115 ansible_user=ismoilovdev
centos ansible_host=34.69.104.233 ansible_user=ismoilovdev

[grafana]
server2 ansible_host=34.27.32.115 ansible_user=ismoilovdev
debian ansible_host=34.170.180.55 ansible_user=ismoilovdev
ubuntu ansible_host=34.27.32.115 ansible_user=ismoilovdev
centos ansible_host=34.69.104.233 ansible_user=ismoilovdev
4 changes: 0 additions & 4 deletions Ansible/grafana-stack/main.yml

This file was deleted.

4 changes: 4 additions & 0 deletions Ansible/grafana-stack/uninstall.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- import_playbook: uninstall_prometheus.yml
- import_playbook: uninstall_node_exporter.yml
- import_playbook: uninstall_grafana.yml
100 changes: 100 additions & 0 deletions Ansible/grafana-stack/uninstall_grafana.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
---
- name: Uninstall Grafana and cleanup
hosts: grafana
become: true
tasks:

- name: Check if Grafana service is present
ansible.builtin.systemd:
name: grafana-server
state: stopped
register: grafana_service_status
ignore_errors: yes
failed_when: false

- name: Stop Grafana service if it exists
ansible.builtin.systemd:
name: grafana-server
state: stopped
when: grafana_service_status.failed is not defined

- name: Remove Grafana package (Debian/Ubuntu)
ansible.builtin.apt:
name: grafana
state: absent
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Remove Grafana package (CentOS/RHEL/Fedora)
ansible.builtin.yum:
name: grafana
state: absent
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']

- name: Remove Grafana package (SUSE/openSUSE)
ansible.builtin.zypper:
name: grafana
state: absent
when: ansible_distribution in ['openSUSE', 'SUSE Linux Enterprise Server']

- name: Remove Grafana repository (Debian/Ubuntu)
ansible.builtin.apt_repository:
repo: "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main"
state: absent
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Remove Grafana repository (CentOS/RHEL/Fedora)
ansible.builtin.file:
path: /etc/yum.repos.d/grafana.repo
state: absent
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']

- name: Remove Grafana repository (SUSE/openSUSE)
ansible.builtin.zypper_repository:
name: grafana
state: absent
when: ansible_distribution in ['openSUSE', 'SUSE Linux Enterprise Server']

- name: Remove Grafana configuration and data directories
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop:
- /etc/grafana
- /var/lib/grafana
- /var/log/grafana

- name: Clean up Grafana GPG key (Debian/Ubuntu)
ansible.builtin.file:
path: /etc/apt/keyrings/grafana.gpg
state: absent
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Find all installed GPG keys (CentOS/RHEL/Fedora)
ansible.builtin.shell: "rpm -q gpg-pubkey --qf '%{VERSION}-%{RELEASE}\\n'"
register: gpg_keys
changed_when: false
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']

- name: Clean up Grafana GPG keys (CentOS/RHEL/Fedora)
ansible.builtin.shell: |
rpm --erase gpg-pubkey-{{ item }}
loop: "{{ gpg_keys.stdout_lines }}"
when:
- ansible_distribution in ['CentOS', 'RedHat', 'Fedora']
- gpg_keys.stdout != ''
- item != ''
ignore_errors: yes

- name: Check if Grafana GPG key exists (SUSE/openSUSE)
ansible.builtin.shell: "rpm -q gpg-pubkey"
register: gpg_key_check_suse
changed_when: false
ignore_errors: yes
when: ansible_distribution in ['openSUSE', 'SUSE Linux Enterprise Server']

- name: Clean up Grafana GPG key (SUSE/openSUSE) if it exists
ansible.builtin.shell: |
rpm --erase gpg-pubkey-$(rpm -q --qf "%{version}-%{release}" gpg-pubkey*)
when:
- ansible_distribution in ['openSUSE', 'SUSE Linux Enterprise Server']
- gpg_key_check_suse.rc == 0
76 changes: 76 additions & 0 deletions Ansible/grafana-stack/uninstall_node_exporter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
- name: Uninstall Node Exporter and cleanup
hosts: node_exporter
become: yes
vars_files:
- vars.yml
tasks:

- name: Check if Node Exporter service is present
ansible.builtin.systemd:
name: node_exporter
state: stopped
register: node_exporter_service_status
ignore_errors: yes
failed_when: false

- name: Stop Node Exporter service if it exists
ansible.builtin.systemd:
name: node_exporter
state: stopped
when: node_exporter_service_status.failed is not defined

- name: Disable Node Exporter service if it exists
ansible.builtin.systemd:
name: node_exporter
enabled: no
when: node_exporter_service_status.failed is not defined

- name: Remove Node Exporter systemd service file
ansible.builtin.file:
path: /etc/systemd/system/node_exporter.service
state: absent

- name: Reload systemd daemon to remove Node Exporter service
ansible.builtin.systemd:
daemon_reload: yes

- name: Remove Node Exporter binary
ansible.builtin.file:
path: /usr/local/bin/node_exporter
state: absent

- name: Remove Node Exporter user
ansible.builtin.user:
name: node_exporter
state: absent
remove: yes

- name: Remove /tmp directory and any remaining files
ansible.builtin.file:
path: /tmp/node_exporter-{{ node_exporter_version }}.linux-amd64*
state: absent

- name: Clean up Node Exporter cache (if exists)
ansible.builtin.file:
path: /var/cache/node_exporter
state: absent

- name: Remove Node Exporter logs (if exists)
ansible.builtin.file:
path: /var/log/node_exporter
state: absent

- name: Remove any additional Node Exporter related directories
ansible.builtin.find:
paths:
- "/tmp"
- "/usr/local"
patterns: "node_exporter*"
register: node_exporter_dirs

- name: Clean found Node Exporter directories
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ node_exporter_dirs.files }}"
Loading

0 comments on commit f1f8200

Please sign in to comment.