---
- name: Populate service facts
  service_facts:

- name: Set before 6/7.24.1 flag
  set_fact:
    datadog_before_7241: "{{ datadog_major is defined and datadog_minor is defined and datadog_bugfix is defined
      and datadog_major | int < 8
      and (datadog_minor | int < 24 or (datadog_minor | int == 24 and datadog_bugfix | int < 1)) }}"

- name: Set before 6/7.18.0 flag
  set_fact:
    datadog_before_7180: "{{ datadog_major is defined and datadog_minor is defined
      and datadog_major | int < 8 and datadog_minor | int < 18 }}"

- name: Add "{{ datadog_user }}" user to additional groups
  user: name="{{ datadog_user }}" groups="{{ datadog_additional_groups }}" append=yes
  when: datadog_additional_groups | default([], true) | length > 0
  notify: restart datadog-agent

- name: Create Datadog agent config directory
  file:
    dest: /etc/datadog-agent
    state: directory
    mode: 0755
  when: datadog_manage_config

- name: Create main Datadog agent configuration file
  template:
    src: datadog.yaml.j2
    dest: /etc/datadog-agent/datadog.yaml
    mode: 0640
    owner: "{{ datadog_user }}"
    group: "{{ datadog_group }}"
  when: datadog_manage_config
  notify: restart datadog-agent

- name: Register all checks directories present in datadog
  find:
    paths: /etc/datadog-agent/conf.d/
    patterns:
    - "*.d"
    file_type: directory
  register: datadog_conf_directories
  when: datadog_manage_config and (datadog_disable_untracked_checks or datadog_disable_default_checks)

- name: Delete checks not present in datadog_tracked_checks
  file:
    path: "/etc/datadog-agent/conf.d/{{ item }}.d/conf.yaml"
    state: absent
  loop: "{{ datadog_conf_directories.files | map(attribute='path') | list | map('basename') | list | map('regex_replace', '^(.*).d$', '\\1') | list }}"
  when: datadog_manage_config and datadog_disable_untracked_checks and item not in datadog_tracked_checks
  notify: restart datadog-agent

- name: Delete all default checks
  file:
    path: "/etc/datadog-agent/conf.d/{{ item }}.d/conf.yaml.default"
    state: absent
  loop: "{{ datadog_conf_directories.files | map(attribute='path') | list | map('basename') | list | map('regex_replace', '^(.*).d$', '\\1') | list }}"
  when: datadog_manage_config and datadog_disable_default_checks and item not in datadog_tracked_checks
  notify: restart datadog-agent

- name: Ensure configuration directories are present for each Datadog check
  file:
    dest: "/etc/datadog-agent/conf.d/{{ item }}.d"
    state: directory
    owner: "{{ datadog_user }}"
    group: "{{ datadog_group }}"
    mode: 0755
  with_items: '{{ datadog_checks|list }}'
  when: datadog_manage_config

- name: Create a configuration file for each Datadog check
  template:
    src: checks.yaml.j2
    dest: "/etc/datadog-agent/conf.d/{{ item }}.d/conf.yaml"
    mode: 0640
    owner: "{{ datadog_user }}"
    group: "{{ datadog_group }}"
  with_items: "{{ datadog_checks|list }}"
  when: datadog_manage_config
  notify: restart datadog-agent

- name: Remove old configuration file for each Datadog check
  file:
    dest: "/etc/datadog-agent/conf.d/{{ item }}.yaml"
    state: absent
  with_items: "{{ datadog_checks|list }}"
  when: datadog_manage_config
  notify: restart datadog-agent

- name: Create system-probe configuration file
  template:
    src: system-probe.yaml.j2
    dest: /etc/datadog-agent/system-probe.yaml
    mode: 0640
    owner: "root"
    group: "{{ datadog_group }}"
  when: datadog_manage_config
  notify:
    "{% if datadog_before_7180 %}restart datadog-agent-sysprobe{% else %}restart datadog-agent{% endif %}"

- name: Set system probe installed
  set_fact:
    datadog_sysprobe_installed: "{{ ansible_facts.services['datadog-agent-sysprobe'] is defined
      or ansible_facts.services['datadog-agent-sysprobe.service'] is defined }}"
  when: not datadog_skip_running_check

# Before 6/7.24.1, system_probe_config controls the system-probe service
# datadog_minor is only defined when a specific Agent version is given
# (see tasks/parse-version.yml)
- name: Set system probe enabled (before 6/7.24.1)
  set_fact:
    datadog_sysprobe_enabled: "{{ system_probe_config is defined
      and 'enabled' in (system_probe_config | default({}, true))
      and system_probe_config['enabled']
      and datadog_sysprobe_installed }}"
  when: not datadog_skip_running_check
    and datadog_before_7241

# Since 6/7.24.1, setting enabled: true in network_config is enough to start the system-probe service:
# https://docs.datadoghq.com/network_monitoring/performance/setup/?tab=agent#setup
- name: Set system probe enabled (since 6/7.24.1)
  set_fact:
    datadog_sysprobe_enabled: "{{
      ((system_probe_config is defined
      and 'enabled' in (system_probe_config | default({}, true))
      and system_probe_config['enabled'])
      or (network_config is defined
      and 'enabled' in (network_config | default({}, true))
      and network_config['enabled']))
      and datadog_sysprobe_installed }}"
  when: not datadog_skip_running_check
    and (not datadog_before_7241)

- name: Ensure datadog-agent is running
  service:
    name: datadog-agent
    state: started
    enabled: yes
  when: not datadog_skip_running_check and datadog_enabled and not ansible_check_mode

- name: Ensure datadog-agent-sysprobe is running if enabled and installed
  service:
    name: datadog-agent-sysprobe
    state: started
    enabled: yes
  when: not datadog_skip_running_check and datadog_enabled and not ansible_check_mode and datadog_sysprobe_enabled

- name: Ensure datadog-agent, datadog-agent-process and datadog-agent-trace are not running
  service:
    name: "{{ item }}"
    state: stopped
    enabled: no
  when: not datadog_skip_running_check and not datadog_enabled
  with_list:
    - datadog-agent
    - datadog-agent-process
    - datadog-agent-trace

# Stop system-probe manually on Agent versions < 6/7.18, as it was not tied
# to the main Agent service: https://github.com/DataDog/datadog-agent/pull/4883
- name: Ensure datadog-agent-sysprobe is stopped if disabled or not installed (before 6/7.18.0)
  service:
    name: datadog-agent-sysprobe
    state: stopped
    enabled: no
  when: not datadog_skip_running_check
    and (not datadog_enabled or not datadog_sysprobe_enabled)
    and datadog_before_7180
    and datadog_sysprobe_installed

- name: Ensure datadog-agent-security is not running
  service:
    name: datadog-agent-security
    state: stopped
    enabled: no
  when: not datadog_skip_running_check and not datadog_enabled
  ignore_errors: yes # Since older versions of the Agent don't include the security agent

- name: Create security-agent configuration file
  template:
    src: security-agent.yaml.j2
    dest: /etc/datadog-agent/security-agent.yaml
    mode: 0640
    owner: "root"
    group: "{{ datadog_group }}"
  when: datadog_manage_config
  notify:
    "{% if datadog_before_7180 %}restart datadog-agent-sysprobe{% else %}restart datadog-agent{% endif %}"

- name: Create installation information file
  template:
    src: install_info.j2
    dest: /etc/datadog-agent/install_info
    owner: "{{ datadog_user }}"
    group: "{{ datadog_group }}"
    mode: 0644
