---
- name: Get systemd version
  command: systemctl --version
  changed_when: false
  check_mode: false
  register: __systemd_version
  tags:
    - skip_ansible_lint

- name: Set systemd version fact
  set_fact:
    prometheus_systemd_version: "{{ __systemd_version.stdout_lines[0].split(' ')[-1] }}"

- name: Assert no duplicate config flags
  assert:
    that:
      - prometheus_config_flags_extra['config.file'] is not defined
      - prometheus_config_flags_extra['storage.tsdb.path'] is not defined
      - prometheus_config_flags_extra['storage.local.path'] is not defined
      - prometheus_config_flags_extra['web.listen-address'] is not defined
      - prometheus_config_flags_extra['web.external-url'] is not defined
    msg: "Detected duplicate configuration entry. Please check your ansible variables and role README.md."

- name: Assert external_labels aren't configured twice
  assert:
    that: prometheus_global.external_labels is not defined
    msg: "Use prometheus_external_labels to define external labels"

- name: Set prometheus external metrics path
  set_fact:
    prometheus_metrics_path: "/{{ ( prometheus_web_external_url + '/metrics' ) | regex_replace('^(.*://)?(.*?)/') }}"

- name: Fail when prometheus_config_flags_extra duplicates parameters set by other variables
  fail:
    msg: >
      Whooops. You are duplicating configuration. Please look at your prometheus_config_flags_extra
      and check against other variables in defaults/main.yml
  with_items:
    - 'storage.tsdb.retention'
    - 'storage.tsdb.path'
    - 'storage.local.retention'
    - 'storage.local.path'
    - 'config.file'
    - 'web.listen-address'
    - 'web.external-url'
  when: item in prometheus_config_flags_extra.keys()

- name: Get all file_sd files from scrape_configs
  set_fact:
    file_sd_files: "{{ prometheus_scrape_configs | json_query('[*][].file_sd_configs[*][].files[]') }}"

- name: Fail when file_sd targets are not defined in scrape_configs
  fail:
    msg: >
      Oh, snap! `{{ item.key }}` couldn't be found in your scrape configs. Please ensure you provided
      all targets from prometheus_targets in prometheus_scrape_configs
  when: not prometheus_config_dir + "/file_sd/" + item.key + ".yml" in file_sd_files
  #  when: not item | basename | splitext | difference(['.yml']) | join('') in prometheus_targets.keys()
  with_dict: "{{ prometheus_targets }}"

- name: Alert when prometheus_alertmanager_config is empty, but prometheus_alert_rules is specified
  debug:
    msg: >
      No alertmanager configuration was specified. If you want your alerts to be sent make sure to
      specify a prometheus_alertmanager_config in defaults/main.yml.
  when:
    - prometheus_alertmanager_config == []
    - prometheus_alert_rules != []

- name: Download... Download Prometheus archive
  get_url:
    url:  "{{ prometheus_url }}/{{ prometheus_tarball }}.tar.gz"
    dest: /tmp/{{ prometheus_tarball }}.tar.gz
    checksum: "sha256:{{ prometheus_url }}/sha256sums.txt"
  register: _download_archive
  until: _download_archive is succeeded
  retries: 5
  delay: 2
  when: not prometheus_skip_install

- name: unpack prometheus binaries
  unarchive:
    src: "/tmp/{{ prometheus_tarball }}.tar.gz"
    dest: "/tmp"
    creates: "/tmp/{{ prometheus_tarball }}/prometheus"
    remote_src: true
  when: not prometheus_skip_install

