---
- name: Apply Prometheus rules configuration
  hosts: localhost
  gather_facts: false
  vars_files:
    - vars.yml

  tasks:
    - name: Create temp directory for doing work in on target
      command: mktemp -d
      register: tempdir

    - name: Clone custom rules
      git:
        repo: "{{ custom_rules_repo }}"
        version: "{{ custom_rules_branch | default('master') }}"
        depth: 1
        dest: "{{ tempdir.stdout }}"
      when: custom_rules_repo is defined

    - name: Apply configmap
      shell: "oc create configmap {{ custom_rules_configmap }} --from-file={{ tempdir.stdout }}/{{ custom_rules_path }} -n {{ metrics_namespace }} --dry-run -o yaml | oc apply -f - -n {{ metrics_namespace }}"
      register: rules_configmap

    - name: Attach configmap as a volume
      command: "oc volume statefulset/prometheus --add --configmap-name={{ custom_rules_configmap }} --name={{ custom_rules_configmap }} -t configmap --mount-path=/etc/prometheus/rules/ --overwrite=true -n {{ metrics_namespace }}"
