Normally, there's an ad running in this spot, but you're using an ad blocker. To help support our blog, which provides free tutorials for everybody, we just ask that you whitelist or follow us on facebook, twitter, or subscribe using the form to the leftabove. Thank you!

    Automating a MongoDB Database Installation on a Virtual Machine with Fabric

    Select Your OS:

    In this guide, you're going to learn how to use a Fabric script to automate a MongoDB database installation on a remote virtual machine.

    In order to complete this guide, you will need to know how to set up a local virtual machine to run the Fabric scripts against. You will also need to be familiar with setting up Virtualenv, which will allow us to install all the Fabric dependencies in an isolated virtual environment so there are no dependency conflicts with any other Python projects we set up in the future. In order to get up to speed, make sure you complete the guides in the prerequisites section.

    Fabric DB Scripts VM Pack 1

    Purchase the entire collection of Fabric DB Scripts at the Codebox Systems Shop and auto-provision MongoDB, MySQL, and PostgreSQL in seconds.

    Shop now

    System requirements

    • A 32-bit or 64-bit computer with at least 8GB of RAM. 16GB or more is recommended.
    • At least 50GB of free disk space.

    Set Up the Project and Virtualenv

    After completing the prerequisites, you may still be in an active Virtualenv. If the username in your terminal session is prepended with (venv), you will want to deactivate the virtualenv by running deactivate. For this tutorial, we're going to want to start from scratch with a new project and Virtualenv. This will allow us to install Fabric to our virtual environment rather than to the global dependencies list, and keep our system clean. Let's run some commands.

    Create the Project Skeleton

    Note: An existing Vagrant VM is required.

    Also, the VM must be running when you run the Fabric command. If you do not have a running VM, please go through the prerequisites.

            cd ~
            mkdir fabric-mongodb
            cd fabric-mongodb
            virtualenv --python=/usr/bin/python2 venv
            source venv/bin/activate
            cd ~
            mkdir fabric-mongodb
            cd fabric-mongodb
            virtualenv --python=/usr/local/lib/python2.7 venv
            source venv/bin/activate

    Install Fabric Into the Virtualenv

    For this tutorial, we're going to install Fabric version 1.13.2.

          pip install fabric==1.13.2

    Fabfile Automation

    Create the Fabfile

    Create a file and name it with the following contents:

    Run the Fabfile

    This will install mongodb on the guest virtual machine. It will be up to you to SSH into the virtual machine and create a new DB and user.

        fab vagrant install_mongodb

    Create a New MongoDB DB and User

    We'll do this by simply SSHing into the remote server and running some mongodb commands.

        # Fetch VM IP address
        vagrant ssh -c "ip address show eth1 | grep 'inet ' | sed -e 's/^.*inet //' -e 's/\/.*$//'"
        # SSH in
        ssh vagrant@MY.VM.IP.ADDRESS

    You’ll be prompted for a password. Enter vagrant. You should now be logged into the virtual machine. Now run the following commands. Replace "mongoadmin" and "mongoP455word" with your own values.

        # Enter a mongodb session
        # Create an admin user
        use admin
        db.addUser({ user: "mongoadmin", pwd: "mongoP455word", roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]})

    Access MongoDB from Host

    Install mongodb on the host.

            sudo apt-get install mongodb

    Install Homebrew from

          # Install MongoDB
          brew install mongodb

    And access MongoDB.

        mongo MY.VM.IP.ADDRESS/admin -umongoadmin -p

    Did you like this tutorial? Help us pay for server costs by following us on Facebook, Twitter, and subscribing below, where you'll get post notifications, training webinar invites, and free bundles.