{"id":5952,"date":"2017-08-16T06:54:31","date_gmt":"2017-08-16T06:54:31","guid":{"rendered":"https:\/\/blog.cloudthat.com\/?p=5952"},"modified":"2024-06-25T11:11:48","modified_gmt":"2024-06-25T11:11:48","slug":"fabric-dive-into-usage","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/fabric-dive-into-usage","title":{"rendered":"Fabric \u2013 Dive into Usage"},"content":{"rendered":"<p>In my previous blog article on Fabric i.e. <a href=\"https:\/\/blog.cloudthat.com\/fabric-automate-administrative-tasks-with-ease\/?utm_source=blog-website&amp;utm_medium=text-link&amp;utm_campaign=fabric-automate-administrative-tasks-with-ease&amp;utm_content=fabric-drive-into-usage\">Fabric &#8211; Automate Administrative Tasks with Ease<\/a> we learnt:<\/p>\n<ul>\n<li>What Fabric is?<\/li>\n<li>What are its features?<\/li>\n<li>What is it best suitable for?<\/li>\n<li>What are its advantages?<\/li>\n<li>What are its disadvantages?<\/li>\n<\/ul>\n<p>In this article, we will cover,<\/p>\n<ul>\n<li>Procedures Provided by Fabric<\/li>\n<li>Installation<\/li>\n<li>Example to Make Configurations on Remote Servers<\/li>\n<\/ul>\n<h2><strong>Procedures Provided by Fabric<\/strong><\/h2>\n<p>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.<\/p>\n<ul>\n<li>\n<h3><strong>run()<\/strong><\/h3>\n<\/li>\n<\/ul>\n<p>While dealing with the remote hosts, <strong>run<\/strong> 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.<br \/>\nExample:<br \/>\n<code>run(\u201cmkdir \/home\/fabric\u201d)<\/code><\/p>\n<ul>\n<li>\n<h3><strong>sudo()<\/strong><\/h3>\n<\/li>\n<\/ul>\n<p>In most cases, we need to run the commands as a superuser. Using <strong>sudo<\/strong> 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.<br \/>\nExample:<br \/>\n<code>sudo(\u201cmkdir \/home\/fabric\u201d)<\/code><\/p>\n<ul>\n<li>\n<h3><strong>local()<\/strong><\/h3>\n<\/li>\n<\/ul>\n<p>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 <strong>local<\/strong> procedure is, we cannot store the output. However, run and sudo procedure lets you store the output.<br \/>\nExample:<br \/>\n<code>local(\u201cpython \/home\/python\/setup.py\u201d)<\/code><\/p>\n<ul>\n<li>\n<h3><strong>get() and put()<\/strong><\/h3>\n<\/li>\n<\/ul>\n<p>For downloading files from the remote hosts, <strong>get()<\/strong> is used. We need to specify the path of source on remote and destination on local. For uploading files to the remote hosts, <strong>put()<\/strong> is used. Just like in get(), here also we need to specify source on local and destination on remote.<br \/>\nExample:<\/p>\n<p>For downloading a database backup<br \/>\n<code>get(\"\/backup\/prod1db.gz\", \".\/db.gz\")<\/code><\/p>\n<p>For uploading a tar to remote<br \/>\n<code>put(\"\/local\/path\/to\/app.tar.gz\", \"\/tmp\/trunk\/app.tar.gz\")<\/code><\/p>\n<ul>\n<li>\n<h3><strong>prompt()<\/strong><\/h3>\n<\/li>\n<\/ul>\n<p>One of my favorite procedure, <strong>prompt()<\/strong> lets you take user input just by making users to give the input on the prompt.<br \/>\nExample:<br \/>\n<code>prompt('Enter the preferred username for this script:', 'pyuser')<\/code><\/p>\n<h2><strong>Installation<\/strong><\/h2>\n<p>For the demo purpose, I will be working on EC2 using RHEL 7 AMI<\/p>\n<p>Fabric can be installed with very easy and quick installation process with minimal system requirements. Below are the system requirements<\/p>\n<ul>\n<li>Python-setup Tools<\/li>\n<li>Python 2.5+ and Development Headers<\/li>\n<li>EPEL Repository Enabled<\/li>\n<\/ul>\n<p>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.<\/p>\n<ol>\n<li>Download and install EPEL Repo<br \/>\n<code>$ yum install wget<\/code><br \/>\n<code>$ wget http:\/\/dl.fedoraproject.org\/pub\/epel\/7\/x86_64\/e\/epel-release-7-10.noarch.rpm<\/code><br \/>\n<code>$ yum install epel-release-7-10.noarch.rpm<\/code><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/EPEL_repo_new_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5938 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/EPEL_repo_new_1.png\" alt=\"\" width=\"640\" height=\"172\" \/><\/a><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/EPEL_repo_new_2-e1502704465771.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5939 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/EPEL_repo_new_2-e1502704465771.png\" alt=\"\" width=\"600\" height=\"269\" \/><\/a><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/EPEL_repo_new_1.png\"><br \/>\n<\/a><\/li>\n<li>Update the system and install Fabric<br \/>\n<code>yum update<\/code><br \/>\n<code>yum install fabric<br \/>\n<\/code><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/yum_update.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5963 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/yum_update.png\" alt=\"\" width=\"510\" height=\"202\" \/><br \/>\n<\/a><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/yum_install.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5962 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/yum_install.png\" alt=\"\" width=\"502\" height=\"132\" \/><\/a><\/li>\n<\/ol>\n<h2><strong>Executing Fabric Locally<\/strong><\/h2>\n<p>Once Fabric is installed, we move towards the main pillar of Fabric i.e. fabfile &#8211; the python scripts. We will see how to create and code as per our requirements as well as how to execute them<\/p>\n<ol start=\"1\">\n<li style=\"list-style-type: none;\">\n<ol start=\"1\">\n<li>Create the fabfile<br \/>\n<code>vi fabfile.py<br \/>\n<\/code><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/create_fabfile_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5967 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/create_fabfile_1.png\" alt=\"\" width=\"348\" height=\"64\" \/><\/a><\/li>\n<li>Put the below content in the file<br \/>\n<code>def welcome():<\/code><br \/>\n<code>print(\u201cWelcome to CloudThat to learn and moveUP\u201d)<\/code><br \/>\n<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/content_for_fabfile_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5968 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/content_for_fabfile_1.png\" alt=\"\" width=\"522\" height=\"128\" \/><\/a><\/li>\n<li>Now save and exit the file<\/li>\n<li>As discussed in previous article, Fabric comes with inbuilt fab tools<\/li>\n<li>Using this fab tool, we will execute the function that we wrote in the fabfile locally as below<br \/>\n<code>fab welcome<br \/>\n<\/code><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fab_welcome.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5969 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fab_welcome.png\" alt=\"\" width=\"394\" height=\"96\" \/><\/a><\/li>\n<li>Currently, we have just one function in the file that we executed above, but we can do many other operations using Fabric easily<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h2><strong>Executing Fabric on Remote Servers<\/strong><\/h2>\n<p>Now, we will execute some commands on remote servers to see how Fabric\u2019s actual working is<\/p>\n<p><strong>NOTE: <\/strong><\/p>\n<ol start=\"3\">\n<li style=\"list-style-type: none;\">\n<ul>\n<li>For executing any command or to configure anything on remote servers, the servers must be able to communicate over SSH with each other<\/li>\n<li>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<\/li>\n<li>Make sure the servers do not need any password to access it.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<ol start=\"1\">\n<li>In this demo, I will use two remote servers with which the Fabric machine will communicate<\/li>\n<li>Once you are ready with such setup, open the fabfile and add the below content in the file\n<ul>\n<li>To import all Fabric APIs without which we will not be able to execute the commands<\/li>\n<li>To specify the remote servers and<\/li>\n<li>Some other functions that we want to run on those servers<\/li>\n<\/ul>\n<p><code>#!\/usr\/bin\/env python<\/code><br \/>\n<code># Import Fabric's API module<br \/>\nfrom fabric.api import *<br \/>\n<\/code><br \/>\n<code>#Set the hosts on which you want to execute the file<br \/>\nenv.hosts = [<br \/>\n'10.0.0.22',<br \/>\n'10.0.0.23'<br \/>\n]<br \/>\n<\/code><br \/>\n<code>def welcome():<br \/>\nprint(\"Welcome to CloudThat to learn and moveUP\")<br \/>\n<\/code><br \/>\n<code>def uptime():<br \/>\nrun('uptime')<br \/>\n<\/code><br \/>\n<code>def diskspace_available():<br \/>\nrun('df -h')<br \/>\n<\/code><br \/>\n<code>def create_file():<br \/>\nrun('touch demo.txt')<br \/>\n<\/code><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fabric_API_module_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5971 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fabric_API_module_1.png\" alt=\"\" width=\"574\" height=\"394\" \/><\/a><\/li>\n<li>Now, similar to executing the function locally, we will execute it on remote servers<br \/>\n<code>fab uptime<br \/>\n<\/code><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fab_uptime_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5972 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fab_uptime_1.png\" alt=\"\" width=\"648\" height=\"246\" \/><\/a><br \/>\n<code>fab diskspace_available<br \/>\n<\/code><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fab_diskapace_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5974 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fab_diskapace_1.png\" alt=\"\" width=\"648\" height=\"436\" \/><\/a><\/li>\n<li>Above we looked at commands for gathering few basic information<\/li>\n<li>Now, we will set user and install some package on the remote servers. For that, add the below content in the same file<br \/>\n<code>#Set the username<br \/>\nenv.user = \"root\"<br \/>\n<\/code><br \/>\n<code>#Update remote machines<br \/>\ndef update():<br \/>\nrun(\"yum update -y\")<br \/>\n<\/code><br \/>\n<code>#Install memcached<br \/>\ndef install_memcached():<br \/>\nrun(\"yum install memcached -y\")<br \/>\n<\/code><br \/>\n<code>#Execute all functions together<br \/>\ndef exec_all():<br \/>\nwelcome()<br \/>\nuptime()<br \/>\ndiskspace_available()<br \/>\ncreate_file()<br \/>\nupdate()<br \/>\ninstall_memcached()<br \/>\n<\/code><\/li>\n<li>Now save and exit the file<\/li>\n<li>Execute the whole script just by calling one function as below<br \/>\n<code>fab exec_all<br \/>\n<\/code><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fab_exec_1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5975 alignnone\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/fab_exec_1.png\" alt=\"\" width=\"688\" height=\"252\" \/><\/a><\/li>\n<\/ol>\n<p>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.<\/p>\n<p>Please feel free to share your views in the comments section below.<\/p>\n<p>If you are new to automation, then you would like to check out our course on <a href=\"https:\/\/cloudthat.in\/course\/fundamentals-of-devops-level-1\/?utm_source=blog-website&amp;utm_medium=text-link&amp;utm_campaign=fundamentals-of-devops&amp;utm_content=Fabric\u2013Dive-into-Usage\">Fundamentals for DevOps<\/a>.<\/p>\n","protected":false},"author":219,"featured_media":5977,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3607],"user_email":"prarthitm@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/5952"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/219"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=5952"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/5952\/revisions"}],"predecessor-version":[{"id":46001,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/5952\/revisions\/46001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=5952"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=5952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}