#INSTALL.
YML
- name: Install required dependencies (gnupg and curl)
apt:
name:
- gnupg
- curl
state: present
update_cache: yes
- name: Remove existing MongoDB GPG key if present
file:
path: /usr/share/keyrings/[Link]
state: absent
- name: Add MongoDB GPG Key
shell: |
curl -fsSL [Link] | sudo gpg -
o /usr/share/keyrings/[Link] --dearmor
args:
executable: /bin/bash
- name: Add MongoDB 8.0 Repository
shell: |
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-
[Link] ] [Link] noble/mongodb-org/8.0 multiverse"
> /etc/apt/[Link].d/[Link]
- name: Update APT cache
apt:
update_cache: yes
- name: Install MongoDB
apt:
name: mongodb-org
state: present
#[Link]
- name: Copy MongoDB Config File
template:
src: [Link].j2
dest: /etc/[Link]
notify: Restart MongoDB
- name: Restart MongoDB
service:
name: mongod
state: restarted
enabled: yes
- name: Create Keyfile for Authentication
copy:
content: "{{ lookup('password', '/dev/null length=32
chars=ascii_letters,digits') }}"
dest: /etc/mongo-keyfile
owner: mongodb
group: mongodb
mode: '0600'
- name: Set MongoDB to Start on Boot
systemd:
name: mongod
enabled: yes
state: started
#[Link]
- import_tasks: [Link]
- import_tasks: [Link]
- import_tasks: [Link]
- import_tasks: replica_init.yml
#REPLICA_INIT.YML
- name: Copy Replica Set Initialization Script
template:
src: replica_init.js.j2
dest: /root/replica_init.js
- name: Initialize MongoDB Replica Set
command: mongosh /root/replica_init.js
when: inventory_hostname == "mongo-master"
#[Link]
- name: Start MongoDB Service
systemd:
name: mongod
state: started
enabled: yes
#/templates/[Link].j2
storage:
dbPath: /var/lib/mongodb
net:
bindIp: [Link]
port: 27017
replication:
replSetName: rs0
#templates/replica_init.js.j2
[Link]({
_id: "rs0",
members: [
{ _id: 0, host: "[Link]:27017" },
{ _id: 1, host: "[Link]:27017" }
]
});