From 0360573ed2a2bd8e61aaea3918b8813951576677 Mon Sep 17 00:00:00 2001 From: DaazKu Date: Wed, 4 Dec 2019 17:18:28 -0500 Subject: [PATCH 1/8] add option to propagate binaries without access to internet Same as cloudalchemy/ansible-alertmanager#83 and cloudalchemy/ansible-prometheus#239 but for Node exporter. --- README.md | 1 + defaults/main.yml | 1 + molecule/alternative/playbook.yml | 1 + molecule/alternative/prepare.yml | 36 +++++++++++++++++-- tasks/install.yml | 58 +++++++++++++++++++------------ tasks/preflight.yml | 9 +++-- 6 files changed, 79 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 12f34ef..214f0ba 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ All variables which can be overridden are stored in [defaults/main.yml](defaults | Name | Default Value | Description | | -------------- | ------------- | -----------------------------------| | `node_exporter_version` | 0.18.1 | Node exporter package version. Also accepts latest as parameter. | +| `node_exporter_binaries_local_dir` | "" | Allows to use local packages instead of ones distributed on github. As parameter it takes a directory where `node_exporter` binary is stored on host on which ansible is ran. This overrides `node_exporter_version` parameter | | `node_exporter_web_listen_address` | "0.0.0.0:9100" | Address on which node exporter will listen | | `node_exporter_system_group` | "node-exp" | System group used to run node_exporter | | `node_exporter_system_user` | "node-exp" | System user used to run node_exporter | diff --git a/defaults/main.yml b/defaults/main.yml index 605468c..f4634a1 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,5 +1,6 @@ --- node_exporter_version: 0.18.1 +node_exporter_binary_local_dir: '' node_exporter_web_listen_address: "0.0.0.0:9100" node_exporter_system_group: "node-exp" diff --git a/molecule/alternative/playbook.yml b/molecule/alternative/playbook.yml index 9c9ffe1..99d2aca 100644 --- a/molecule/alternative/playbook.yml +++ b/molecule/alternative/playbook.yml @@ -5,6 +5,7 @@ roles: - ansible-node-exporter vars: + node_exporter_binary_local_dir: '/tmp/node-exporter-linux-amd64' node_exporter_system_group: "root" node_exporter_system_user: "root" node_exporter_textfile_dir: "" diff --git a/molecule/alternative/prepare.yml b/molecule/alternative/prepare.yml index 5358b3b..7933ccb 100644 --- a/molecule/alternative/prepare.yml +++ b/molecule/alternative/prepare.yml @@ -1,5 +1,37 @@ --- - name: Prepare - hosts: all + hosts: localhost gather_facts: false - tasks: [] + vars: + # Version seeds to be specified here as molecule doesn't have access to ansible_version at this stage + version: 0.18.1 + tasks: + - name: Download node_exporter binary to local folder + become: false + get_url: + url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp/node_exporter-{{ version }}.linux-{{ go_arch }}.tar.gz" + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + run_once: true + check_mode: false + + - name: Unpack node_exporter binary + become: false + unarchive: + src: "/tmp/node_exporter-{{ version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp" + creates: "/tmp/node_exporter-{{ version }}.linux-{{ go_arch }}/node_exporter" + run_once: true + check_mode: false + + - name: link to alertmanager binaries directory + become: false + file: + src: "/tmp/node_exporter-{{ version }}.linux-amd64" + dest: "/tmp/node_exporter-linux-amd64" + state: link + run_once: true + check_mode: false diff --git a/tasks/install.yml b/tasks/install.yml index b28c84d..34f59bb 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -27,34 +27,46 @@ home: / when: node_exporter_system_user != "root" -- name: Download node_exporter binary to local folder - become: false - get_url: - url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" - dest: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" - checksum: "sha256:{{ node_exporter_checksum }}" - register: _download_binary - until: _download_binary is succeeded - retries: 5 - delay: 2 - delegate_to: localhost - check_mode: false +- block: + - name: Download node_exporter binary to local folder + become: false + get_url: + url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" + checksum: "sha256:{{ node_exporter_checksum }}" + register: _download_binary + until: _download_binary is succeeded + retries: 5 + delay: 2 + delegate_to: localhost + check_mode: false + + - name: Unpack node_exporter binary + become: false + unarchive: + src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp" + creates: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/node_exporter" + delegate_to: localhost + check_mode: false -- name: Unpack node_exporter binary - become: false - unarchive: - src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" - dest: "/tmp" - creates: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/node_exporter" - delegate_to: localhost - check_mode: false + - name: Propagate node_exporter binaries + copy: + src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/node_exporter" + dest: "/usr/local/bin/node_exporter" + mode: 0755 + owner: root + group: root + notify: restart node_exporter + when: not ansible_check_mode + when: node_exporter_binary_local_dir | length = 0 -- name: Propagate node_exporter binaries +- name: propagate locally distributed node_exporter binary copy: - src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/node_exporter" + src: "{{ node_exporter_binary_local_dir }}/node_exporter" dest: "/usr/local/bin/node_exporter" mode: 0755 owner: root group: root + when: node_exporter_binary_local_dir | length > 0 notify: restart node_exporter - when: not ansible_check_mode diff --git a/tasks/preflight.yml b/tasks/preflight.yml index c1b60db..f572892 100644 --- a/tasks/preflight.yml +++ b/tasks/preflight.yml @@ -66,7 +66,9 @@ - name: "Set node_exporter version to {{ _latest_release.json.tag_name[1:] }}" set_fact: node_exporter_version: "{{ _latest_release.json.tag_name[1:] }}" - when: node_exporter_version == "latest" + when: + - node_exporter_version == "latest" + - node_exporter_binary_local_dir | length == 0 delegate_to: localhost run_once: true @@ -74,9 +76,12 @@ set_fact: _checksums: "{{ lookup('url', 'https://github.com/prometheus/node_exporter/releases/download/v' + node_exporter_version + '/sha256sums.txt', wantlist=True) | list }}" run_once: true + when: node_exporter_binary_local_dir | length == 0 - name: "Get checksum for {{ go_arch }} architecture" set_fact: node_exporter_checksum: "{{ item.split(' ')[0] }}" with_items: "{{ _checksums }}" - when: "('linux-' + go_arch + '.tar.gz') in item" + when: + - "('linux-' + go_arch + '.tar.gz') in item" + - node_exporter_binary_local_dir | length == 0 From 01ae89bc6ad7306f0c0c807d98e0f357d48a44fa Mon Sep 17 00:00:00 2001 From: DaazKu Date: Wed, 4 Dec 2019 17:29:53 -0500 Subject: [PATCH 2/8] Fix typo --- tasks/install.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/install.yml b/tasks/install.yml index 34f59bb..8b6d1b2 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -59,7 +59,7 @@ group: root notify: restart node_exporter when: not ansible_check_mode - when: node_exporter_binary_local_dir | length = 0 + when: node_exporter_binary_local_dir | length == 0 - name: propagate locally distributed node_exporter binary copy: From 5aefe6bfdefaf03decc8c3769a125293aa9db939 Mon Sep 17 00:00:00 2001 From: DaazKu Date: Wed, 4 Dec 2019 17:50:08 -0500 Subject: [PATCH 3/8] Fix molecule/alternative/prepare.yml --- molecule/alternative/prepare.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/molecule/alternative/prepare.yml b/molecule/alternative/prepare.yml index 7933ccb..45391cf 100644 --- a/molecule/alternative/prepare.yml +++ b/molecule/alternative/prepare.yml @@ -9,8 +9,8 @@ - name: Download node_exporter binary to local folder become: false get_url: - url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" - dest: "/tmp/node_exporter-{{ version }}.linux-{{ go_arch }}.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-{{ version }}.linux-amd64.tar.gz" register: _download_binary until: _download_binary is succeeded retries: 5 @@ -21,9 +21,9 @@ - name: Unpack node_exporter binary become: false unarchive: - src: "/tmp/node_exporter-{{ version }}.linux-{{ go_arch }}.tar.gz" + src: "/tmp/node_exporter-{{ version }}.linux-amd64.tar.gz" dest: "/tmp" - creates: "/tmp/node_exporter-{{ version }}.linux-{{ go_arch }}/node_exporter" + creates: "/tmp/node_exporter-{{ version }}.linux-amd64/node_exporter" run_once: true check_mode: false From 23920aaa728af5dea7864a445032b1f6cfecda9d Mon Sep 17 00:00:00 2001 From: DaazKu Date: Wed, 4 Dec 2019 17:50:54 -0500 Subject: [PATCH 4/8] Fix molecule/alternative/prepare.yml again :D --- molecule/alternative/prepare.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/alternative/prepare.yml b/molecule/alternative/prepare.yml index 45391cf..f993691 100644 --- a/molecule/alternative/prepare.yml +++ b/molecule/alternative/prepare.yml @@ -9,7 +9,7 @@ - name: Download node_exporter binary to local folder become: false get_url: - url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-amd64.tar.gz" + url: "https://github.com/prometheus/node_exporter/releases/download/v{{ version }}/node_exporter-{{ version }}.linux-amd64.tar.gz" dest: "/tmp/node_exporter-{{ version }}.linux-amd64.tar.gz" register: _download_binary until: _download_binary is succeeded From c406dc6f6c8e045272cbc46efae7a086efe1640e Mon Sep 17 00:00:00 2001 From: DaazKu Date: Wed, 4 Dec 2019 22:53:32 -0500 Subject: [PATCH 5/8] Fix _checksum not being defined --- molecule/alternative/playbook.yml | 2 +- molecule/alternative/prepare.yml | 14 +++++++------- tasks/preflight.yml | 24 ++++++++++++------------ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/molecule/alternative/playbook.yml b/molecule/alternative/playbook.yml index 99d2aca..238fbbd 100644 --- a/molecule/alternative/playbook.yml +++ b/molecule/alternative/playbook.yml @@ -5,7 +5,7 @@ roles: - ansible-node-exporter vars: - node_exporter_binary_local_dir: '/tmp/node-exporter-linux-amd64' + node_exporter_binary_local_dir: "/tmp/node_exporter-linux-amd64" node_exporter_system_group: "root" node_exporter_system_user: "root" node_exporter_textfile_dir: "" diff --git a/molecule/alternative/prepare.yml b/molecule/alternative/prepare.yml index f993691..ed65ff5 100644 --- a/molecule/alternative/prepare.yml +++ b/molecule/alternative/prepare.yml @@ -3,14 +3,14 @@ hosts: localhost gather_facts: false vars: - # Version seeds to be specified here as molecule doesn't have access to ansible_version at this stage - version: 0.18.1 + go_arch: amd64 + node_eqxporter_version: 0.18.1 tasks: - name: Download node_exporter binary to local folder become: false get_url: - url: "https://github.com/prometheus/node_exporter/releases/download/v{{ version }}/node_exporter-{{ version }}.linux-amd64.tar.gz" - dest: "/tmp/node_exporter-{{ version }}.linux-amd64.tar.gz" + url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" + dest: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" register: _download_binary until: _download_binary is succeeded retries: 5 @@ -21,16 +21,16 @@ - name: Unpack node_exporter binary become: false unarchive: - src: "/tmp/node_exporter-{{ version }}.linux-amd64.tar.gz" + src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}.tar.gz" dest: "/tmp" - creates: "/tmp/node_exporter-{{ version }}.linux-amd64/node_exporter" + creates: "/tmp/node_exporter-{{ node_exporter_version }}.linux-{{ go_arch }}/node_exporter" run_once: true check_mode: false - name: link to alertmanager binaries directory become: false file: - src: "/tmp/node_exporter-{{ version }}.linux-amd64" + src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-amd64" dest: "/tmp/node_exporter-linux-amd64" state: link run_once: true diff --git a/tasks/preflight.yml b/tasks/preflight.yml index f572892..5f44b01 100644 --- a/tasks/preflight.yml +++ b/tasks/preflight.yml @@ -72,16 +72,16 @@ delegate_to: localhost run_once: true -- name: Get checksum list from github - set_fact: - _checksums: "{{ lookup('url', 'https://github.com/prometheus/node_exporter/releases/download/v' + node_exporter_version + '/sha256sums.txt', wantlist=True) | list }}" - run_once: true - when: node_exporter_binary_local_dir | length == 0 +- block: + - name: Get checksum list from github + set_fact: + _checksums: "{{ lookup('url', 'https://github.com/prometheus/node_exporter/releases/download/v' + node_exporter_version + '/sha256sums.txt', wantlist=True) | list }}" + run_once: true -- name: "Get checksum for {{ go_arch }} architecture" - set_fact: - node_exporter_checksum: "{{ item.split(' ')[0] }}" - with_items: "{{ _checksums }}" - when: - - "('linux-' + go_arch + '.tar.gz') in item" - - node_exporter_binary_local_dir | length == 0 + - name: "Get checksum for {{ go_arch }} architecture" + set_fact: + node_exporter_checksum: "{{ item.split(' ')[0] }}" + with_items: "{{ _checksums }}" + when: + - "('linux-' + go_arch + '.tar.gz') in item" + when: node_exporter_binary_local_dir | length == 0 From cb206696e7b705809f6821046bb0144d52173d31 Mon Sep 17 00:00:00 2001 From: DaazKu Date: Wed, 4 Dec 2019 22:57:10 -0500 Subject: [PATCH 6/8] Use double quotes for consistency --- defaults/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/defaults/main.yml b/defaults/main.yml index f4634a1..723ffff 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,6 +1,6 @@ --- node_exporter_version: 0.18.1 -node_exporter_binary_local_dir: '' +node_exporter_binary_local_dir: "" node_exporter_web_listen_address: "0.0.0.0:9100" node_exporter_system_group: "node-exp" From a3ecf85881e973d91ba9d94f85f2a576fb0d8fea Mon Sep 17 00:00:00 2001 From: DaazKu Date: Wed, 4 Dec 2019 23:05:58 -0500 Subject: [PATCH 7/8] Fix typo... --- molecule/alternative/prepare.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/alternative/prepare.yml b/molecule/alternative/prepare.yml index ed65ff5..c6ceefa 100644 --- a/molecule/alternative/prepare.yml +++ b/molecule/alternative/prepare.yml @@ -4,7 +4,7 @@ gather_facts: false vars: go_arch: amd64 - node_eqxporter_version: 0.18.1 + node_exporter_version: 0.18.1 tasks: - name: Download node_exporter binary to local folder become: false From ed04aeb435411fce8683c887dbc6c82dd5d374c1 Mon Sep 17 00:00:00 2001 From: DaazKu Date: Wed, 4 Dec 2019 23:37:56 -0500 Subject: [PATCH 8/8] Fix c/p error --- molecule/alternative/prepare.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/alternative/prepare.yml b/molecule/alternative/prepare.yml index c6ceefa..2f75da1 100644 --- a/molecule/alternative/prepare.yml +++ b/molecule/alternative/prepare.yml @@ -27,7 +27,7 @@ run_once: true check_mode: false - - name: link to alertmanager binaries directory + - name: link to node_exporter binaries directory become: false file: src: "/tmp/node_exporter-{{ node_exporter_version }}.linux-amd64"