100 Days of DevOps — Day 75- Introduction to Fabric

rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
yum -y install python-pip
pip install fabric
Collecting fabric
Installing collected packages: fabric
Successfully installed fabric-1.13.1
[root@fabric-server ~]# fab -hUsage: fab [options] <command>[:arg1,arg2=val2,host=foo,hosts=’h1;h2',…] …
  • Now let say I want to check disk-space utilization of Client(192.168.0.16)
[root@fabric-server ~]# fab -H 192.168.0.16 -I — df -THInitial value for env.password:[192.168.0.16] Executing task ‘<remainder>’[192.168.0.16] run: df -TH[192.168.0.16] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.16] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.16] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.16] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.16] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.16] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.16] out:Done.Disconnecting from 192.168.0.16… done.
-I, — initial-password-promptForce password prompt up-front-H HOSTS, — hosts=HOSTScomma-separated list of hosts to operate on
  • In case of multiple hosts, we can pass hostname/IP via comma separated value
[root@fabric-server ~]# fab -H 192.168.0.15,192.168.0.16 -I — df -THInitial value for env.password:[192.168.0.15] Executing task ‘<remainder>’[192.168.0.15] run: df -TH[192.168.0.15] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.15] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.15] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.15] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.15] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.15] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.15] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.15] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.15] out:[192.168.0.16] Executing task ‘<remainder>’[192.168.0.16] run: df -TH[192.168.0.16] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.16] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.16] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.16] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.16] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.16] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.16] out:Done.Disconnecting from 192.168.0.16… done.Disconnecting from 192.168.0.15… done.
  • In both the above cases I need to pass password at the prompt,but in case of automation we need some way so that we don’t need to pass password each time
export password=”test12"
[root@fabric-server ~]# fab -H 192.168.0.15,192.168.0.16 -p $password — df -TH[192.168.0.15] Executing task ‘<remainder>’[192.168.0.15] run: df -TH[192.168.0.15] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.15] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.15] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.15] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.15] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.15] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.15] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.15] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.15] out:[192.168.0.16] Executing task ‘<remainder>’[192.168.0.16] run: df -TH[192.168.0.16] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.16] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.16] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.16] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.16] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.16] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.16] out:Done.Disconnecting from 192.168.0.16… done.Disconnecting from 192.168.0.15… done.
  • All these command executed linearly i.e one after the other, in case we need make these threads run in parallel use (-P)
[root@fabric-server ~]# fab -H 192.168.0.15,192.168.0.16 -p $password -P — df -TH[192.168.0.15] Executing task ‘<remainder>’[192.168.0.16] Executing task ‘<remainder>’[192.168.0.16] run: df -TH[192.168.0.15] run: df -TH[192.168.0.16] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.16] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.16] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.16] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.16] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.16] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.16] out:[192.168.0.15] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.15] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.15] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.15] out: tmpfs tmpfs 521M 13k 521M 1% /dev/shm[192.168.0.15] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.15] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.15] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.15] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.15] out:Done.
  • Now to control how many thread to fork when run in parallel(-z number of concurrent processes to use in parallel mode)
[root@fabric-server ~]# fab -H 192.168.0.15,192.168.0.16 -p $password -z 1 — df -TH[192.168.0.15] Executing task ‘<remainder>’[192.168.0.15] run: df -TH[192.168.0.15] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.15] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.15] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.15] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.15] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.15] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.15] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.15] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.15] out:[192.168.0.16] Executing task ‘<remainder>’[192.168.0.16] run: df -TH[192.168.0.16] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.16] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.16] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.16] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.16] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.16] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.16] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.16] out:Done.Disconnecting from 192.168.0.16… done.Disconnecting from 192.168.0.15… done.
[root@fabric-server ~]# fab -H 192.168.0.15,192.168.0.16 -p $password -z 1 — df -TH >> /tmp/faboutput
  • Now a days most of the companies running there infrastructure in cloud,in case of AWS cloud we need to modify our command little bit
fab -H ec-<ip add>.us-west-2.compute.amazonaws.com -u ec2-user -i test.pem — df -TH[ec-<ip add>.us-west-2.compute.amazonaws.com] Executing task ‘<remainder>’[ec-<ip add>.us-west-2.compute.amazonaws.com] run: df -TH[ec-<ip add>.us-west-2.compute.amazonaws.com] out: Filesystem Type Size Used Avail Use% Mounted on[ec-<ip add>.us-west-2.compute.amazonaws.com] out: /dev/xvda2 xfs 11G 1.9G 9.0G 17% /[ec-<ip add>.us-west-2.compute.amazonaws.com] out: devtmpfs devtmpfs 500M 0 500M 0% /dev[ec-<ip add>.us-west-2.compute.amazonaws.com] out: tmpfs tmpfs 520M 0 520M 0% /dev/shm[ec-<ip add>.us-west-2.compute.amazonaws.com] out: tmpfs tmpfs 520M 66M 455M 13% /run[ec-<ip add>.us-west-2.compute.amazonaws.com] out: tmpfs tmpfs 520M 0 520M 0% /sys/fs/cgroup[ec-<ip add>.us-west-2.compute.amazonaws.com] out: tmpfs tmpfs 104M 0 104M 0% /run/user/1000[ec-<ip add>.us-west-2.compute.amazonaws.com] out:Done.Disconnecting from ec-<ip add>.us-west-2.compute.amazonaws.com… done.
i PATH path to SSH private key file. May be repeated-u USER, — user=USER username to use when connecting to remote hosts
  • So far all these commands we are executing on the command line,in order to automate the whole process we need to create fabfile,which is a simple python script(here you can see we combine both memory as well as disk space utilization)
#Fabfile to:# — Run the command on remote system(s)# — Check the memory and disk space#Import Fabric’s API Modulefrom fabric.api import *env.hosts = [‘192.168.0.15’,‘192.168.0.16’]# Set the usernameenv.user = “root”# Set the password[NOT RECOMMENDED]”env.password =“test12”def memory_check():“””Check Memory usage on remote system“””run(“free -m”)def disk_check():“””Check Disk space usage on remote system“””run(“df -TH”)def tesenv():memory_check()disk_check
[root@fabric-server ~]# fab -lAvailable commands:disk_checkmemory_checktestenv
[root@fabric-server ~]# fab testenv[192.168.0.17] Executing task ‘testenv’[192.168.0.17] run: free -m[192.168.0.17] out: total used free shared buff/cache available[192.168.0.17] out: Mem: 992 79 701 6 211 762[192.168.0.17] out: Swap: 499 0 499[192.168.0.17] out:[192.168.0.17] run: df -TH[192.168.0.17] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.17] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.17] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.17] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.17] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.17] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.17] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.17] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.17] out:[192.168.0.18] Executing task ‘testenv’[192.168.0.18] run: free -m[192.168.0.18] out: total used free shared buff/cache available[192.168.0.18] out: Mem: 992 106 477 6 409 728[192.168.0.18] out: Swap: 499 0 499[192.168.0.18] out:[192.168.0.18] run: df -TH[192.168.0.18] out: Filesystem Type Size Used Avail Use% Mounted on[192.168.0.18] out: /dev/mapper/cl-root xfs 20G 1.4G 19G 7% /[192.168.0.18] out: devtmpfs devtmpfs 510M 0 510M 0% /dev[192.168.0.18] out: tmpfs tmpfs 521M 0 521M 0% /dev/shm[192.168.0.18] out: tmpfs tmpfs 521M 7.0M 514M 2% /run[192.168.0.18] out: tmpfs tmpfs 521M 0 521M 0% /sys/fs/cgroup[192.168.0.18] out: /dev/sda1 xfs 102M 98M 3.7M 97% /boot[192.168.0.18] out: tmpfs tmpfs 105M 0 105M 0% /run/user/0[192.168.0.18] out:

--

--

--

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Livestream from the Web with Zoom Video SDK to Amazon IVS

Six o’ clock in the morning, you’re the last to hear the warning

How disabling external IPs helped us cut down over 80% of our Cloud Dataflow costs

Luhn Algorithm

Daggerito - Flutter dependency injection framework

Master The Skills Of Iot In Civil Engineering And Be Successful.

TPMS: Small sensors give you big peace of mind

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Lakhera

Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

More from Medium

Learn VPC in Alibaba Cloud

Deploying an EC2 Instance using Terraform

OpenShift Local Lab Setup on RHEL VM

Setting up Apache Web Server on AWS EC2 Instance Using Ansible