---
# file: tasks/mongodb.yml
# playbook to install MongoDB

- name: Ensure apt key for MongoDB is present
  apt_key:
    keyserver: keyserver.ubuntu.com
    id: "{{ reddit_mongodb_key }}"

- name: Ensure apt repository for MongoDB is present
  apt_repository:
    repo: "{{ reddit_mongodb_repo }}"
    filename: mongodb-org-{{ reddit_mongodb_version | splitext | first }}
    update_cache: yes
    state: present

- name: Ensure MongoDB package is pinned
  copy:
    dest: /etc/apt/preferences.d/mongodb-org
    content: |
      Package: mongodb-org
      Pin: version {{ reddit_mongodb_version }}
      Pin-Priority: 1002

- name: Ensure MongoDB package is present
  apt:
    name: mongodb-org={{ reddit_mongodb_version }}
    update_cache: yes
    cache_valid_time: 3600
    state: present

- name: Ensure MongoDB service is running and enabled
  service:
    name: mongod
    enabled: yes
    state: started

Add task to install MongoDB
Add task to install MongoDB
