How to install MongoDB with Ansible?
Solution 1
create your mongo-ansible.yml file and use this:
# Install mongodb
---
- name: Add mongo ppa key
sudo: yes
apt_key: >
keyserver=hkp://keyserver.ubuntu.com:80
id=7F0CEB10
state=present
- name: Add mongo sources list
sudo: yes
lineinfile: >
line="deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse"
dest=/etc/apt/sources.list.d/mongodb.list
state=present
create=yes
- name: Install mongo
sudo: yes
apt: name=mongodb-org state=latest update_cache=yes
Solution 2
In that case you don't even have to mess with lineinfile
. Use the apt_repository
module (documentation) ; in this example we'll install MongoDB 3.4:
[...]
tasks:
- name: Add Mongo packages repo
apt_key: id=2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5 keyserver=keyserver.ubuntu.com
- name: add repo itself
apt_repository: repo='deb http://repo.mongodb.org/apt/ubuntu {{ansible_distribution_release}}/mongodb-org/3.4 multiverse' state=present
- name: install packages
apt: pkg=mongodb-org state=present
[...]
Solution 3
In ansible 2.0 sudo
deprecated and block
added. So for modern syntax and latest mongodb:
---
# https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
- block:
- name: Import MongoDB public GPG Key
apt_key:
keyserver: keyserver.ubuntu.com
id: 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
- name: Add MongoDB repository into sources list
apt_repository:
repo: deb http://repo.mongodb.org/apt/ubuntu {{ansible_distribution_release}}/mongodb-org/3.4 multiverse
state: present
- name: Install MongoDB package
apt:
name: mongodb-org
update_cache: yes
become: yes
Solution 4
There is no need to do this by using shell commands. apt_key module can add keys, apt module can be used to install and lineinfile module can ensure a particular line in file.
- name: get apt key
apt_key: keyserver=hkp://keyserver.ubuntu.com:80 id=EA312927
- name: add to mongodb-org-3.2.list
lineinfile: dest=/etc/apt/sources.list.d/mongodb-org-3.2.list line="deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse"
- name: install mongo db
apt: name=mongodb-org update_cache=yes
If you need to create mongodb-org-3.2.list
first then you can use file module for that.
- name: create mongodb-org-3.2.list
file: path=/etc/apt/sources.list.d/mongodb-org-3.2.list state=present
kramer65
Updated on June 04, 2022Comments
-
kramer65 almost 2 years
I'm a beginner with Ansible and I'm now trying to install MongoDB on an Ubuntu 14.04 host. According to the MongoDB installation instructions the manual process is as follows:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5 echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list sudo apt-get update sudo apt-get install -y mongodb-org
I guess I can run this using the Ansible shell module, but since line 2 of the 4 lines about would constantly add new lines to the
mongodb-org-3.6.list
file I guess that is not the correct way.Does anybody know what the logical way of doing this with Ansible is? All tips are welcome!
-
kramer65 almost 8 yearsThanks for your answer. May I ask why you install
mongodb-10gen
instead ofmongodb-org
? -
Ghassan Zein almost 8 yearsWell you can use mongodb-org instead :) I used to use 10gen back in the old days but now as you can stick to org one.
-
Xiong Chiamiov almost 8 yearsYou should use apt_repository instead of
lineinfile
.