---
- name: Establish some role-related facts
  set_fact:
    mongodb_major_version: "{{ mongodb_version[0:3] | string }}"

- name: Disable transparent huge pages on systemd systems
  include_tasks: disable_transparent_hugepages.yml
  when:
    - mongodb_disable_transparent_hugepages | bool
    - ansible_service_mgr == "systemd"

- name: Add APT key
  apt_key:
    keyserver: "{{ mongodb_apt_keyserver if not mongodb_apt_key_from_url|bool else omit }}"
    url: "{{ mongodb_apt_key_url[mongodb_major_version] if mongodb_apt_key_from_url|bool else omit }}"
    id: "{{ mongodb_apt_key_id[mongodb_major_version] }}"
  when: mongodb_package == 'mongodb-org'

- name: Fail when used wrong mongodb_version variable
  fail:
    msg: "mongodb_version variable should be '3.6' or '4.0', '4.2' or '4.4'"
  when: (mongodb_package == 'mongodb-org' and
        (mongodb_version is not defined
         or mongodb_repository[mongodb_major_version] is not defined))

- name: Add APT repository
  apt_repository:
    repo: "{{ mongodb_repository[version_item] }}"
    update_cache: true
  with_items: "{{ mongodb_major_version }}"
  loop_control:
    loop_var: version_item
  when: mongodb_package == 'mongodb-org'

- name: Install MongoDB package
  apt:
    name: "{{ mongodb_package }}{% if (mongodb_version | length > 3) %}={{ mongodb_version }}{% endif %}"
    state: "{{ mongodb_package_state }}"
    update_cache: true

- name: Check if NUMA is available on host
  command: "ls -1 /proc/1/numa_maps"
  register: numa_available
  ignore_errors: true
  changed_when: false
  check_mode: no
  when: mongodb_use_numa | bool

- name: Don't use NUMA if it is unavailable on host
  set_fact:
    mongodb_use_numa: false
  when: (mongodb_use_numa | bool and numa_available.rc != 0)

- name: Install numactl package
  apt:
    name: numactl
    state: present
  when: mongodb_use_numa | bool

- name: Add systemd configuration if present
  template:
    src: mongodb.service.j2
    dest: "/lib/systemd/system/{{ mongodb_daemon_name }}.service"
    owner: root
    group: root
    mode: '0644'
  when:
    - ansible_service_mgr == "systemd"
    - mongodb_manage_systemd_unit | bool
  notify:
    - reload systemd

- name: Add symlink for systemd
  file:
    src: "/lib/systemd/system/{{ mongodb_daemon_name }}.service"
    dest: "/etc/systemd/system/multi-user.target.wants/{{ mongodb_daemon_name }}.service"
    state: link
  when:
    - ansible_service_mgr == "systemd"
    - mongodb_manage_systemd_unit | bool
  notify:
    - reload systemd

  # Workaround for the idempotence issue on Debian 10 with pip module
- name: Forcefully set mongodb_pymongo_from_pip to false on Debian 10
  set_fact:
    mongodb_pymongo_from_pip: false
  when:
    - ansible_distribution == 'Debian'
    - ansible_distribution_release == 'buster'

- name: Install PyMongo package
  apt:
    name: "{{ mongodb_pymongo_package }}"
  when: not mongodb_pymongo_from_pip

- name: Install PIP
  apt:
    pkg: "{{ mongodb_pymongo_deps }}"
  when: mongodb_pymongo_from_pip | bool

  # TODO: check if it is still needed for Debian/Ubuntu
- name: Install setuptools (required for ansible 2.7+)
  apt:
    pkg: "{{ mongodb_python_setuptools }}"
  when: mongodb_pymongo_from_pip | bool

- name: Install PyMongo from PIP
  pip:
    name:
      - pymongo
    state: "{{ mongodb_pymongo_pip_version is defined | ternary('present', 'latest') }}"
    version: "{{ mongodb_pymongo_pip_version | default(omit) }}"
    executable: "{{ mongodb_pip_executable }}"
  when: mongodb_pymongo_from_pip | bool
