- Consulting
- Training
- Partners
- About Us
x
In my previous blog article on Fabric i.e. Fabric – Automate Administrative Tasks with Ease we learnt:
In this article, we will cover,
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.
Example:
run(“mkdir /home/fabric”)
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.
Example:
sudo(“mkdir /home/fabric”)
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.
Example:
local(“python /home/python/setup.py”)
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.
Example:
For downloading a database backup
get("/backup/prod1db.gz", "./db.gz")
For uploading a tar to remote
put("/local/path/to/app.tar.gz", "/tmp/trunk/app.tar.gz")
One of my favorite procedure, prompt() lets you take user input just by making users to give the input on the prompt.
Example:
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
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.
$ 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
yum update
yum install fabric
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
vi fabfile.py
def welcome():
print(“Welcome to CloudThat to learn and moveUP”)
fab welcome
Now, we will execute some commands on remote servers to see how Fabric’s actual working is
NOTE:
#!/usr/bin/env python
# Import Fabric's API module
from fabric.api import *
#Set the hosts on which you want to execute the file
env.hosts = [
'10.0.0.22',
'10.0.0.23'
]
def welcome():
print("Welcome to CloudThat to learn and moveUP")
def uptime():
run('uptime')
def diskspace_available():
run('df -h')
def create_file():
run('touch demo.txt')
fab uptime
fab diskspace_available
#Set the username
env.user = "root"
#Update remote machines
def update():
run("yum update -y")
#Install memcached
def install_memcached():
run("yum install memcached -y")
#Execute all functions together
def exec_all():
welcome()
uptime()
diskspace_available()
create_file()
update()
install_memcached()
fab exec_all
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.
Voiced by Amazon Polly |
CloudThat is a leading provider of cloud training and consulting services, empowering individuals and organizations to leverage the full potential of cloud computing. With a commitment to delivering cutting-edge expertise, CloudThat equips professionals with the skills needed to thrive in the digital era.
Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!
Click to Comment