{"id":12730,"date":"2019-03-28T15:01:31","date_gmt":"2019-03-28T15:01:31","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12730"},"modified":"2019-03-28T15:01:31","modified_gmt":"2019-03-28T15:01:31","slug":"fabric-automate-your-linux-administration-tasks-and-application-deployments-over-ssh","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/fabric-automate-your-linux-administration-tasks-and-application-deployments-over-ssh\/","title":{"rendered":"Fabric \u2013 Automate Your Linux Administration Tasks and Application Deployments Over SSH"},"content":{"rendered":"<p>When it comes to managing remote machines and deployment of applications, there are several command line tools out there in existence though many have a common problem of lack of detailed documentation.<\/p>\n<p>In this guide, we shall cover the steps to introduce and get started on how to use fabric to improve on administering groups of servers.<\/p>\n<div id=\"attachment_17184\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Automate-Linux-Administration-Tasks-Using-Fabric.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17184\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Automate-Linux-Administration-Tasks-Using-Fabric.png\" alt=\"Automate Linux Administration Tasks Using Fabric\" width=\"718\" height=\"341\" aria-describedby=\"caption-attachment-17184\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17184\" class=\"wp-caption-text\">Automate Linux Administration Tasks Using Fabric<\/p>\n<\/div>\n<p><strong>Fabric<\/strong>\u00a0is a python library and a powerful command line tool for performing system administration tasks such as executing SSH commands on multiple machines and application deployment.<\/p>\n<p><strong>Read Also<\/strong>:\u00a0<a href=\"https:\/\/www.tecmint.com\/using-shell-script-to-automate-linux-system-maintenance-tasks\/\" target=\"_blank\" rel=\"noopener\">Use Shell Scripting to Automate Linux System Maintenance Tasks<\/a><\/p>\n<p>Having a working knowledge of Python can be helpful when using\u00a0<strong>Fabric<\/strong>, but may certainly not be necessary.<\/p>\n<p>Reasons why you should choose fabric over other alternatives:<\/p>\n<ol>\n<li>Simplicity<\/li>\n<li>It is well-documented<\/li>\n<li>You don\u2019t need to learn another language if you\u2019re already a python guy.<\/li>\n<li>Easy to install and use.<\/li>\n<li>It is fast in its operations.<\/li>\n<li>It supports parallel remote execution.<\/li>\n<\/ol>\n<h3>How to Install Fabric Automation Tool in Linux<\/h3>\n<p>An important characteristic about fabric is that the remote machines which you need to administer only need to have the standard OpenSSH server installed. You only need certain requirements installed on the server from which you are administering the remote servers before you can get started.<\/p>\n<h4>Requirements:<\/h4>\n<ol>\n<li>Python 2.5+ with the development headers<\/li>\n<li>Python-setuptools and pip (optional, but preferred) gcc<\/li>\n<\/ol>\n<p><strong>Fabric<\/strong>\u00a0is easily installed using\u00a0<strong>pip<\/strong>\u00a0(highly recommended), but you may also prefer to choose your default package manager\u00a0<a href=\"https:\/\/www.tecmint.com\/20-linux-yum-yellowdog-updater-modified-commands-for-package-mangement\/\" target=\"_blank\" rel=\"noopener\">yum<\/a>,\u00a0<a href=\"https:\/\/www.tecmint.com\/dnf-commands-for-fedora-rpm-package-management\/\" target=\"_blank\" rel=\"noopener\">dnf<\/a>\u00a0or\u00a0<a href=\"https:\/\/www.tecmint.com\/useful-basic-commands-of-apt-get-and-apt-cache-for-package-management\/\" target=\"_blank\" rel=\"noopener\">apt-get<\/a>\u00a0to install fabric package, typically called\u00a0<strong>fabric<\/strong>\u00a0or\u00a0<strong>python-fabric<\/strong>.<\/p>\n<p>For\u00a0<strong>RHEL<\/strong>\/<strong>CentOS<\/strong>\u00a0based distributions, you must have\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-enable-epel-repository-for-rhel-centos-6-5\/\" target=\"_blank\" rel=\"noopener\">EPEL repository<\/a>\u00a0installed and enabled on the system to install fabric package.<\/p>\n<pre># yum install fabric   [On RedHat based systems]  \r\n# dnf install fabric   [On Fedora 22+ versions]\r\n<\/pre>\n<p>For\u00a0<strong>Debian<\/strong>\u00a0and it\u2019s derivatives such as\u00a0<strong>Ubuntu<\/strong>\u00a0and\u00a0<strong>Mint<\/strong>\u00a0users can simply do\u00a0<strong>apt-get<\/strong>\u00a0to install the fabric package as shown:<\/p>\n<pre># apt-get install fabric\r\n<\/pre>\n<p>If you want to install development version of fabric, you may use\u00a0<strong>pip<\/strong>\u00a0to grab the most recent master branch.<\/p>\n<pre># yum install python-pip       [On RedHat based systems] \r\n# dnf install python-pip       [On Fedora 22+ versions]\r\n# apt-get install python-pip   [On Debian based systems]\r\n<\/pre>\n<p>Once pip has been installed successfully, you may use pip to grab the latest version of fabric as shown:<\/p>\n<pre># pip install fabric\r\n<\/pre>\n<h3>How to Use Fabric to Automate Linux Administration Tasks<\/h3>\n<p>So lets get started on how you can use Fabric. During the installation process, a Python script called\u00a0<code>fab<\/code>\u00a0was added to a directory in your path. The\u00a0<strong>fab<\/strong>\u00a0script does all the work when using fabric.<\/p>\n<h4>Executing commands on the local Linux machine<\/h4>\n<p>By convention, you need to start by creating a Python file called\u00a0<code>fabfile.py<\/code>\u00a0using your favorite editor. Remember you can give this file a different name as you wish but you will need to specify the file path as follows:<\/p>\n<pre># fabric --fabfile \/path\/to\/the\/file.py\r\n<\/pre>\n<p>Fabric uses\u00a0<code>fabfile.py<\/code>\u00a0to execute tasks. The fabfile should be in the same directory where you run the Fabric tool.<\/p>\n<p><strong>Example 1:<\/strong>\u00a0Let\u2019s create a basic\u00a0<code>Hello World<\/code>\u00a0first.<\/p>\n<pre># vi fabfile.py\r\n<\/pre>\n<p>Add these lines of code in the file.<\/p>\n<pre>def hello():\r\n       print('Hello world, Tecmint community')\r\n<\/pre>\n<p>Save the file and run the command below.<\/p>\n<pre># fab hello\r\n<\/pre>\n<div id=\"attachment_17167\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Create-Fabric-Fab-Python-File.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17167\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Create-Fabric-Fab-Python-File.gif\" alt=\"Fabric Tool Usage\" width=\"816\" height=\"515\" aria-describedby=\"caption-attachment-17167\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17167\" class=\"wp-caption-text\">Fabric Tool Usage<\/p>\n<\/div>\n<p>Let us now look at an example of a\u00a0<strong>fabfile.py<\/strong>\u00a0to execute the\u00a0<strong>uptime<\/strong>\u00a0command on the local machine.<\/p>\n<p><strong>Example 2:<\/strong>\u00a0Open a new\u00a0<strong>fabfile.py<\/strong>\u00a0file as follows:<\/p>\n<pre># vi fabfile.py\r\n<\/pre>\n<p>And paste the following lines of code in the file.<\/p>\n<pre>#!  \/usr\/bin\/env python\r\nfrom fabric.api import local\r\ndef uptime():\r\n  local('uptime')\r\n<\/pre>\n<p>Then save the file and run the following command:<\/p>\n<pre># fab uptime\r\n<\/pre>\n<div id=\"attachment_17171\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Fabric-Uptime.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17171\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Fabric-Uptime.gif\" alt=\"Fabric: Check System Uptime\" width=\"816\" height=\"515\" aria-describedby=\"caption-attachment-17171\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17171\" class=\"wp-caption-text\">Fabric: Check System Uptime<\/p>\n<\/div>\n<h4>Executing commands on remote Linux machines to automate tasks<\/h4>\n<p>The\u00a0<strong>Fabric API<\/strong>\u00a0uses a configuration dictionary which is Python\u2019s equivalent of an associative array known as\u00a0<code>env<\/code>, which stores values that control what Fabric does.<\/p>\n<p>The\u00a0<code>env.hosts<\/code>\u00a0is a list of servers on which you want run Fabric tasks. If your network is\u00a0<strong>192.168.0.0<\/strong>\u00a0and wish to manage host\u00a0<strong>192.168.0.2<\/strong>\u00a0and\u00a0<strong>192.168.0.6<\/strong>\u00a0with your\u00a0<strong>fabfile<\/strong>, you could configure the\u00a0<strong>env.hosts<\/strong>\u00a0as follows:<\/p>\n<pre>#!\/usr\/bin\/env python\r\nfrom  fabric.api import env\r\nenv.hosts = [ '192.168.0.2', '192.168.0.6' ]\r\n<\/pre>\n<p>The above line of code only specify the hosts on which you will run Fabric tasks but do nothing more. Therefore you can define some tasks, Fabric provides a set of functions which you can use to interact with your remote machines.<\/p>\n<p>Although there are many functions, the most commonly used are:<\/p>\n<ol>\n<li><strong>run<\/strong>\u00a0\u2013 which runs a shell command on a remote machine.<\/li>\n<li><strong>local<\/strong>\u00a0\u2013 which runs command on the local machine.<\/li>\n<li><strong>sudo<\/strong>\u00a0\u2013 which runs a shell command on a remote machine, with root privileges.<\/li>\n<li><strong>Get<\/strong>\u00a0\u2013 which downloads one or more files from a remote machine.<\/li>\n<li><strong>Put<\/strong>\u00a0\u2013 which uploads one or more files to a remote machine.<\/li>\n<\/ol>\n<p><strong>Example 3:<\/strong>\u00a0To echo a message on multiple machines create a\u00a0<code>fabfile.py<\/code>\u00a0such as the one below.<\/p>\n<pre>#!\/usr\/bin\/env python\r\nfrom fabric.api import env, run\r\nenv.hosts = ['192.168.0.2','192.168.0.6']\r\ndef echo():\r\n      run(\"echo -n 'Hello, you are tuned to Tecmint ' \")\r\n<\/pre>\n<p>To execute the tasks, run the following command:<\/p>\n<pre># fab echo\r\n<\/pre>\n<div id=\"attachment_17174\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Fabrick-Automate-Linux-Tasks.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17174\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Fabrick-Automate-Linux-Tasks.gif\" alt=\"Fabric: Automate Linux Tasks on Remote Linux\" width=\"893\" height=\"458\" aria-describedby=\"caption-attachment-17174\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17174\" class=\"wp-caption-text\">Fabric: Automate Linux Tasks on Remote Linux<\/p>\n<\/div>\n<p><strong>Example 4:<\/strong>\u00a0You can improve the\u00a0<code>fabfile.py<\/code>\u00a0which you created earlier on to execute the\u00a0<strong>uptime<\/strong>\u00a0command on the local machine, so that it runs the\u00a0<strong>uptime<\/strong>\u00a0command and also checks disk usage using the\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-check-disk-space-in-linux\/\" target=\"_blank\" rel=\"noopener\">df command<\/a>\u00a0on multiple machines as follows:<\/p>\n<pre>#!\/usr\/bin\/env python\r\nfrom fabric.api import env, run\r\nenv.hosts = ['192.168.0.2','192.168.0.6']\r\ndef uptime():\r\n      run('uptime')\r\ndef disk_space():\r\n     run('df -h')\r\n<\/pre>\n<p>Save the file and run the following command:<\/p>\n<pre># fab uptime\r\n# fab disk_space\r\n<\/pre>\n<div id=\"attachment_17175\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Fabric-Run-Multiple-Commands-on-Multiple-Linux-Systems.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-17175\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2015\/11\/Fabric-Run-Multiple-Commands-on-Multiple-Linux-Systems.gif\" alt=\"Fabric: Automate Tasks on Multiple Linux Systems\" width=\"893\" height=\"458\" aria-describedby=\"caption-attachment-17175\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-17175\" class=\"wp-caption-text\">Fabric: Automate Tasks on Multiple Linux Systems<\/p>\n<\/div>\n<h4>Automatically Deploy LAMP Stack on Remote Linux Server<\/h4>\n<p><strong>Example 4:<\/strong>\u00a0Let us look at an example to deploy\u00a0<strong>LAMP<\/strong>\u00a0(Linux, Apache, MySQL\/MariaDB and PHP) server on a remote Linux server.<\/p>\n<p>We shall write a function that will allow LAMP to be installed remotely using root privileges.<\/p>\n<h5>For RHEL\/CentOS and Fedora<\/h5>\n<pre>#!\/usr\/bin\/env python\r\nfrom fabric.api import env, run\r\nenv.hosts = ['192.168.0.2','192.168.0.6']\r\ndef deploy_lamp():\r\n  run (\"yum install -y httpd mariadb-server php php-mysql\")\r\n<\/pre>\n<h5>For Debian\/Ubuntu and Linux Mint<\/h5>\n<pre>#!\/usr\/bin\/env python\r\nfrom fabric.api import env, run\r\nenv.hosts = ['192.168.0.2','192.168.0.6']\r\ndef deploy_lamp():\r\n  sudo(\"apt-get install -q apache2 mysql-server libapache2-mod-php5 php5-mysql\")\r\n<\/pre>\n<p>Save the file and run the following command:<\/p>\n<pre># fab deploy_lamp\r\n<\/pre>\n<p><strong>Note<\/strong>: Due to large output, it\u2019s not possible for us to create a screencast (animated gif) for this example.<\/p>\n<p>Now you can able to\u00a0<a href=\"https:\/\/www.tecmint.com\/use-ansible-playbooks-to-automate-complex-tasks-on-multiple-linux-servers\/\" target=\"_blank\" rel=\"noopener\">automate Linux server management tasks<\/a>\u00a0using Fabric and its features and examples given above\u2026<\/p>\n<h4>Some Useful Options to Use with Fabric<\/h4>\n<ol>\n<li>You can run\u00a0<strong>fab \u2013help<\/strong>\u00a0to view help information and a long list of available command line options.<\/li>\n<li>An important option is\u00a0<strong>\u2013fabfile=PATH<\/strong>\u00a0that helps you to specify a different python module file to import other then\u00a0<strong>fabfile.py<\/strong>.<\/li>\n<li>To specify a username to use when connecting to remote hosts, use the\u00a0<strong>\u2013user=USER<\/strong>\u00a0option.<\/li>\n<li>To use password for authentication and\/or\u00a0<strong>sudo<\/strong>, use the\u00a0<strong>\u2013password=PASSWORD<\/strong>\u00a0option.<\/li>\n<li>To print detailed info about command NAME, use\u00a0<strong>\u2013display=NAME<\/strong>\u00a0option.<\/li>\n<li>To view formats use\u00a0<strong>\u2013list<\/strong>\u00a0option, choices: short, normal, nested, use the\u00a0<strong>\u2013list-format=FORMAT<\/strong>\u00a0option.<\/li>\n<li>To print list of possible commands and exit, include the\u00a0<strong>\u2013list<\/strong>\u00a0option.<\/li>\n<li>You can specify the location of config file to use by using the\u00a0<strong>\u2013config=PATH<\/strong>\u00a0option.<\/li>\n<li>To display a colored error output, use\u00a0<strong>\u2013colorize-errors<\/strong>.<\/li>\n<li>To view the program\u2019s version number and exit, use the\u00a0<strong>\u2013version<\/strong>\u00a0option.<\/li>\n<\/ol>\n<h3>Summary<\/h3>\n<p>Fabric is a powerful tool and is well documented and provides easy usage for newbies. You can read the full documentation to get more understanding of it. If you have any information to add or incase of any errors you encounter during installation and usage, you can leave a comment and we shall find ways to fix them.<\/p>\n<p>Reference:\u00a0<a href=\"http:\/\/docs.fabfile.org\/en\/1.4.0\/usage\/env.html\" target=\"_blank\" rel=\"nofollow noopener\">Fabric documentation<\/a><\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/automating-linux-system-administration-tasks\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to managing remote machines and deployment of applications, there are several command line tools out there in existence though many have a common problem of lack of detailed documentation. In this guide, we shall cover the steps to introduce and get started on how to use fabric to improve on administering groups &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/fabric-automate-your-linux-administration-tasks-and-application-deployments-over-ssh\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fabric \u2013 Automate Your Linux Administration Tasks and Application Deployments Over SSH&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12730","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=12730"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12730\/revisions"}],"predecessor-version":[{"id":12731,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12730\/revisions\/12731"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}