使用Ansible在Debian/Ubuntu上安装最新的Prometheus Node Exporter
我尝试在Ubuntu上安装node exporter,但是使用apt install prometheus-node-exporter无法安装最新的node exporter。为了安装最新的node exporter,需要从GitHub上下载二进制文件。
由于检查最新版本很麻烦,所以我创建了一个用于安装最新版本的Ansible playbook。
(2023/8/8时刻,使用apt install命令安装的版本是1.5.0。在GitHub上的最新发布版本是1.6.1。)
我按照以下页面的手顺进行了Prometheus Node Exporter的设置。
---
- name: groupadd -f --system node_exporter
ansible.builtin.group:
name: node_exporter
state: present
system: true
- name: useradd -g node_exporter --no-create-home --shell /bin/false node_exporter
ansible.builtin.user:
name: node_exporter
group: node_exporter
state: present
shell: /bin/false
create_home: false
system: true
- name: mkdir -p /etc/node_exporter; chown node_exporter:node_exporter /etc/node_exporter
ansible.builtin.file:
path: /etc/node_exporter
state: directory
owner: node_exporter
group: node_exporter
mode: 0755
- name: Read GitHub API (latest release info)
ansible.builtin.uri:
url: https://api.github.com/repos/prometheus/node_exporter/releases/latest
register: _result_response
- name: set_fact `_tag_name`
ansible.builtin.set_fact:
_tag_name: "{{ _result_response.json['tag_name'] }}"
- name: node_exporter - set `_dict_file_name`
ansible.builtin.set_fact:
_dict_file_name:
x86_64: "node_exporter-{{ _tag_name | regex_replace('^v?(.*)$', '\\1') }}.linux-amd64"
aarch64: "node_exporter-{{ _tag_name | regex_replace('^v?(.*)$', '\\1') }}.linux-arm64"
- name: Show the value of `_dict_file_name`
ansible.builtin.debug:
var: _dict_file_name
- name: Unarchive `node_exporter-${VERSION}.linux-<ARCH>.tar.gz` and save it in `/tmp`
ansible.builtin.unarchive:
remote_src: yes
src: "\
https://github.com/prometheus/node_exporter/releases/download/\
{{ _tag_name }}/{{ _dict_file_name[ansible_architecture] }}.tar.gz"
dest: /tmp
- name: Copy `/tmp/node_exporter-${VERSION}.linux-<ARCH>./node_exporter` to `/usr/bin/node_exporter`
ansible.builtin.copy:
remote_src: yes
src: "/tmp/{{ _dict_file_name[ansible_architecture] }}/node_exporter"
dest: /usr/bin/node_exporter
owner: node_exporter
group: node_exporter
mode: +x
- name: Copy `./node_exporter.service` to `/usr/lib/systemd/system/node_exporter.service`
ansible.builtin.copy:
remote_src: no
src: "{{ playbook_dir }}/tasks/node_exporter.service"
dest: /usr/lib/systemd/system
- name: systemctl daemon-reload; systemctl enable --now node_exporter.service
ansible.builtin.systemd:
name: node_exporter.service
daemon_reload: true
scope: system
state: started
enabled: yes
- name: rm -r "/tmp/node_exporter-${VERSION}.linux-<ARCH>
ansible.builtin.file:
path: "/tmp/{{ _dict_file_name[ansible_architecture] }}/"
state: absent
[Unit]
Description=Node Exporter
Documentation=https://prometheus.io/docs/guides/node-exporter/
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
Restart=on-failure
ExecStart=/usr/bin/node_exporter
[Install]
WantedBy=multi-user.target