Network Engineers Learn Python-35-Open Source Automated Deployment Tool Fabric

Python Fabric is an open source automated deployment tool based on Python that simplifies the process of managing remote servers. It allows you to execute commands on one or more remote hosts, upload/download files, create and manage virtual environments, and more.

Install

Before you start using Fabric, you need to install it. You can use pip to install it:

pip install fabric

use

Fabric is a command line tool that allows you to use commands fabto run Fabric tasks. It defines tasks using the fabfile.py file, which contains one or more Python functions that represent the tasks to be performed.

Here is a sample fabfile.py file:

from fabric import Connection

def deploy(c):
    with c.cd('/var/www/html'):
        c.run('git pull origin master')
        c.sudo('systemctl restart httpd')

def uptime(c):
    result = c.run('uptime')
    print(result.stdout.strip())

In the above example, we defined two tasks: deployand uptime. deployThe task uses Git to pull the code from the warehouse and restart the Apache service. uptimeThe task simply executes uptimethe command and prints the output.

To run the task, you can use the following command in the terminal:

fab deploy -H user@host
fab uptime -H user@host

In the above example, -Hthe flags specify the address and username of the remote host. If you have multiple hosts that need to perform the same task, you can separate them with commas:

fab deploy -H user1@host1,user2@host2,user3@host3

You can also use --prompt-for-login-passwordthe option to prompt for a password, or use an SSH key for authentication.

Advanced usage

Fabric also provides many advanced options, such as executing tasks in parallel, batch deployment, using Fabric API, etc. Here are some examples:

Execute tasks in parallel

from fabric import Connection
from fabric import Config

config = Config(overrides={'run': {'pty': True}})
conn1 = Connection('user@host1', config=config)
conn2 = Connection('user@host2', config=config)
conn3 = Connection('user@host3', config=config)

result = Connection.run(conn1, 'ls', hide=True)
print(result.stdout.strip())

result = Connection.run(conn2, 'ls', hide=True)
print(result.stdout.strip())

result = Connection.run(conn3, 'ls', hide=True)
print(result.stdout.strip())

In the above example, we use Connection()methods to create three connection objects and methods Config()to create a configuration object to enable pseudo-terminal mode. We then use run()methods to execute commands on each connection object lsand print the output.

Using the Fabric API

from fabric import Connection, Config
from fabric.api import env, run, sudo

env.hosts = ['user@host1', 'user@host2']
config = Config(overrides={'run': {'pty': True}})

with Connection(env.hosts[0], config=config) as c:
    c.run('ls', hide=True)

with Connection(env.hosts[1], config=config) as c:
    c.sudo('systemctl restart httpd')

In the above example, we use envthe module to set the host list and Config()the method to create a configuration object to enable pseudo-terminal mode. We then use Connection()methods to create connection objects and execute commands on each connection object.

in conclusion

Python Fabric is a very powerful automated deployment tool that can greatly simplify the process of managing remote servers. In this article, we have listed some examples of Python Fabric that range from basic to advanced usage. Hopefully these examples will help you get started with Python Fabric and save time and effort in your automated deployment efforts.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132623434