In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to install Fabric on Linux. It is very detailed and has a certain reference value. Friends who are interested must finish it!
Fabric is a command line tool library written in Python, which can help system administrators perform certain tasks efficiently, such as executing certain commands on multiple machines through SSH, remotely deploying applications, and so on.
Before using it, if you have experience with Python, it will help you to use Fabric better. Of course, if not, it does not affect the use of Fabric.
Why should we choose Fabric:
simple
Complete documentation
If you can Python, you don't have to increase the cost of learning other languages.
Easy to install and use
Easy to use
Support parallel operation of multiple machines
How to install Fabric on Linux
One feature of Fabric is that machines to be operated remotely only need to support standard OpenSSH services. As long as you make sure that the service is installed and enabled on the machine, you can use Fabric to manage the machine.
Dependence
Python 2.5 or later, and the corresponding development components
Python-setuptools and pip (optional, but highly recommended) gcc
We recommend using pip to install Fabric, but you can also install it using a package manager that comes with the system, such as yum, dnf or apt-get. The package name is usually fabric or python-fabric.
If it is a RHEL/CentOS-based distribution system, you can use the EPEL source that comes with the system to install fabric.
# yum install fabric [for RedHat-based systems] # dnf install fabric [for Fedora 22 + version]
If you are a user of Debian or its derivative systems such as Ubuntu and Mint, you can use apt-get to install it, as shown below:
# apt-get install fabric
If you want to install the development version of Fabric, you need to install pip to install the * version on the master branch.
# yum install python-pip [for RedHat-based systems] # dnf install python-pip [for Fedora 22 + version] # apt-get install python-pip [for Debian-based systems]
After installing pip, you can use pip to get the * version of Fabric.
# pip install fabric
How to automate the management of Linux tasks using Fabric
Now let's start using Fabric. During the previous installation, the Fabric Python script has been placed in our system directory, and we can enter the fab command when we want to run Fabric.
Run the command line on the local Linux machine
By convention, create a Python script called fabfile.py with your favorite editor. You can name the script with a different name, but you need to specify the path to the script, as shown below:
# fabric-- fabfile / path/to/the/file.py
Fabric uses fabfile.py to perform tasks, and this file should be placed in the directory where you executed the Fabric command.
Example 1: create an entry-level Hello World task:
# vi fabfile.py
Enter the following in the file:
Def hello (): print ('Hello world, Tecmint community')
Save the file and execute the following command:
# fab hello
Instructions for using the Fabric tool
Example 2: create a new file named fabfile.py and open:
Paste the following code into the file:
#! / usr/bin/env python from fabric.api import local def uptime (): local ('uptime')
Save the file and execute the following command:
# fab uptime
Fabric: check the uptime of the system
Let's take a look at this example, where the fabfile.py file executes the command uptime locally.
Run commands on a remote Linux machine to perform automated tasks
Fabric API uses an associative array called env (a dictionary in Python) as the configuration directory to store information about the machines that Fabric wants to control.
Env.hosts is a list of machines on which you want to perform Fabric tasks. If your IP address is 192.168.0.0 and you want to use Fabric to manage machines with addresses 192.168.0.2 and 192.168.0.6, the required configuration is as follows:
#! / usr/bin/env python from fabric.api import env env.hosts = ['192.168.0.2', '192.168.0.6']
The above lines of code just declare the host address where you want to perform the Fabric task, but you don't actually perform any tasks, so let's define some tasks. Fabric provides a series of ways to interact with remote servers.
Fabric provides a number of methods, and here are a few that are often used:
Run-shell commands that can be run on a remote machine
Local-shell commands that can be run on the local machine
Sudo-A shell command that runs on a remote machine with root privileges
Get-download one or more files from a remote machine
Put-upload one or more files to a remote machine
Example 3: output information on multiple machines and create a new fabfile.py file as follows
#! / usr/bin/env python from fabric.api import env, run env.hosts = ['192.168.0.2] def echo (): run ("echo-n' Hello, you are tuned to Tecmint'")
Run the following command to execute the Fabric task
# fab echo
Fabric: automatically perform tasks on remote Linux machines
Example 4: you can continue to improve the previously created fabfile.py file that executes uptime tasks so that it can run uptime commands on multiple servers or check its disk usage, as shown below:
#! / usr/bin/env pythonfrom fabric.api import env, runenv.hosts = ['192.168.0.2] def uptime (): run (' uptime') def disk_space (): run ('df-h')
Save and execute the following command
# fab uptime# fab disk_space
Fabric: automatically perform tasks on multiple servers
Automate the deployment of LAMP on remote servers
Example 5: let's try deploying LAMP (Linux, Apache, MySQL/MariaDB and PHP) on a remote server.
We need to write a function to install LAMP remotely with root permissions.
On RHEL/CentOS or Fedora
#! / usr/bin/env python from fabric.api import env, run env.hosts = ['192.168.0.2] def deploy_lamp (): run ("yum install-y httpd mariadb-server php php-mysql")
On Debian/Ubuntu or Linux Mint
#! / usr/bin/env python from fabric.api import env, run env.hosts = ['192.168.0.2] def deploy_lamp (): sudo ("apt-get install-Q apache2 mysql-server libapache2-mod-php5 php5-mysql")
Save and execute the following command:
# fab deploy_lamp
Note: since a large amount of information will be output during installation, we will not provide a screen gif diagram for this example.
Now you can use the functions shown in Fabric and the example above to automate the management of tasks on the Linux server.
Some useful options for Fabric
You can run fab-help to output help information, which lists all the command line information you can use
The-fabfile=PATH option allows you to define modules other than those named fabfile.py
If you want to log in to the remote host with the specified user name, use the-user=USER option
If you need a password for authentication or sudo rights, please use the-password=PASSWORD option
If you need to output the details of a command, use the-display= command name option
Use-- list to output all available tasks
Use the-- list-format=FORMAT option to format the output information from the-list option, optionally short, normal, nested
-- the config=PATH option specifies the address where the configuration file is read
-- colorize-errors can display error output information in color
-- version outputs the current version
The above is all the contents of the article "how to install Fabric on Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.