From dec38d63327c676898b99438193b68007b6ac0a4 Mon Sep 17 00:00:00 2001 From: wangjian Date: Fri, 5 Dec 2025 09:56:03 +0800 Subject: [PATCH] add grafana tasks and variable --- automation/deploy/ansible/group_vars/all | 7 +- .../ansible/roles/grafana/tasks/main.yml | 70 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 automation/deploy/ansible/roles/grafana/tasks/main.yml diff --git a/automation/deploy/ansible/group_vars/all b/automation/deploy/ansible/group_vars/all index 4aa36b5c..e34335ce 100644 --- a/automation/deploy/ansible/group_vars/all +++ b/automation/deploy/ansible/group_vars/all @@ -2,4 +2,9 @@ nginx_conf_path: /etc/nginx/nginx.conf nginx_web_path: /usr/share/nginx/html nginx_worker_processes: 512 -nginx_worker_connections: 65535 \ No newline at end of file +nginx_worker_connections: 65535 + +grafana_repo_url: https://packages.grafana.com/oss/rpm +grafana_gpg_key: https://packages.grafana.com/gpg.key +grafana_package: grafana{{ ('-' + grafana_version) if grafana_version else '' }} +grafana_service: grafana-server diff --git a/automation/deploy/ansible/roles/grafana/tasks/main.yml b/automation/deploy/ansible/roles/grafana/tasks/main.yml new file mode 100644 index 00000000..c0c2acc1 --- /dev/null +++ b/automation/deploy/ansible/roles/grafana/tasks/main.yml @@ -0,0 +1,70 @@ +# roles/grafana/tasks/main.yml +--- +- name: Include OS-specific variables + include_vars: "{{ ansible_os_family }}.yml" + +- name: Add Grafana GPG key (RedHat) + rpm_key: + key: "{{ grafana_gpg_key }}" + state: present + when: ansible_os_family == "RedHat" + +- name: Add Grafana YUM repository (RedHat / OpenEuler) + yum_repository: + name: grafana + description: Grafana OSS Repository + baseurl: "{{ grafana_repo_url }}" + gpgcheck: yes + gpgkey: "{{ grafana_gpg_key }}" + enabled: yes + when: ansible_os_family == "RedHat" + +- name: Add Grafana GPG key (Debian) + apt_key: + url: "{{ grafana_gpg_key }}" + state: present + when: ansible_os_family == "Debian" + +- name: Add Grafana APT repository (Debian/Ubuntu) + apt_repository: + repo: "{{ grafana_repo_url }}" + state: present + filename: grafana + when: ansible_os_family == "Debian" + +- name: Install Grafana package + package: + name: "{{ grafana_package }}" + state: present + notify: restart grafana + +- name: Configure Grafana from template + template: + src: grafana.ini.j2 + dest: "{{ grafana_config_file }}" + owner: root + group: root + mode: "0644" + when: grafana_custom_config | bool + notify: restart grafana + +- name: Ensure firewalld allows Grafana port (OpenEuler / RHEL) + firewalld: + port: "{{ grafana_port }}/tcp" + permanent: yes + state: enabled + immediate: yes + when: ansible_os_family == "RedHat" and ansible_facts['distribution'] == "openEuler" + +- name: Start and enable Grafana service + systemd: + name: "{{ grafana_service }}" + state: started + enabled: yes + +# roles/grafana/handlers/main.yml +- name: restart grafana + systemd: + name: "{{ grafana_service }}" + state: restarted + daemon_reload: yes -- Gitee