diff --git a/tasks/exporter/_common.yml b/tasks/exporter/_common.yml deleted file mode 100644 index adb3469..0000000 --- a/tasks/exporter/_common.yml +++ /dev/null @@ -1,130 +0,0 @@ ---- - -# to build prometheus repo for another var: https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names -- set_fact: - prometheus_repo: "{{ lookup('vars', 'prometheus_repo_' ~ exporter) }}" - -#- name: debug -# debug: -# msg: "Repo: {{ prometheus_repo }}" - -- name: '{{ exporter }} exporter | check if exporter is already install' - stat: - path: /usr/local/bin/prometheus-{{ exporter }}-exporter - register: stat_result - tags: [ 'prometheus_{{ exporter }}' ] - -- name: '{{ exporter }} exporter | stop old version' - systemd: - name: prometheus-{{ exporter }}-exporter.service - state: stopped - when: exporter_upgrade == "yes" - tags: [ 'prometheus_{{ exporter }}' ] - -- name: '{{ exporter }} exporter | remove old version' - file: - path: /usr/local/bin/prometheus-{{ exporter }}-exporter - state: absent - when: exporter_upgrade == "yes" - tags: [ 'prometheus_{{ exporter }}' ] - -# If prometheus_exporters_sources == "repo" -- name: '{{ exporter }} exporter | download from repo' - get_url: - url: "{{prometheus_exporters_repo}}/releases/download/{{prometheus_exporters_release}}/{{exporter}}_exporter" - dest: /usr/local/bin/prometheus-{{ exporter }}-exporter - remote_src: yes - owner: root - group: root - mode: 0755 - ignore_errors: '{{ ansible_check_mode }}' - when: > - (not stat_result.stat.exists or exporter_upgrade == "yes") and - prometheus_exporters_sources == "repo" - tags: [ 'prometheus_{{ exporter }}' ] - -# If prometheus_exporters_sources == "origin" -- name: '{{ exporter }} exporter | create temp path' - file: - state: directory - path: /tmp/prometheus_exporter/{{ exporter }} - when: > - (not stat_result.stat.exists or exporter_upgrade == "yes") and - prometheus_exporters_sources == "origin" - tags: [ 'prometheus_{{ exporter }}' ] - -- name: '{{ exporter }} exporter | get latest release from GitHub' - shell: curl --silent "https://api.github.com/repos/{{ prometheus_repo }}/{{ exporter }}_exporter/releases/latest" | jq -r .tag_name | sed "s/v//" - register: prometheus_exporter_latest_version - run_once: true - when: > - (not stat_result.stat.exists or exporter_upgrade == "yes") and - prometheus_exporters_sources == "origin" - tags: [ 'prometheus_{{ exporter }}' ] - -- name: '{{ exporter }} exporter | download from origin' - get_url: - url: "https://github.com/{{ prometheus_repo }}/{{ exporter }}_exporter/releases/download/v{{ prometheus_exporter_latest_version.stdout }}/{{ exporter }}_exporter-{{ prometheus_exporter_latest_version.stdout }}.{{ prometheus_os }}-{{ prometheus_arch }}.tar.gz" - dest: /tmp/prometheus_exporter/{{ exporter }}/ - ignore_errors: '{{ ansible_check_mode }}' - when: > - (not stat_result.stat.exists or exporter_upgrade == "yes") and - prometheus_exporters_sources == "origin" - tags: [ 'prometheus_{{ exporter }}' ] - -- name: '{{ exporter }} exporter | unarchive' - unarchive: - remote_src: yes - src: /tmp/prometheus_exporter/{{ exporter }}/{{ exporter }}_exporter-{{ prometheus_exporter_latest_version.stdout }}.{{ prometheus_os }}-{{ prometheus_arch }}.tar.gz - dest: /tmp/prometheus_exporter/{{ exporter }}/ - when: > - (not stat_result.stat.exists or exporter_upgrade == "yes") and - prometheus_exporters_sources == "origin" - ignore_errors: '{{ ansible_check_mode }}' - tags: [ 'prometheus_{{ exporter }}' ] - -- name: '{{ exporter }} exporter | move to /usr/local/bin' - copy: - src: /tmp/prometheus_exporter/{{ exporter }}/{{ exporter }}_exporter-{{ prometheus_exporter_latest_version.stdout }}.{{ prometheus_os }}-{{ prometheus_arch }}/{{ exporter }}_exporter - dest: /usr/local/bin/prometheus-{{ exporter }}-exporter - remote_src: yes - owner: root - group: root - mode: 0755 - notify: - - '{{ exporter }}-exporter-restart' - when: > - (not stat_result.stat.exists or exporter_upgrade == "yes") and - prometheus_exporters_sources == "origin" - tags: [ 'prometheus_{{ exporter }}' ] - ignore_errors: '{{ ansible_check_mode }}' - -- name: '{{ exporter }} exporter | cleanup temp path' - file: - state: absent - path: /tmp/prometheus_exporter/{{ exporter }} - when: > - (not stat_result.stat.exists or exporter_upgrade == "yes") and - prometheus_exporters_sources == "origin" - -- name: '{{ exporter }} exporter | systemd | copy unit file' - template: - src: systemd/prometheus-{{ exporter }}-exporter.service.j2 - dest: /etc/systemd/system/prometheus-{{ exporter }}-exporter.service - owner: root - group: root - mode: 0600 - notify: - - '{{ exporter }}-exporter-restart' - when: not stat_result.stat.exists or exporter_upgrade == "yes" - ignore_errors: '{{ ansible_check_mode }}' - tags: [ 'prometheus_{{ exporter }}' ] - -- name: '{{ exporter }} exporter | systemd | enable service' - systemd: - daemon_reload: yes - enabled: yes - name: prometheus-{{ exporter }}-exporter.service - ignore_errors: '{{ ansible_check_mode }}' - when: not stat_result.stat.exists or exporter_upgrade == "yes" - tags: [ 'prometheus_{{ exporter }}' ] diff --git a/tasks/exporter/common.yml b/tasks/exporter/common.yml new file mode 100644 index 0000000..830f278 --- /dev/null +++ b/tasks/exporter/common.yml @@ -0,0 +1,47 @@ +--- +- name: '{{ exporter }} exporter | check if exporter is already install' + stat: + path: /usr/local/bin/prometheus-{{ exporter }}-exporter + register: stat_result + tags: [ 'prometheus_{{ exporter }}' ] + +- name '{{ exporter }} exporter | remove old exporter version' + include_tasks: "upgrade.yml" + when: stat_result.stat.exists and exporter_upgrade == "yes" + tags: [ 'prometheus_{{ exporter }}' ] + +- name '{{ exporter }} exporter | install from origin' + include_tasks: "origin.yml" + when: > + (not stat_result.stat.exists or exporter_upgrade == "yes") and + prometheus_exporters_sources == "origin" + tags: [ 'prometheus_{{ exporter }}' ] + +- name '{{ exporter }} exporter | install from repo' + include_tasks: "repo.yml" + when: > + (not stat_result.stat.exists or exporter_upgrade == "yes") and + prometheus_exporters_sources == "repo" + tags: [ 'prometheus_{{ exporter }}' ] + +- name: '{{ exporter }} exporter | systemd | copy unit file' + template: + src: systemd/prometheus-{{ exporter }}-exporter.service.j2 + dest: /etc/systemd/system/prometheus-{{ exporter }}-exporter.service + owner: root + group: root + mode: 0600 + notify: + - '{{ exporter }}-exporter-restart' + when: not stat_result.stat.exists or exporter_upgrade == "yes" + ignore_errors: '{{ ansible_check_mode }}' + tags: [ 'prometheus_{{ exporter }}' ] + +- name: '{{ exporter }} exporter | systemd | enable service' + systemd: + daemon_reload: yes + enabled: yes + name: prometheus-{{ exporter }}-exporter.service + ignore_errors: '{{ ansible_check_mode }}' + when: not stat_result.stat.exists or exporter_upgrade == "yes" + tags: [ 'prometheus_{{ exporter }}' ] diff --git a/tasks/exporter/_docker.yml b/tasks/exporter/docker.yml similarity index 98% rename from tasks/exporter/_docker.yml rename to tasks/exporter/docker.yml index 5b122d4..50f5a50 100644 --- a/tasks/exporter/_docker.yml +++ b/tasks/exporter/docker.yml @@ -8,7 +8,7 @@ failed_when: result.rc != 0 and result.rc != 127 with_items: - docker - - docker-compose + - docker-compose-plugin tags: [ 'prometheus_{{ exporter }}' ] - name: '{{ exporter }} exporter | create docker-compose directory' diff --git a/tasks/exporter/origin.yml b/tasks/exporter/origin.yml new file mode 100644 index 0000000..07f7ec4 --- /dev/null +++ b/tasks/exporter/origin.yml @@ -0,0 +1,50 @@ +--- + +# to build prometheus repo for another var: https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names +- set_fact: + prometheus_repo: "{{ lookup('vars', 'prometheus_repo_' ~ exporter) }}" + +- name: '{{ exporter }} exporter | create temp path' + file: + state: directory + path: /tmp/prometheus_exporter/{{ exporter }} + tags: [ 'prometheus_{{ exporter }}' ] + +- name: '{{ exporter }} exporter | get latest release from GitHub' + shell: curl --silent "https://api.github.com/repos/{{ prometheus_repo }}/{{ exporter }}_exporter/releases/latest" | jq -r .tag_name | sed "s/v//" + register: prometheus_exporter_latest_version + run_once: true + tags: [ 'prometheus_{{ exporter }}' ] + +- name: '{{ exporter }} exporter | download from origin' + get_url: + url: "https://github.com/{{ prometheus_repo }}/{{ exporter }}_exporter/releases/download/v{{ prometheus_exporter_latest_version.stdout }}/{{ exporter }}_exporter-{{ prometheus_exporter_latest_version.stdout }}.{{ prometheus_os }}-{{ prometheus_arch }}.tar.gz" + dest: /tmp/prometheus_exporter/{{ exporter }}/ + ignore_errors: '{{ ansible_check_mode }}' + tags: [ 'prometheus_{{ exporter }}' ] + +- name: '{{ exporter }} exporter | unarchive' + unarchive: + remote_src: yes + src: /tmp/prometheus_exporter/{{ exporter }}/{{ exporter }}_exporter-{{ prometheus_exporter_latest_version.stdout }}.{{ prometheus_os }}-{{ prometheus_arch }}.tar.gz + dest: /tmp/prometheus_exporter/{{ exporter }}/ + ignore_errors: '{{ ansible_check_mode }}' + tags: [ 'prometheus_{{ exporter }}' ] + +- name: '{{ exporter }} exporter | move to /usr/local/bin' + copy: + src: /tmp/prometheus_exporter/{{ exporter }}/{{ exporter }}_exporter-{{ prometheus_exporter_latest_version.stdout }}.{{ prometheus_os }}-{{ prometheus_arch }}/{{ exporter }}_exporter + dest: /usr/local/bin/prometheus-{{ exporter }}-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + notify: + - '{{ exporter }}-exporter-restart' + tags: [ 'prometheus_{{ exporter }}' ] + ignore_errors: '{{ ansible_check_mode }}' + +- name: '{{ exporter }} exporter | cleanup temp path' + file: + state: absent + path: /tmp/prometheus_exporter/{{ exporter }} diff --git a/tasks/exporter/repo.yml b/tasks/exporter/repo.yml new file mode 100644 index 0000000..f284e44 --- /dev/null +++ b/tasks/exporter/repo.yml @@ -0,0 +1,11 @@ +--- +- name: '{{ exporter }} exporter | download from repo' + get_url: + url: "{{prometheus_exporters_repo}}/releases/download/{{prometheus_exporters_release}}/{{exporter}}_exporter" + dest: /usr/local/bin/prometheus-{{ exporter }}-exporter + remote_src: yes + owner: root + group: root + mode: 0755 + ignore_errors: '{{ ansible_check_mode }}' + tags: [ 'prometheus_{{ exporter }}' ] diff --git a/tasks/exporter/apache.yml b/tasks/exporter/services/apache.yml similarity index 58% rename from tasks/exporter/apache.yml rename to tasks/exporter/services/apache.yml index e0efbd1..765571d 100644 --- a/tasks/exporter/apache.yml +++ b/tasks/exporter/services/apache.yml @@ -1,3 +1,3 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_apache_exporter'] diff --git a/tasks/exporter/blackbox.yml b/tasks/exporter/services/blackbox.yml similarity index 92% rename from tasks/exporter/blackbox.yml rename to tasks/exporter/services/blackbox.yml index fca1155..3dda9d3 100644 --- a/tasks/exporter/blackbox.yml +++ b/tasks/exporter/services/blackbox.yml @@ -1,5 +1,5 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_blackbox_exporter'] - name: 'blackbox exporter | configuration' diff --git a/tasks/exporter/cloudflare.yml b/tasks/exporter/services/cloudflare.yml similarity index 85% rename from tasks/exporter/cloudflare.yml rename to tasks/exporter/services/cloudflare.yml index bb9d98f..e490a69 100644 --- a/tasks/exporter/cloudflare.yml +++ b/tasks/exporter/services/cloudflare.yml @@ -5,5 +5,5 @@ - cloudflare_api_key is defined tags: ['prometheus_cloudflare'] -- include_tasks: _docker.yml +- include_tasks: ../docker.yml tags: ['prometheus_cloudflare'] diff --git a/tasks/exporter/memcached.yml b/tasks/exporter/services/memcached.yml similarity index 59% rename from tasks/exporter/memcached.yml rename to tasks/exporter/services/memcached.yml index 263453e..98b3b57 100644 --- a/tasks/exporter/memcached.yml +++ b/tasks/exporter/services/memcached.yml @@ -1,3 +1,3 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_memcached_exporter'] diff --git a/tasks/exporter/mongodb.yml b/tasks/exporter/services/mongodb.yml similarity index 95% rename from tasks/exporter/mongodb.yml rename to tasks/exporter/services/mongodb.yml index 6f385fb..263a82b 100644 --- a/tasks/exporter/mongodb.yml +++ b/tasks/exporter/services/mongodb.yml @@ -1,5 +1,5 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_mongodb_exporter'] - name: 'mongodb exporter < v0.2 | move to /usr/local/bin' diff --git a/tasks/exporter/mysqld.yml b/tasks/exporter/services/mysqld.yml similarity index 93% rename from tasks/exporter/mysqld.yml rename to tasks/exporter/services/mysqld.yml index 4c90f54..2309a9b 100644 --- a/tasks/exporter/mysqld.yml +++ b/tasks/exporter/services/mysqld.yml @@ -1,5 +1,5 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_mysqld_exporter'] - name: 'mysqld exporter | configuration' diff --git a/tasks/exporter/nginx.yml b/tasks/exporter/services/nginx.yml similarity index 57% rename from tasks/exporter/nginx.yml rename to tasks/exporter/services/nginx.yml index 5789c1b..7476fe9 100644 --- a/tasks/exporter/nginx.yml +++ b/tasks/exporter/services/nginx.yml @@ -1,3 +1,3 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_nginx_exporter'] diff --git a/tasks/exporter/node.yml b/tasks/exporter/services/node.yml similarity index 56% rename from tasks/exporter/node.yml rename to tasks/exporter/services/node.yml index 417928c..8323f68 100644 --- a/tasks/exporter/node.yml +++ b/tasks/exporter/services/node.yml @@ -1,3 +1,3 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_node_exporter'] diff --git a/tasks/exporter/opcache.yml b/tasks/exporter/services/opcache.yml similarity index 92% rename from tasks/exporter/opcache.yml rename to tasks/exporter/services/opcache.yml index 2650697..1b728a1 100644 --- a/tasks/exporter/opcache.yml +++ b/tasks/exporter/services/opcache.yml @@ -1,5 +1,5 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_opcache_exporter'] - name: 'opcache exporter | configuration' diff --git a/tasks/exporter/phpfpm.yml b/tasks/exporter/services/phpfpm.yml similarity index 91% rename from tasks/exporter/phpfpm.yml rename to tasks/exporter/services/phpfpm.yml index 84216fc..ab8fe5e 100644 --- a/tasks/exporter/phpfpm.yml +++ b/tasks/exporter/services/phpfpm.yml @@ -1,5 +1,5 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_phpfpm_exporter'] - name: 'phpfpm exporter | install startup script' diff --git a/tasks/exporter/postgres.yml b/tasks/exporter/services/postgres.yml similarity index 92% rename from tasks/exporter/postgres.yml rename to tasks/exporter/services/postgres.yml index 0582d22..683ea36 100644 --- a/tasks/exporter/postgres.yml +++ b/tasks/exporter/services/postgres.yml @@ -1,5 +1,5 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_postgres_exporter'] - name: 'postgres exporter | configuration' diff --git a/tasks/exporter/proxmox.yml b/tasks/exporter/services/proxmox.yml similarity index 100% rename from tasks/exporter/proxmox.yml rename to tasks/exporter/services/proxmox.yml diff --git a/tasks/exporter/redis.yml b/tasks/exporter/services/redis.yml similarity index 92% rename from tasks/exporter/redis.yml rename to tasks/exporter/services/redis.yml index 023667e..6608f71 100644 --- a/tasks/exporter/redis.yml +++ b/tasks/exporter/services/redis.yml @@ -1,5 +1,5 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_redis_exporter'] - name: 'redis exporter | configuration' diff --git a/tasks/exporter/varnish.yml b/tasks/exporter/services/varnish.yml similarity index 58% rename from tasks/exporter/varnish.yml rename to tasks/exporter/services/varnish.yml index 3c31843..22218c8 100644 --- a/tasks/exporter/varnish.yml +++ b/tasks/exporter/services/varnish.yml @@ -1,3 +1,3 @@ --- -- include_tasks: _common.yml +- include_tasks: ../common.yml tags: ['prometheus_varnish_exporter'] diff --git a/tasks/exporter/upgrade.yml b/tasks/exporter/upgrade.yml new file mode 100644 index 0000000..21d5984 --- /dev/null +++ b/tasks/exporter/upgrade.yml @@ -0,0 +1,12 @@ +--- +- name: '{{ exporter }} exporter | stop old version' + systemd: + name: prometheus-{{ exporter }}-exporter.service + state: stopped + tags: [ 'prometheus_{{ exporter }}' ] + +- name: '{{ exporter }} exporter | remove old version' + file: + path: /usr/local/bin/prometheus-{{ exporter }}-exporter + state: absent + tags: [ 'prometheus_{{ exporter }}' ] diff --git a/tasks/main.yml b/tasks/main.yml index 88fefcd..6ae0968 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -24,7 +24,7 @@ - name: 'exporters install' vars: exporter: "{{ item }}" - include_tasks: "exporter/{{ item }}.yml" + include_tasks: "exporter/services/{{ item }}.yml" tags: - prometheus_node_exporter - prometheus_mysqld_exporter