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.
- 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.
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.
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
For this tutorial, we're going to install Fabric version
pip install fabric==1.13.2
Create a file and name it
fabfile.py with the following contents:
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.
- 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.
- Run the following command in the terminal:
- Rerun the
fab vagrant hostnamecommand. If you get the same error, you will have to manually delete the
ssh-keygen -R [127.0.0.1]:2222
- Python Crash Course: A Hands-On, Project-Based Introduction to Programming
- Learning Python, 5th Edition
- Python Pocket Reference: Python In Your Pocket (Pocket Reference (O'Reilly))
- Automate the Boring Stuff with Python: Practical Programming for Total Beginners
- Learn Python 3 the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code (Zed Shaw's Hard Way Series)
- Building Virtual Machine Labs: A Hands-On Guide
- Vagrant: Up and Running: Create and Manage Virtualized Development Environments
- Creating Development Environments with Vagrant - Second Edition
- VirtualBox Guide for Beginners