Hi there! I'm Maneshwar. Right now, I’m building LiveAPI, a first-of-its-kind tool that helps you automatically index API endpoints across all your repositories. LiveAPI makes it easier to discover, understand, and interact with APIs in large infrastructures.
MongoDB 8.0 is here — and it’s faster, stricter, and more powerful than ever. Whether you're spinning up a dev server or laying the groundwork for production, automating your setup is key.
In this post, I’ll show you how to install MongoDB 8.0 cleanly using Ansible, the preferred tool of DevOps sorcerers and lazy sysadmins alike.
Let’s go from zero to mongod in ~10 tasks.
What You’ll Need
- An Ubuntu 22.04 (Jammy) or Debian 12 (Bookworm) machine
- Ansible installed on your local/dev machine
- SSH access to your target server
Step-by-Step: Ansible Tasks Breakdown
Let’s break the setup into bite-sized YAML pieces.
1. Install Dependencies
We’ll need GPG and a few tools for downloading MongoDB's GPG key and repo config:
- name: Install prerequisites for MongoDB
ansible.builtin.package:
name:
- gnupg
- curl
- wget
state: present
become: true
2. Add MongoDB's GPG Key (Debian-style)
Debian Bookworm prefers keys to be stored in /usr/share/keyrings:
- name: Add MongoDB GPG key
ansible.builtin.shell: >
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc |
gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
args:
creates: /usr/share/keyrings/mongodb-server-8.0.gpg
become: true
3. Add MongoDB Repository (Debian)
We add the official MongoDB 8.0 repo:
- name: Add MongoDB repository
ansible.builtin.apt_repository:
repo: "deb [ signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] http://repo.mongodb.org/apt/debian bookworm/mongodb-org/8.0 main"
state: present
filename: mongodb-org-8.0
become: true
For Ubuntu Jammy (22.04), you can also add:
- name: Add MongoDB repository (Ubuntu)
ansible.builtin.apt_repository:
repo: "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse"
state: present
filename: mongodb-org-8.0-ubuntu
become: true
This makes it flexible for both systems.
4. Install MongoDB Itself
- name: Install MongoDB
ansible.builtin.package:
name: mongodb-org
state: present
update_cache: true
become: true
This installs:
- mongod (the database server)
- mongos (the sharding router)
- mongosh (MongoDB’s newer shell)
5. Start and Enable MongoDB Service
Let’s make sure it starts now and every time the server reboots:
- name: Ensure mongod service is started and enabled
ansible.builtin.service:
name: mongod
state: started
enabled: true
daemon_reload: true
become: true
Now you can check if it’s running:
sudo systemctl status mongod
Bonus: Install and Verify mongosh
- name: Install MongoDB shell
ansible.builtin.package:
name: mongodb-mongosh
state: present
become: true
- name: Check MongoDB shell version
ansible.builtin.command: mongosh --version
register: mongosh_version
become: true
- name: Display MongoDB shell version
ansible.builtin.debug:
var: mongosh_version.stdout
You’ll get clean output like:
ok: [your-server] => {
"mongosh_version.stdout": "2.0.1"
}
Which tells you that your shell is good to go.
Tip: Alternative GPG method
Some systems still expect keys in /etc/apt/trusted.gpg.d:
- name: Add MongoDB GPG key (alternative method)
ansible.builtin.shell: >
wget -qO- https://www.mongodb.org/static/pgp/server-8.0.asc |
tee /etc/apt/trusted.gpg.d/server-8.0.asc
args:
creates: /etc/apt/trusted.gpg.d/server-8.0.asc
become: true
Use this only if the keyring method gives you issues.
Recap: What We Just Did
With this Ansible playbook, you:
✅ Installed MongoDB 8.0 on Debian or Ubuntu
✅ Pulled and trusted the GPG keys
✅ Set up MongoDB’s official APT repository
✅ Installed and started the mongod service
✅ Verified your shell with mongosh
You can now securely connect via:
mongosh
Or remotely if enabled.
What’s Next?
Now that MongoDB is up, you’ll probably want to:
- Create an admin user with db.createUser
- Enable authentication in mongod.conf
- Add TLS for production
- Set up backups or replication
- Monitor it with Prometheus or Grafana
MongoDB is powerful, but with Ansible, it’s also reproducible, secure, and version-controlled.
Happy deploying! 🚀
LiveAPI helps you get all your backend APIs documented in a few minutes.
With LiveAPI, you can generate interactive API docs that allow users to search and execute endpoints directly from the browser.
If you're tired of updating Swagger manually or syncing Postman collections, give it a shot.
Top comments (0)