Skip to content

Commit

Permalink
[NEW] added grafana stack ansible playbook
Browse files Browse the repository at this point in the history
  • Loading branch information
ismoilovdevml committed Sep 9, 2024
1 parent ca7d90f commit d3126b9
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 0 deletions.
87 changes: 87 additions & 0 deletions Ansible/grafana-stack/grafana.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
- name: Install Grafana
hosts: grafana
become: true
tasks:
- name: Install dependencies (Debian/Ubuntu)
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- apt-transport-https
- software-properties-common
- wget
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Add Grafana GPG key and repository (Debian/Ubuntu)
block:
- name: Add Grafana GPG key
ansible.builtin.shell: |
mkdir -p /etc/apt/keyrings/
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor | tee /etc/apt/keyrings/grafana.gpg > /dev/null
- name: Add Grafana repository
ansible.builtin.apt_repository:
repo: "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main"
state: present

- name: Update APT cache and install Grafana
ansible.builtin.apt:
update_cache: yes
name: grafana
state: present
when: ansible_distribution in ['Ubuntu', 'Debian']

- name: Install Grafana (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: Add Grafana repository
ansible.builtin.copy:
dest: /etc/yum.repos.d/grafana.repo
content: |
[grafana]
name=grafana
baseurl=https://rpm.grafana.com
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://rpm.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
- name: Install Grafana
ansible.builtin.yum:
name: grafana
state: present
when: ansible_distribution in ['CentOS', 'RedHat', 'Fedora']

- name: Install Grafana (SUSE/openSUSE)
block:
- name: Add Grafana GPG key
ansible.builtin.shell: |
wget -q -O gpg.key https://rpm.grafana.com/gpg.key
rpm --import gpg.key
- name: Add Grafana repository
ansible.builtin.zypper_repository:
name: grafana
description: "Grafana repository"
baseurl: https://rpm.grafana.com
enabled: yes
autorefresh: yes

- name: Install Grafana
ansible.builtin.zypper:
name: grafana
state: present
when: ansible_distribution in ['openSUSE', 'SUSE Linux Enterprise Server']

- name: Enable and start Grafana
ansible.builtin.systemd:
name: grafana-server
enabled: yes
state: started
10 changes: 10 additions & 0 deletions Ansible/grafana-stack/inventory.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[prometheus]
server3 ansible_host=34.170.180.55 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

[grafana]
server2 ansible_host=34.27.32.115 ansible_user=ismoilovdev
4 changes: 4 additions & 0 deletions Ansible/grafana-stack/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- import_playbook: prometheus.yml
- import_playbook: node_exporter.yml
- import_playbook: grafana.yml
58 changes: 58 additions & 0 deletions Ansible/grafana-stack/node_exporter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
- name: Install Node Exporter on Node Exporter Servers
hosts: node_exporter
become: yes
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"

- name: Extract Node Exporter archive
ansible.builtin.unarchive:
src: "/tmp/node_exporter-1.8.2.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/

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

- name: Create node_exporter user
ansible.builtin.user:
name: node_exporter
shell: /bin/false
system: yes

- name: Create Node Exporter systemd service
ansible.builtin.copy:
dest: /etc/systemd/system/node_exporter.service
content: |
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
- name: Reload systemd daemon
ansible.builtin.systemd:
daemon_reload: yes

- name: Enable and start Node Exporter service
ansible.builtin.systemd:
name: node_exporter
enabled: yes
state: started
98 changes: 98 additions & 0 deletions Ansible/grafana-stack/prometheus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
- name: Install Prometheus on Prometheus Servers
hosts: prometheus
become: yes
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"

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

- name: Create Prometheus directories
ansible.builtin.file:
path: "{{ item }}"
state: directory
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

- name: Move prometheus.yml
ansible.builtin.copy:
src: /tmp/prometheus-2.54.1.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

- 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

- name: Set permissions for Prometheus
ansible.builtin.command: chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus

- name: Create Prometheus systemd service
ansible.builtin.copy:
dest: /etc/systemd/system/prometheus.service
content: |
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries \
--web.listen-address=0.0.0.0:9090 \
--web.enable-lifecycle \
--log.level=info
[Install]
WantedBy=multi-user.target
- name: Reload systemd daemon
ansible.builtin.systemd:
daemon_reload: yes

- name: Enable and start Prometheus
ansible.builtin.systemd:
name: prometheus
enabled: yes
state: started

handlers:
- name: reload_prometheus
ansible.builtin.systemd:
name: prometheus
state: restarted

0 comments on commit d3126b9

Please sign in to comment.