--- - name: "Install | Create Prometheus system user" ansible.builtin.user: name: "prometheus" system: true shell: "/sbin/nologin" create_home: false state: present - name: "Install | Create directories {{ item }}" loop: - "{{ prometheus_config_dir }}" - "{{ prometheus_config_dir }}/rules" - "{{ prometheus_db_dir }}" ansible.builtin.file: path: "{{ item }}" state: directory owner: "prometheus" group: "prometheus" mode: "0755" - name: "Install | Install the binaries" block: - name: "Install | Check Prometheus version" changed_when: false ansible.builtin.command: cmd: "prometheus --version" register: prometheus_ver - name: "Install | Assert version correctness" ansible.builtin.assert: that: "prometheus_ver.stdout is regex('{{ prometheus_version }}')" success_msg: "prometheus version {{ prometheus_version }} is installed and working" fail_msg: "prometheus version {{ prometheus_version }} is not installed or not working correctly" rescue: - name: "Install | Fetch and unpack the distribution" notify: "(Re)start Prometheus service" ansible.builtin.unarchive: src: "https://github.com/prometheus/prometheus/releases/download/v{{ prometheus_version }}/prometheus-{{ prometheus_version }}.linux-amd64.tar.gz" dest: "/tmp" creates: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64" remote_src: true - name: "Install | Put the binaries under the PATH" with_items: - prometheus - promtool ansible.builtin.copy: remote_src: true src: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64/{{ item }}" dest: "/usr/bin/{{ item }}" owner: "prometheus" group: "prometheus" mode: "0755" - name: "Install | Clean up the downloads" ansible.builtin.file: path: "/tmp/prometheus-{{ prometheus_version }}.linux-amd64" state: absent - name: "Install | Create a systemd service unit" notify: "(Re)start Prometheus service" ansible.builtin.template: src: prometheus.service.j2 dest: /etc/systemd/system/prometheus.service owner: "prometheus" group: "prometheus" mode: "0660"