In my previous blog article on Fabric i.e. Fabric – Automate Administrative Tasks with Ease we learnt:
- What Fabric is?
- What are its features?
- What is it best suitable for?
- What are its advantages?
- What are its disadvantages?
In this article, we will cover,
- Procedures Provided by Fabric
- Example to Make Configurations on Remote Servers
Procedures Provided by Fabric
Following are the most beneficial procedures (commands or tools) that comes with Fabric to make the operations even more easier. We will use all these procedures inside the fabfile after installing Fabric.
While dealing with the remote hosts, run procedure is used to execute the shell commands. Using some variables, the output can also be stored for either future use or as log. Any shell command that we want to execute on one or more remote host needs to be placed inside the bracket.
In most cases, we need to run the commands as a superuser. Using sudo is the most commonly used procedure other than run. The same way we used run, we will use sudo by placing the desired shell command inside the brackets.
As stated above, fabric allows us to do the automation locally just like the way it allows us to automate on remote machines. For that purpose, it provides the local utility to execute commands locally. Only limitation in local procedure is, we cannot store the output. However, run and sudo procedure lets you store the output.
- get() and put()
For downloading files from the remote hosts, get() is used. We need to specify the path of source on remote and destination on local. For uploading files to the remote hosts, put() is used. Just like in get(), here also we need to specify source on local and destination on remote.
For downloading a database backup
For uploading a tar to remote
One of my favorite procedure, prompt() lets you take user input just by making users to give the input on the prompt.
prompt('Enter the preferred username for this script:', 'pyuser')
For the demo purpose, I will be working on EC2 using RHEL 7 AMI
Fabric can be installed with very easy and quick installation process with minimal system requirements. Below are the system requirements
- Python-setup Tools
- Python 2.5+ and Development Headers
- EPEL Repository Enabled
If you are using the RHEL 7 AMI, then it will have first two requirements already installed on it. You will just need to download and install EPEL Repository.
- Download and install EPEL Repo
$ yum install wget
$ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-10.noarch.rpm
$ yum install epel-release-7-10.noarch.rpm
- Update the system and install Fabric
yum install fabric
Executing Fabric Locally
Once Fabric is installed, we move towards the main pillar of Fabric i.e. fabfile – the python scripts. We will see how to create and code as per our requirements as well as how to execute them
- Create the fabfile
- Put the below content in the file
print(“Welcome to CloudThat to learn and moveUP”)
- Now save and exit the file
- As discussed in previous article, Fabric comes with inbuilt fab tools
- Using this fab tool, we will execute the function that we wrote in the fabfile locally as below
- Currently, we have just one function in the file that we executed above, but we can do many other operations using Fabric easily
- Create the fabfile
Executing Fabric on Remote Servers
Now, we will execute some commands on remote servers to see how Fabric’s actual working is
- For executing any command or to configure anything on remote servers, the servers must be able to communicate over SSH with each other
- If such setup is already ready with you, then you can use that one and if not, you will need to generate a new key and share it with remote servers
- Make sure the servers do not need any password to access it.
- In this demo, I will use two remote servers with which the Fabric machine will communicate
- Once you are ready with such setup, open the fabfile and add the below content in the file
- To import all Fabric APIs without which we will not be able to execute the commands
- To specify the remote servers and
- Some other functions that we want to run on those servers
# Import Fabric's API module
from fabric.api import *
#Set the hosts on which you want to execute the file
env.hosts = [
print("Welcome to CloudThat to learn and moveUP")
- Now, similar to executing the function locally, we will execute it on remote servers
- Above we looked at commands for gathering few basic information
- Now, we will set user and install some package on the remote servers. For that, add the below content in the same file
#Set the username
env.user = "root"
#Update remote machines
run("yum update -y")
run("yum install memcached -y")
#Execute all functions together
- Now save and exit the file
- Execute the whole script just by calling one function as below
If you notice the output, you can observe that Fabric is not idempotent as like other configuration management tools, but it is very useful and fast for routine administrative tasks.
Please feel free to share your views in the comments section below.
If you are new to automation, then you would like to check out our course on Fundamentals for DevOps.