# SPDX-License-Identifier: MIT
---
- name: Ensure ansible_facts used by role
  setup:
    gather_subset: "{{ __metrics_required_facts_subsets }}"
  when: __metrics_required_facts |
    difference(ansible_facts.keys() | list) | length > 0

- name: Add Elasticsearch to metrics domain list
  set_fact:
    __metrics_domains: "{{ __metrics_domains + ['elasticsearch'] }}"
  when: metrics_from_elasticsearch | d(false) | bool

- name: Add SQL Server to metrics domain list
  set_fact:
    __metrics_domains: "{{ __metrics_domains + ['mssql'] }}"
  when: metrics_from_mssql | d(false) | bool

- name: Add Postfix to metrics domain list
  set_fact:
    __metrics_domains: "{{ __metrics_domains + ['postfix'] }}"
  when: metrics_from_postfix | d(false) | bool

- name: Add bpftrace to metrics domain list
  set_fact:
    __metrics_domains: "{{ __metrics_domains + ['bpftrace'] }}"
  when: metrics_from_bpftrace | d(false) | bool

- name: Setup metrics access for roles
  set_fact:
    __metrics_accounts:
      - {user: "{{ metrics_username }}", sasluser: "{{ metrics_username }}",
         saslpassword: "{{ metrics_password }}"}
  when: metrics_username | d("") | length > 0

# yamllint disable rule:line-length
- name: Configure Elasticsearch metrics
  vars:
    elasticsearch_agent: "{{ metrics_from_elasticsearch | d(false) | bool }}"
    elasticsearch_export_type: "{{ metrics_provider }}-metrics"
    elasticsearch_export_index: "{{ metrics_provider }}"
    elasticsearch_export_metrics: "{{ metrics_into_elasticsearch | d(false) | bool }}"
    elasticsearch_metrics_provider: "{{ metrics_provider }}"
  include_role:
    # noqa role-name[path]
    name: "{{ role_path }}/roles/elasticsearch"
  when: >
    metrics_from_elasticsearch | d(false) | bool or
    metrics_into_elasticsearch | d(false) | bool
# yamllint enable rule:line-length

- name: Configure SQL Server metrics.
  vars:
    mssql_metrics_provider: "{{ metrics_provider }}"
  include_role:
    # noqa role-name[path]
    name: "{{ role_path }}/roles/mssql"
  when: metrics_from_mssql | d(false) | bool

- name: Configure Postfix metrics.
  vars:
    postfix_metrics_provider: "{{ metrics_provider }}"
  include_role:
    # noqa role-name[path]
    name: "{{ role_path }}/roles/postfix"
  when: metrics_from_postfix | d(false) | bool

- name: Setup bpftrace metrics.
  vars:
    bpftrace_users: "{{ __metrics_accounts }}"
    bpftrace_metrics_provider: "{{ metrics_provider }}"
  include_role:
    # noqa role-name[path]
    name: "{{ role_path }}/roles/bpftrace"
  when: metrics_from_bpftrace | d(false) | bool

- name: Setup metric querying service.
  vars:
    redis_metrics_provider: "{{ metrics_provider }}"
  include_role:
    # noqa role-name[path]
    name: "{{ role_path }}/roles/redis"
  when: metrics_query_service | bool

- name: Setup metric collection service.
  vars:
    pcp_pmie_endpoint: "{{ metrics_webhook_endpoint }}"
    pcp_pmlogger_discard: "{{ metrics_retention_days }}"
    pcp_target_hosts: "{{ metrics_monitored_hosts }}"
    pcp_optional_agents: "{{ __metrics_domains }}"
    pcp_accounts: "{{ __metrics_accounts }}"
    pcp_rest_api: "{{ metrics_query_service | bool or
                      metrics_graph_service | bool }}"
  include_role:
    # noqa role-name[path]
    name: "{{ role_path }}/roles/pcp"
  when: metrics_provider == 'pcp'

- name: Setup metric graphing service.
  vars:
    grafana_metrics_provider: "{{ metrics_provider }}"
  include_role:
    # noqa role-name[path]
    name: "{{ role_path }}/roles/grafana"
  when: metrics_graph_service | bool

- name: Configure firewall
  include_tasks: firewall.yml

- name: Configure selinux
  include_tasks: selinux.yml
