How to install MongoDB with Ansible? - linux

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!

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
[...]

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

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

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

Related

How to install couchdb version 2.3.0

I am following this guide to install couchDB on Ubuntu 16.04. Everytime I tried, it installs only version 1.6.0. I tried installing and unstalling many time but it is not updating and I am getting only above official guide to install it.
What is the way to install the latest version?
You need to tell Ubuntu about the official CouchDB repository:
echo "deb https://apache.bintray.com/couchdb-deb xenial main" | sudo tee -a /etc/apt/sources.list
Be sure to set your distribution correctly, in the above replace xenial with what matches your distribution:
Ubuntu 14.04: trusty
Ubuntu 16.04: xenial
Ubuntu 18.04: bionic
You then also need to trust the new repository, by adding its public key to apt:
curl -L https://couchdb.apache.org/repo/bintray-pubkey.asc | sudo apt-key add -
Then you can install CouchDB with apt:
sudo apt-get update && sudo apt-get install couchdb
See official CouchDB install instructions.

CI + Test: run unit tests against DB

Executing tests against mongo DB on the CI (circleCI) fails even though they pass locally.
Am installing mongo db and connect the app to the db, here is my circle.yml file
machine:
node:
version: 7.2.1
dependencies:
override:
- sudo apt-get purge mongodb-org*
- sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
- echo "deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
- sudo apt-get update
- sudo apt-get install -y mongodb-org
- sudo service mongod restart
- npm install
test:
override:
- npm run test
The DataBase URL: mongodb://mongodb:27017/db-name
CircleCI already provides MongoDB 3.0.7 by default
Edit your circle.yml for this:
machine:
node:
version: 7.2.1
dependencies:
override:
- npm install
test:
override:
- npm run test
More information

ansible install node.js version 6

For installing the node 6.x version these are the commands:
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
now how exactly do I do that in ansible?
any ideas here?
this is what I had till now, but it installs the old version
---
- name: Ensure Ubuntu Distro is Supported
get_url:
url='https://deb.nodesource.com/node/dists/"{{ ansible_distribution_release }}"/Release'
dest=/dev/null
register: distrosupported
- name: Remove Old Chris Lea PPA
apt_repository:
repo='ppa:chris-lea/node.js'
state=absent
when: distrosupported|success
ignore_errors: yes
- name: Remove Old Chris Lea Sources
sudo: yes
file:
path='/etc/apt/sources.list.d/chris-lea-node_js-"{{ ansible_distribution_release }}".list'
state=absent
when: distrosupported|success
ignore_errors: yes
- name: Add Nodesource Keys
sudo: yes
apt_key:
url=https://deb.nodesource.com/gpgkey/nodesource.gpg.key
state=present
- name: Add Nodesource Apt Sources List Deb
sudo: yes
apt_repository:
repo='deb https://deb.nodesource.com/node "{{ ansible_distribution_release }}" main'
state=present
when: distrosupported|success
- name: Add Nodesource Apt Sources List Deb Src
sudo: yes
apt_repository:
repo='deb-src https://deb.nodesource.com/node "{{ ansible_distribution_release }}" main'
state=present
when: distrosupported|success
- name: Install NodeJS
sudo: yes
apt: pkg=nodejs state=latest update_cache=true
when: distrosupported|success
- debug: msg="{{npm_pkgs}}"
- name: install global npm packages
sudo: yes
npm: name={{item}} global=yes state=latest
with_items: "{{npm_pkgs}}"
I was using this playbook for installing node 6.1.0 via nvm(node version manager):
Note: You may need to change the hosts and connection in the play.
---
- hosts: localhost
connection: local
vars:
node_version: 6.1.0
tasks:
- name: Download the nvm(node version manager) install script
get_url: url=https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh dest=/tmp/install.sh
- name: Install dependencies
apt: pkg={{ item }} update_cache=yes cache_valid_time=3600
with_items:
- git
- curl
- build-essential
- libssl-dev
become: yes
become_user: root
- name: Execute the nvm install script
shell: bash install.sh chdir=/tmp executable=/bin/bash
- name: Register the NVM_DIR
shell: echo $NVM_DIR
register: nvm_dir
- name: Install the specified node version using the nvm command and set it as default
shell: . {{ nvm_dir.stdout }}/nvm.sh && nvm install {{ node_version }} && nvm run {{node_version}} --version && nvm alias default {{node_version}}
creates=~/.nvm/versions/node/v{{ node_version }}
For more information on nvm, see: https://github.com/creationix/nvm
Based on the code in the original question, and the helpful comment from #ydaetskcoR, I was able to install NodeJS 6.x with the following on Ubuntu 16.04:
- name: Add Nodesource Keys
become: yes
apt_key:
url: https://deb.nodesource.com/gpgkey/nodesource.gpg.key
state: present
# Note: "xenial" is ubuntu-speak for 16.04
- name: Add Nodesource Apt Sources
become: yes
apt_repository:
repo: '{{ item }}'
state: present
with_items:
- 'deb https://deb.nodesource.com/node_6.x xenial main'
- 'deb-src https://deb.nodesource.com/node_6.x xenial main'
- name: Install NodeJS and NPM
become: yes
apt:
name: '{{ item }}'
state: latest
update_cache: yes
with_items:
- nodejs
- nodejs-legacy
- npm
I've refactored a little for brevity, but the most important thing is the addition of _6.x to the repository URLs.
This worked for me using Ansible 2.3.2.0

pure-uploadscript of pure-ftpd is not working on ubuntu

I am using the apt-get install the pure-ftp on ubuntu server 14.04.4
sudo apt-get install pure-ftpd
sudo pure-uploadscript -B -r /home/john/hello.sh
the hell.sh file, and it's able to run.
#!/bin/sh
echo "hello"
Then, I use FileZilla to upload the file. I can upload the file, but the script is not called. please help;
official doc
If you install the pure-ftpd server by apt-get, it may not give you the feature that you want to use. I checked the /var/run folder, some file are missing there. I complied the code with --with-uploadscript, it's working now.
I had to also compile from source, fortunately the install is not too heavy. It may be worth uploading the compiled files from your system to your mirror and just downloading and running make install. On the other hand, this works as well:
- name: install pure-ftpd from source
block:
- name: create required pure-ftpd dirs
become: yes
file:
path: /etc/pure-ftpd
state: directory
owner: root
mode: 0755
- name: install deps for building pureftpd
apt: pkg={{ item }} state=present
with_items:
- libssl-dev
- libpam0g-dev
- name: download and unpack pure-ftpd source
unarchive:
src: http://download.pureftpd.org/pub/pure-ftpd/releases/pure-ftpd-1.0.49.tar.gz
dest: /usr/local/src/
remote_src: yes
keep_newer: yes
register: source_unpack
- name: configuring pure-ftpd source with custom modules
command: "./configure --prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/libexec
--datadir=/usr/share --sysconfdir=/etc --sharedstatedir=/usr/com --localstatedir=/var --libdir=/usr/lib64
--includedir=/usr/include --infodir=/usr/share/info --mandir=/usr/share/man --with-virtualchroot --with-everything
--with-uploadscript --with-tls --with-pam"
args:
chdir: /usr/local/src/pure-ftpd-1.0.49
when: source_unpack|changed
register: pure_ftpd_configure
- name: make and install pure-ftpd
become: yes
shell: make && make install
args:
chdir: /usr/local/src/pure-ftpd-1.0.49
when: pure_ftpd_configure|changed
when: stat_result.stat.exists == False
tags:
- ftp

Ansible playbooks

I haven't used Ansible before, does anyone know how to write a simple playbook that uninstalls nano and installs vim on a Linux server? I would imagine you would need to include an option to configure which text editor preference you want after doing the above.
Cheers
edit
This is what I've got so far...
---
# Playbook to uninstall nano and install vim
- hosts: all
remote_user: luca
sudo: yes
tasks:
- name: uninstall nano
action: command: sudo apt-get purge nano
- name: Install vim
action: command: sudo apt-get install vim
tmp.yml
---
- hosts: all
tasks:
- name: nano remove
apt: name=nano state=absent
- name: vim install
apt: name=vim state=present
ansible-playbook tmp.yml
http://docs.ansible.com/apt_module.html
If you are working on fedora/centos/rhel:
---
- hosts: all
tasks:
- name: nano remove
yum: name=nano state=absent
- name: vim install
yum: name=vim state=latest
Refer to doc of yum module . Set proper args for "state": install (present or installed, latest), or remove (absent or removed) a package.
If you want to remove and install with command, you must write without 'action', like this:
tasks:
- name: uninstall nano
command: sudo apt-get purge nano
- name: Install vim
command: sudo apt-get install vim
But it is not recommended, it is better doing with 'absent'.
And I am not sure if can put sudo, so you can connect directly with your root user.
- hosts: all
remote_user: root
sudo: True
I personally find it cleaner with a loop for the installs. For the uninstall just change the "state"
hosts: desktop-linux
tasks:
- name: Install Desktop packages
apt: name={{item}} state=installed
with_items:
- meld
- synergy
- retext
- pidgin
- steam
- ubuntu-restricted-extras
- nautilus-admin
- unity-tweak-tool
- vlc

Resources