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

- name: Add YUM repository
  template:
    src: mongodb.repo.j2
    dest: /etc/yum.repos.d/mongodb.repo
    mode: 0644
  with_items: "{{ mongodb_major_version }}"
  loop_control:
    loop_var: version_item
  when: mongodb_package == 'mongodb-org'

- name: Install MongoDB package
  yum:
    name: "{{ mongodb_package }}{% if (mongodb_version | length > 3) %}-{{ mongodb_version }}{% endif %}"
    state: "{{ mongodb_package_state }}"
    lock_timeout: "{{ yum_lock_timeout }}"

- name: Install numactl package
  yum:
    name: numactl
    state: present
    lock_timeout: "{{ yum_lock_timeout }}"
  when: mongodb_use_numa | bool

- name: Install PyMongo package
  yum:
    name: "{{ mongodb_pymongo_package }}"
    state: present
    lock_timeout: "{{ yum_lock_timeout }}"
  when: not mongodb_pymongo_from_pip

- name: Install PIP
  yum:
    name: "{{ mongodb_pymongo_deps }}"
    lock_timeout: "{{ yum_lock_timeout }}"
  when: mongodb_pymongo_from_pip

- 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
