---
- name: apt | Add mongodb key
  become: yes
  apt_key:
    # Don't use mongodb.org key location due to SNI verification problems under Ubuntu Trusty
    keyserver: "hkp://keyserver.ubuntu.com:80"
    id: "{{ mongodb_apt_keys[mongodb_major_minor_version] }}"
    state: present
  tags: [databases, mongodb]

- name: apt | Add mongodb repository
  become: yes
  apt_repository:
    repo: 'deb http://repo.mongodb.org/apt/{{ ansible_distribution|lower }} {{ ansible_distribution_release|lower }}/mongodb-org/{{ mongodb_major_minor_version }} multiverse'
    state: present
  tags: [databases, mongodb]

- name: apt | Install mongodb
  become: yes
  apt:
    name: mongodb-org={{ mongodb_version }}*
    state: present
  notify: restart mongodb
  tags: [databases, mongodb]

---
- name: apt | Add mongodb key
  become: yes
  apt_key:
    # Don't use mongodb.org key location due to SNI verification problems under Ubuntu Trusty
    keyserver: "hkp://keyserver.ubuntu.com:80"
    id: "{{ mongodb_apt_keys[mongodb_major_minor_version] }}"
    state: present
  tags: [databases, mongodb]

- name: apt | Add mongodb repository
  become: yes
  apt_repository:
    repo: 'deb http://repo.mongodb.org/apt/{{ ansible_distribution|lower }} {{ ansible_distribution_release|lower }}/mongodb-org/{{ mongodb_major_minor_version }} multiverse'
    state: present
  tags: [databases, mongodb]

- name: apt | Install mongodb
  become: yes
  apt:
    name: {{ item }}={{ mongodb_version }}*
    state: present
  notify: restart mongodb
  with_items:
    # re-installing different version of 'mongodb-org' meta package doesn't automatically
    # upgrade or downgrade its dependencies. So we need to explicitly list them one-by-one.
    - mongodb-org
    - mongodb-org-shell
    - mongodb-org-server
    - mongodb-org-mongos
    - mongodb-org-tools
  tags: [databases, mongodb]

Fix upgrade/downgrade path for deb Ubuntu
Fix upgrade/downgrade path for deb Ubuntu

When switching between 3.2 <> 3.4 <> etc mongo versions, role should ensure that all mongo packages are re-installed.
deb 'mongodb-org' meta package doesn't handle correct versions of dependencies and doesn't update or downgrade other mongo packages.
