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!

    Server Provisioning Automation with Python and Fabric

    Select Your OS:

    This tutorial will serve as a basic introduction to running Fabric scripts against a virtual machine spun up using Vagrant and VirtualBox. Fabric is a Python (2.5-2.7) library that allows us to automate command line operations and other systems administration tasks.

    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.

    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-commands
            cd fabric-commands
            virtualenv --python=/usr/bin/python2 venv
            source venv/bin/activate
            
          
            
            cd ~
            mkdir fabric-commands
            cd fabric-commands
            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
          
        

    Python Automation with Fabric

    Create the Fabfile

    Create a file and name it fabfile.py with the following contents:

    Run the Fabfile

          
          fab vagrant hostname
          [localhost] local: vagrant global-status | grep running
          [localhost] local: vagrant ssh-config 2fbb51c | grep IdentityFile
          [127.0.0.1:2222] Executing task 'hostname'
          [127.0.0.1:2222] run: hostname
          [127.0.0.1:2222] out: vagrant-ubuntu-trusty-64
          [127.0.0.1:2222] out:  
          Done.
          Disconnecting from 127.0.0.1:2222... done.
          
        

    Possible Error: Fatal error: Host key for 127.0.0.1 did not match pre-existing key!

    • Host OS: Ubuntu 16.04
    • Guest OS: Ubuntu 14.04
    • Vagrant versions: 1.8.1, 1.9.5, 2.0.0
    • VirtualBox versions: 5.0.26, 5.1.22 r115126 (Qt5.5.1)
    • Python 2.7.12
    • Fabric 1.13.2
            [localhost] local: vagrant global-status | grep running
    [localhost] local: vagrant ssh-config e48abe0 | grep IdentityFile
    [127.0.0.1:2222] Executing task 'hostname'
    [127.0.0.1:2222] run: hostname  
    Fatal error: Host key for 127.0.0.1 did not match pre-existing key! Server's key was changed recently, or possible man-in-the-middle attack.  
    Underlying exception:
        ('127.0.0.1', , )  
    Aborting.
          

    Solution: Remove the old key from the known_hosts file

    1. Run the following command in the terminal:
    2.           ssh-keygen  -R [127.0.0.1]:2222
              
    3. Rerun the fab vagrant hostname command. If you get the same error, you will have to manually delete the 127.0.0.1 line from ~/.ssh/known_hosts.

    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.