Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
718f6ce
update mongo version
MohitSainiS Jun 26, 2025
3cfe215
update mongo version
MohitSainiS Jun 26, 2025
b11f67d
Update main.yml
MohitSainiS Jun 27, 2025
e1084cb
Update README.md
MohitSainiS Jun 27, 2025
8600d2e
Update main.yml
MohitSainiS Jun 30, 2025
5ed5b07
Update install_mongo_debian.yml
MohitSainiS Jun 30, 2025
bfc7d27
Update mongo_conf.j2
MohitSainiS Jun 30, 2025
2f4d520
Update mongo_init_conf.j2
MohitSainiS Jun 30, 2025
d5b2424
Update setup_authentication_mongo.yml
MohitSainiS Jun 30, 2025
4d5182d
Update setup_authentication_mongo.yml
MohitSainiS Jun 30, 2025
ac22954
Update setup_authentication_mongo.yml
MohitSainiS Jun 30, 2025
9aac931
Update setup_authentication_mongo.yml
MohitSainiS Jun 30, 2025
73efe32
Update main.yml
MohitSainiS Jun 30, 2025
4079807
Update main.yml
MohitSainiS Jun 30, 2025
adba942
Update main.yml
MohitSainiS Jun 30, 2025
a0127da
Update main.yml
MohitSainiS Jun 30, 2025
3db59fe
Update main.yml
MohitSainiS Jun 30, 2025
07f6193
Update install_mongo_debian.yml
MohitSainiS Jun 30, 2025
5995d5d
Update main.yml
MohitSainiS Jun 30, 2025
7b270cf
Update main.yml
MohitSainiS Jun 30, 2025
5f51afd
Update setup_authentication_mongo.yml
MohitSainiS Jun 30, 2025
35f79b8
Update setup_authentication_mongo.yml
MohitSainiS Jun 30, 2025
09346fa
Update main.yml
MohitSainiS Jun 30, 2025
dfa5394
Update configure.yml
MohitSainiS Jun 30, 2025
a8b7980
Update main.yml
MohitSainiS Jul 1, 2025
055c594
Update install_mongo_debian.yml
MohitSainiS Jul 1, 2025
6c735a8
Update install_mongo_debian.yml
MohitSainiS Jul 1, 2025
c8ace8f
Update setup_authentication_mongo.yml
MohitSainiS Jul 1, 2025
93359ef
Update main.yml
MohitSainiS Jul 1, 2025
45fa7eb
Update main.yml
MohitSainiS Jul 1, 2025
a1cc74c
Update main.yml
MohitSainiS Jul 1, 2025
4af3395
Update main.yml
MohitSainiS Jul 1, 2025
911a51b
Update main.yml
MohitSainiS Jul 1, 2025
8c4a4ea
Update main.yml
MohitSainiS Jul 1, 2025
bff18b7
Update main.yml
MohitSainiS Jul 1, 2025
c2f5116
Update main.yml
MohitSainiS Jul 1, 2025
64f93ea
Update main.yml
MohitSainiS Jul 1, 2025
db4c565
Update setup_authentication_mongo.yml
MohitSainiS Jul 1, 2025
1652845
Update install_mongo_debian.yml
MohitSainiS Jul 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Version History
|----------|---------|---------------|-----------------|
|**27 June 2020** | v0.0.1 | Initial draft | Rajat Vats |
|**21 March 2022** | v0.0.2 | Manage node DNS | Kritarth Pant |
|**26 June 2025** | V0.0.3 | Update version 7.0 | Mohit Saini

### PREREQUISITE
* ###### NOTHING !!!!
Expand All @@ -25,7 +26,7 @@ Version History

|**Variable**|**Default Value**|**Possible Values**|**Description**|
|------------|-----------------|-------------------|---------------|
| mongo_version | 4.2 | [4.2, 4.0, 3.6, 3.4] Can mention minor version as well eg: 4.2.7 |MongoDB version |
| mongo_version | 7.0 | [7.0, 6.0, 5.0, 4.4, 4.2, 4.0, 3.6, 3.4] | Can mention minor version as well e.g., 7.0.2 | MongoDB version |
| mongo_port | 27017 | As per your requirement | MongoDB port |
| replication_enabled | false | [true, false] |Set True to setup replication
| mongo_replSetName | repl | As per your requirement | Name of the replica set|
Expand Down
35 changes: 13 additions & 22 deletions defaults/main.yml
Copy link

@sharvarikhamkar1304 sharvarikhamkar1304 Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • prefer downloading key directly.

  • use secret for password

  • perform ansible lint

Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,26 @@
# defaults file for mongodb

# Mongo Installation parameters
mongo_version: "4.2"
mongo_version: "7.0.21"
mongo_major_version: "7.0"
mongo_package_name: mongodb-org
mongo_apt_repo_url: http://repo.mongodb.org/apt/ubuntu
mongo_apt_repo_url: https://repo.mongodb.org/apt/ubuntu

# GPG Key URLs for Mongo versions
mongo_apt_key_url:
"7.0": "https://www.mongodb.org/static/pgp/server-7.0.asc"

# Only used for older versions (< 7.0)
mongo_apt_key_id:
"3.4": "0C49F3730359A14518585931BC711F9BA15703C6"
"3.6": "2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5"
"4.0": "9DA31620334BD75D9DCB49F368818C72E52529D4"
"4.2": "E162F504A20CDF15827F718D4B7C549A058F8B6B"

mongo_apt_key_server: keyserver.ubuntu.com
mongo_gpg_key_path: "/usr/share/keyrings/mongodb-server-7.0.gpg"

pymongo_pip_version: 3.7.1
mongo_redhat_url: https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org
mongo_redhat_repository:
"3.4": "{{ mongo_redhat_url }}/3.4/$basearch/"
"3.6": "{{ mongo_redhat_url }}/3.6/$basearch/"
"4.0": "{{ mongo_redhat_url }}/4.0/$basearch/"
"4.2": "{{ mongo_redhat_url }}/4.2/$basearch/"
mongo_redhat_repository_gpgkey:
"3.4": "https://www.mongodb.org/static/pgp/server-3.4.asc"
"3.6": "https://www.mongodb.org/static/pgp/server-3.6.asc"
"4.0": "https://www.mongodb.org/static/pgp/server-4.0.asc"
"4.2": "https://www.mongodb.org/static/pgp/server-4.2.asc"
mongo_amazon_url: https://repo.mongodb.org/yum/amazon/2/mongodb-org
mongo_amazon_repository:
"3.4": "{{ mongo_amazon_url }}/3.4/$basearch/"
"3.6": "{{ mongo_amazon_url }}/3.6/$basearch/"
"4.0": "{{ mongo_amazon_url }}/4.0/$basearch/"
"4.2": "{{ mongo_amazon_url }}/4.2/$basearch/"
yum_lock_timeout: 180
epel_release_rpm: https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

# Mongo Service parameters
mongo_service_name: mongod
Expand All @@ -44,7 +35,7 @@ mongo_db_path: "{{ '/var/lib/mongodb' if ansible_os_family == 'Debian' else '/va
mongo_pid_file_path: /var/run/mongodb/mongod.pid
mongo_time_zone_info: /usr/share/zoneinfo
mongo_port: 27017
mongo_bindIp: localhost
mongo_bindIp: 0.0.0.0

# Mongo Replication parameter
replication_enabled: false
Expand Down
8 changes: 6 additions & 2 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
service:
name: "{{ mongo_service_name }}"
state: restarted

- name: wait_till_mongodb_started
wait_for:
host: "{{ item }}"
port: "{{ mongo_port }}"
timeout: 120
timeout: 180
with_items:
- "{{ groups['mongo_master'] | list }}"
- "{{ groups['mongo_slave'] | list }}"
delegate_to: "{{ item }}"

- name: wait_till_mongodb_started_on_localhost
wait_for:
host: "127.0.0.1"
port: "{{ mongo_port }}"
delay: 5
timeout: 120
delegate_to: "{{ ansible_host | default(inventory_hostname) }}"


2 changes: 1 addition & 1 deletion tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
mode: 0644
notify:
- mongodb_restart
- "{{ 'wait_till_mongodb_started' if replication_enabled == 'true' else 'wait_till_mongodb_started_on_localhost' }}"
# - "{{ 'wait_till_mongodb_started' if replication_enabled == 'true' else 'wait_till_mongodb_started_on_localhost' }}"

- name: Flush all handlers at this point
meta: flush_handlers
30 changes: 27 additions & 3 deletions tasks/install_mongo_debian.yml
Copy link

@sharvarikhamkar1304 sharvarikhamkar1304 Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Use ansible.builtin.pip instead of pip

  • get_url + signed-by instead of apt_key

  • signed-by=/usr/share/keyrings/... in the repo: line for secure APT key

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
# Debian-specific MongoDB install and wait

- name: Establish some role-related facts
set_fact:
mongo_major_version: "{{ mongo_version[0:3] }}"
Expand All @@ -19,7 +21,6 @@
state: "{{ pymongo_pip_version is defined | ternary('present','latest') }}"
version: "{{ pymongo_pip_version | default(omit) }}"


- name: Add an apt key by id from a keyserver
apt_key:
keyserver: "{{ mongo_apt_key_server }}"
Expand All @@ -28,12 +29,35 @@

- name: Add MongoDB repo
apt_repository:
repo: deb {{ mongo_apt_repo_url }} {{ ansible_distribution_release }}/mongodb-org/{{ mongo_major_version }} multiverse
filename: mongodb-org-{{ mongo_major_version }}
repo: "deb {{ mongo_apt_repo_url }} {{ ansible_distribution_release }}/mongodb-org/{{ mongo_major_version }} multiverse"
filename: "mongodb-org-{{ mongo_major_version }}"
state: present

- name: Install mongodb
apt:
name: "{{ mongo_package_name }}{% if (mongo_version | length > 3) %}={{ mongo_version }}{% endif %}"
state: present
update_cache: "yes"

# ---------------------------------------
# ✅ MongoDB Port Wait Logic (Safe)
# ---------------------------------------

# - name: Check if MongoDB is already listening on 127.0.0.1
# shell: ss -ltnp | grep 127.0.0.1:{{ mongo_port }}
# register: mongo_port_check_local
# ignore_errors: true
# changed_when: false

# - name: Debug: MongoDB already running
# debug:
# msg: "✅ MongoDB is already listening on {{ inventory_hostname }}:{{ mongo_port }}. Skipping wait."
# when: mongo_port_check_local.rc == 0

# - name: Wait for MongoDB to start (if not already running)
# wait_for:
# host: 127.0.0.1
# port: "{{ mongo_port }}"
# delay: 5
# timeout: 180
# when: mongo_port_check_local.rc != 0
81 changes: 58 additions & 23 deletions tasks/setup_authentication_mongo.yml
Original file line number Diff line number Diff line change
@@ -1,78 +1,113 @@
---

- name: Use different mongod.conf for auth initialization
- name: Use mongod.conf for auth initialization
template:
src: mongo_init_conf.j2
dest: /etc/mongod.conf
owner: root
group: root
mode: 0644
mode: '0644'
notify:
- mongodb_restart
- wait_till_mongodb_started_on_localhost
# - wait_till_mongodb_started_on_localhost

- name: Flush all handlers at this point
- name: Flush handlers after initial config
meta: flush_handlers

- name: Check if MongoDB is already listening on {{ mongo_port }}
shell: ss -ltnp | grep {{ mongo_port }}
register: mongo_port_check
changed_when: false
ignore_errors: true

- name: Wait for MongoDB to be ready on this host
wait_for:
host: "{{ ansible_default_ipv4.address }}"
port: "{{ mongo_port }}"
delay: 5
timeout: 180
when: mongo_port_check.rc != 0

- name: Debug root user credentials
debug:
msg: "Creating root user: {{ mongo_root_admin_name }}"

# Create root admin user
- name: Create root admin user
mongodb_user:
login_host: localhost
database: admin
name: "{{ mongo_root_admin_name }}"
password: "{{ mongo_root_admin_pwd }}"
update_password: on_create
roles: root
roles: ["root"]
state: present
update_password: on_create
when: inventory_hostname in groups['mongo_master']
run_once: true
no_log: true
no_log: false

- name: Create admin user
# Create admin user
- name: Create userAdminAnyDatabase user
mongodb_user:
login_host: localhost
database: admin
name: "{{ mongo_user_admin_name }}"
password: "{{ mongo_user_admin_pwd }}"
update_password: on_create
roles: userAdminAnyDatabase
roles: ["userAdminAnyDatabase"]
state: present
update_password: on_create
login_user: "{{ mongo_root_admin_name }}"
login_password: "{{ mongo_root_admin_pwd }}"
auth_source: admin
when: inventory_hostname in groups['mongo_master']
run_once: true
no_log: true
no_log: false

# Create backup user
- name: Create backup root user
mongodb_user:
login_host: localhost
database: admin
name: "{{ mongo_root_backup_name }}"
password: "{{ mongo_root_backup_pwd }}"
update_password: on_create
roles: backup,clusterMonitor
roles: ["backup", "clusterMonitor"]
state: present
update_password: on_create
login_user: "{{ mongo_root_admin_name }}"
login_password: "{{ mongo_root_admin_pwd }}"
auth_source: admin
when: inventory_hostname in groups['mongo_master']
run_once: true
no_log: true
no_log: false

- name: Create user defined users
# Create custom users
- name: Create user-defined MongoDB users
mongodb_user:
login_host: localhost
database: admin
name: "{{ item.name }}"
password: "{{ item.password }}"
update_password: on_create
roles: "{{ item.roles }}"
with_items:
- "{{ mongo_custom_users }}"
state: present
update_password: on_create
login_user: "{{ mongo_root_admin_name }}"
login_password: "{{ mongo_root_admin_pwd }}"
auth_source: admin
loop: "{{ mongo_custom_users }}"
when: inventory_hostname in groups['mongo_master'] and mongo_custom_users | length > 0
run_once: true
no_log: true
when: inventory_hostname in groups['mongo_master'] and mongo_custom_users != "None"
no_log: false

# Restore actual mongod.conf after user setup
- name: Move back mongod.conf
template:
src: mongo_conf.j2
dest: /etc/mongod.conf
owner: root
group: root
mode: 0644
mode: '0644'
notify:
- mongodb_restart
- "{{ 'wait_till_mongodb_started' if replication_enabled == 'true' else 'wait_till_mongodb_started_on_localhost' }}"
# - wait_till_mongodb_started

- name: Flush all handlers at this point
meta: flush_handlers
3 changes: 2 additions & 1 deletion templates/mongo_conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ systemLog:
# Where and how to store data.
storage:
dbPath: {{ mongo_db_path }}
{% if mongo_major_version is version('7.0', '<') %}
journal:
enabled: true
{% endif %}

# network interfaces
net:
Expand All @@ -36,4 +38,3 @@ security:
replication:
replSetName: {{ mongo_replSetName }}
{% endif %}

20 changes: 2 additions & 18 deletions templates/mongo_init_conf.j2
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
# mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
destination: file
logAppend: true
path: {{ mongo_systemlog_path }}

# Where and how to store data.
storage:
dbPath: {{ mongo_db_path }}
{% if mongo_major_version is version('7.0', '<') %}
journal:
enabled: true

# network interfaces
net:
port: {{ mongo_port }}
bindIp: localhost

{% endif %}
4 changes: 2 additions & 2 deletions vars/main.yml
Copy link

@sharvarikhamkar1304 sharvarikhamkar1304 Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • for mongo_authentication_key you can use lookup

  • mongo_version you already use in defaults no need to mention - avoid duplication

  • Hardcoded credentials

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# vars file for mongodb

# Mongo Installation parameters
mongo_version: "4.2"
mongo_version: "7.0"

# Mongo Configuration parameters
mongo_port: 27017
mongo_bindIp: localhost
mongo_bindIp: 0.0.0.0

# Mongo Replication parameter
replication_enabled: false
Expand Down