How to install Fabric in Linux
Installation of Fabric in Linux
Fabric is a command-line tool that uses SSH for application deployment or system administration tasks. It performs operations like uploading/downloading files, prompting the running user for input, or aborting execution. Methods of introducing fabric usage for the enhancement on administering server groups is explained in this article.
Features
- Parallel remote execution support.
- Operational speed.
- Easy installation and usage.
- No need for learning another language.
- Well-documentation.
Specifications
- pip gcc and Python-setup tools.
- Python 2.5+ as well as the development headers.
For CentOS/RHEL based distributions
Enabled with EPEL repository, before installing fabric .
[root@linuxhelp1 ~]# yum install epel-release -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.webwerks.com
* extras: centos.webwerks.com
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-6 will be installed
.
.
.
Installed:
epel-release.noarch 0:7-6
Complete!
[root@linuxhelp1 ~]# yum install fabric -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.webwerks.com
* epel: epel.mirror.net.in
* extras: centos.webwerks.com
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package fabric.noarch 0:1.6.2-1.el7 will be installed
--> Processing Dependency: python-paramiko > = 1.10.0 for package: fabric-1.6.2-1.el7.noarch
.
.
.
Installed:
fabric.noarch 0:1.6.2-1.el7
Dependency Installed:
libtomcrypt.x86_64 0:1.17-23.el7 libtommath.x86_64 0:0.42.0-4.el7
python-six.noarch 0:1.9.0-2.el7 python2-crypto.x86_64 0:2.6.1-9.el7
python2-ecdsa.noarch 0:0.13-4.el7 python2-paramiko.noarch 0:1.16.1-1.el7
Complete!
For Debian
Run the following command to install fabric package.
[root@linuxhelp1 ~]# apt-get install fabric
[root@linuxhelp1 ~]# yum install python-pip -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.webwerks.com
* epel: epel.mirror.net.in
* extras: centos.webwerks.com
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package python-pip.noarch 0:7.1.0-1.el7 will be installed
--> Finished Dependency Resolution
.
.
.
Installed:
python-pip.noarch 0:7.1.0-1.el7
Complete!
To Execute fabric on Local machine
Add a Python code to fabfile.py and create a basic hello function inside the file.
[root@linuxhelp1 ~]# vim fabfile.py
def hello(): print(' Welcome to linuxhelp.com' )
Save the file and exit
Execute the below command to verify the fab file.
[root@linuxhelp1 ~]# fab hello
Welcome To linxhelp.com
Done.
To execute the uptime command, open a new fabfile.py file.
[root@linuxhelp1 ~]# vim fabfile.py
Insert the below code in the file.
#! /usr/bin/env python from fabric.api import local def uptime(): local(' uptime' )
Save the file and execute the below command in the terminal to verify the uptime.
[root@linuxhelp1 ~]# fab uptime
[localhost] local: uptime
04:59:45 up 1:01, 2 users, load average: 0.14, 0.25, 0.44
Done.
To Execute commands on remote Linux machines
Functions of Fabric
- ' Put' for uploading one or more files to a remote machine.
- ' Get' for downloading one or more files from a remote machine.
- ' sudo' for running a shell command with root privileges on a remote machine.
- ' local' for running command on the local machine.
- ' run' for running a shell command on a remote machine.
Create a fabfile.py for displaying a message on multiple machines.
[root@linuxhelp1 ~]# vim fabfile.py
#!/usr/bin/env python from fabric.api import env, run env.hosts = [' 192.168.7.208' ,' 192.168.7.235' ] def echo(): run(" echo -n ‘ Welcome to linuxhelp.com’ " )
Execute the below command for task execution
[root@linuxhelp1 ~]# fab echo
[192.168.7.208] Executing task ' echo'
[192.168.7.208] run: echo -n ' Welcome To linuxhelp.com '
[192.168.7.208] Login password for ' root' :
[192.168.7.208] out: Welcome To linuxhelp.com
[192.168.7.235] Executing task ' echo'
[192.168.7.235] run: echo -n ' Welcome To linuxhelp.com '
[192.168.7.235] out: Welcome To linuxhelp.com
Done.
Disconnecting from 192.168.7.208... done.
Disconnecting from 192.168.7.235... done.
Open the fabfile.py for the execution of the uptime command on the remote machine. It checks disk usage and runs the uptime command with the help of the df command
[root@linuxhelp1 ~]# vim fabfile.py
#!/usr/bin/env python
from fabric.api import env, run
env.hosts = [' 192.168.7.208' ,' 192.168.7.235' ]
def uptime():
run(' uptime' )
def disk_space():
run(' df -h' )
Save the file and exit
[root@linuxhelp1 ~]# fab uptime
[192.168.7.208] Executing task ' uptime'
[192.168.7.208] run: uptime
[192.168.7.208] Login password for ' root' :
[192.168.7.208] out: 19:39:14 up 2:16, 3 users, load average: 0.00, 0.02, 0.05
[192.168.7.208] out:
[192.168.7.235] Executing task ' uptime'
[192.168.7.235] run: uptime
[192.168.7.235] out: 05:09:15 up 1:11, 3 users, load average: 0.04, 0.14, 0.30
[192.168.7.235] out:
Done.
Disconnecting from 192.168.7.208... done.
Disconnecting from 192.168.7.235... done.
[root@linuxhelp1 ~]# fab disk_space
[192.168.7.208] Executing task ' disk_space'
[192.168.7.208] run: df -h
[192.168.7.208] Login password for ' root' :
[192.168.7.208] out: Filesystem Size Used Avail Use% Mounted on
[192.168.7.208] out: /dev/sda3 15G 4.5G 11G 31% /
[192.168.7.208] out: devtmpfs 482M 0 482M 0% /dev
[192.168.7.208] out: tmpfs 490M 140K 490M 1% /dev/shm
[192.168.7.208] out: tmpfs 490M 7.0M 483M 2% /run
[192.168.7.208] out: tmpfs 490M 0 490M 0% /sys/fs/cgroup
[192.168.7.208] out: /dev/sda1 297M 106M 192M 36% /boot
[192.168.7.208] out: /dev/sr0 3.9G 3.9G 0 100% /run/media/root/CentOS 7 x86_64
[192.168.7.208] out:
[192.168.7.235] Executing task ' disk_space'
[192.168.7.235] run: df -h
[192.168.7.235] out: Filesystem Size Used Avail Use% Mounted on
[192.168.7.235] out: /dev/sda3 15G 8.3G 6.4G 57% /
[192.168.7.235] out: devtmpfs 486M 0 486M 0% /dev
[192.168.7.235] out: tmpfs 494M 140K 494M 1% /dev/shm
[192.168.7.235] out: tmpfs 494M 7.1M 487M 2% /run
[192.168.7.235] out: tmpfs 494M 0 494M 0% /sys/fs/cgroup
[192.168.7.235] out: /dev/sda1 297M 106M 192M 36% /boot
[192.168.7.235] out:
Done.
Disconnecting from 192.168.7.208... done.
Disconnecting from 192.168.7.235... done.
To Deploy apache on Remote Linux Server
Write a python code which allows apache installation with root privileges.
For CentOS/RHEL and Fedora
[root@linuxhelp1 ~]# vim fabfile.py
#!/usr/bin/env python
from fabric.api import env, run
env.hosts = [' 192.168.7.208' ,' 192.168.7.235' ]
def httpd():
run (" yum install -y httpd " )
For Ubuntu/Debian and Linux Mint
#!/usr/bin/env python
from fabric.api import env, run
env.hosts = [' 192.168.7.208' ,' 192.168.7.235' ]
def apache():
sudo(" apt-get install apache2 " )
Save the file and exit.
Execute the below command:
[root@linuxhelp1 ~]# fab httpd
[192.168.7.208] Executing task ' httpd'
[192.168.7.208] run: yum install -y httpd
[192.168.7.208] Login password for ' root' :
[192.168.7.208] out: Loaded plugins: fastestmirror, langpacks
[192.168.7.208] out: Loading mirror speeds from cached hostfile
[192.168.7.208] out: * base: mirror.nbrc.ac.in
[192.168.7.208] out: * extras: mirror.nbrc.ac.in
[192.168.7.208] out: * updates: mirror.nbrc.ac.in
[192.168.7.208] out: Resolving Dependencies
[192.168.7.208] out: --> Running transaction check
.
.
.
[192.168.7.235] out: Installed:
[192.168.7.235] out: httpd.x86_64 0:2.4.6-40.el7.centos.1
[192.168.7.235] out:
[192.168.7.235] out: Dependency Installed:
[192.168.7.235] out: apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-40.el7.centos.1
[192.168.7.235] out: mailcap.noarch 0:2.1.41-2.el7
[192.168.7.235] out:
[192.168.7.235] out: Complete!
[192.168.7.235] out:
Done.
Disconnecting from 192.168.7.208... done.
Disconnecting from 192.168.7.235... done.
Some Useful Options of Utilizing with Fabric
- --version option &rarr to view the program’ s version number and exit.
- --colorize-errors option&rarr to display a colored error output.
- --config=PATH option, You can specify the location of config file to use.
- --list option &rarr to print list of possible commands and exit.
- --list-format=FORMAT option &rarr to view formats.
- --display=NAME option &rarr to print detailed info about command NAME.
- --password=PASSWORD option &rarr to use password for authentication.
- --user=USER option &rarr to specify the username, while connecting to remote hosts.
- --fabfile=PATH option &rarr to specify a different python module file which is imported other than fabfile.py.
- --help option &rarr to view a large list of available command line options.
To update latest version
To get the fabric' s latest version, utilize pip.
[root@linuxhelp1 ~]# pip install fabric
Comments ( 0 )
No comments available