LinuxCommandLibrary

fabric

Execute commands on remote systems

TLDR

Run the setup to configure fabric

$ fabric [[-S|--setup]]
copy

List all available patterns
$ fabric [[-l|--listpatterns]]
copy

Run a pattern with input from a file
$ fabric [[-p|--pattern]] [pattern_name] < [path/to/input_file]
copy

Run a pattern on a YouTube video URL
$ fabric [[-y|--youtube]] "[https://www.youtube.com/watch?v=video_id]" [[-p|--pattern]] [pattern_name]
copy

Chain patterns together by piping output from one to another
$ fabric [[-p|--pattern]] [pattern1] | fabric [[-p|--pattern]] [pattern2]
copy

Run a custom user-defined pattern
$ fabric [[-p|--pattern]] [custom_pattern_name]
copy

Run a pattern and save the output to a file
$ fabric [[-p|--pattern]] [pattern_name] [[-o|--output]] [path/to/output_file]
copy

Run a pattern with the specified variables
$ fabric [[-p|--pattern]] [pattern_name] [[-v|--variable]] "[variable_name]:[value]"
copy

SYNOPSIS

fab [-H ] [-f ] [-u ] [-p ] [-i ] [--warn-only] [--abort-on-error] [--list] [task_name [:arg=value...]]

PARAMETERS

-H HOSTS, --hosts=HOSTS
    Specifies a comma-separated list of target host strings (e.g., 'user@host:port') on which to execute tasks. Overrides hosts defined in the fabfile.

-f FABFILE, --fabfile=FABFILE
    Specifies a custom Python fabfile to load instead of the default fabfile.py or tasks.py.

-u USER, --user=USER
    Defines the username to use for SSH authentication when connecting to remote hosts.

-p PASSWORD, --password=PASSWORD
    Provides the password for SSH authentication. This should be used with caution, as it exposes the password in the command history.

-i KEY_FILENAME, --identity=KEY_FILENAME
    Specifies the path to an SSH identity (private key) file for authentication.

-l, --list
    Displays all available tasks (Python functions) defined within the loaded fabfile.

-R ROLES, --roles=ROLES
    Executes tasks only on hosts belonging to the specified comma-separated roles, as defined in the fabfile.

-W, --warn-only
    Prevents Fabric from aborting execution immediately if a remote command returns a non-zero exit code; instead, it prints a warning and continues.

--abort-on-error
    Forces Fabric to abort execution immediately if any remote command returns a non-zero exit code. This is often the default but can be specified for clarity.

--version
    Displays the program's version number and exits.

DESCRIPTION

The fabric (often invoked as fab) command is a powerful Python library and command-line tool designed to automate the execution of shell commands remotely over SSH. It allows users to define "tasks" (Python functions) within a file, typically named fabfile.py, which can then be invoked from the command line to perform complex operations. These operations range from deploying applications and managing server configurations to orchestrating multi-server deployments.

Fabric abstracts away the complexities of direct SSH interactions, providing a high-level API for running commands, uploading and downloading files, and managing connection state. Its strength lies in its simplicity for scripting common administrative routines, making repetitive tasks efficient, reliable, and easily reproducible across multiple remote hosts. It is widely utilized for continuous deployment pipelines and general server automation due to its flexibility and ease of integration.

CAVEATS

Python 2 vs. Python 3 Compatibility: Early versions of Fabric (Fabric 1.x) were built for Python 2, while modern versions (Fabric 2.x and later) are Python 3 only. The APIs are significantly different, meaning scripts written for one version are generally not compatible with the other.

Reliance on fabfile.py: Fabric requires a Python script (fabfile.py or tasks.py) to define its operations, which might be an initial hurdle for users unfamiliar with Python or for very simple, one-off command executions.

Error Handling: While Fabric provides options like --warn-only, careful consideration must be given to error handling within tasks to ensure robust automation, as default behavior on command failure can be abrupt.

Idempotency: Fabric itself does not enforce idempotency (the property that applying an operation multiple times produces the same result as applying it once); it is up to the user to write idempotent tasks for reliable state management.

<I>FABFILE.PY</I>

The core of Fabric operations lies within the fabfile.py (or tasks.py) file. This Python script defines functions that are treated as callable tasks by the fab command-line tool. Within these tasks, users specify target hosts, define roles, and write Python code that leverages Fabric's API to execute shell commands remotely, transfer files, manipulate directories, and manage various aspects of remote systems. It's where the logic for deployment steps, server configuration, and other automation routines is encapsulated.

HISTORY

Developed by Jeff Forcier, Fabric gained significant popularity in the Python community during its 1.x series, which was built on Python 2 and the low-level SSH library Paramiko. It became a de facto standard for automating application deployments and system administration tasks due to its simplicity and Pythonic interface.

With the transition to Python 3, a major rewrite was undertaken, leading to the Fabric 2.x series. This version modularized the project, separating core functionalities into Paramiko (for SSH communication), Invoke (for task running and command-line parsing), and Fabric itself providing the high-level deployment-oriented API on top. This modernization improved maintainability and allowed for better integration with other Python tools, maintaining its relevance in modern CI/CD pipelines and custom automation scripts.

SEE ALSO

ssh(1), rsync(1), ansible(1), salt-master(1), chef-client(8), puppet(8)

Copied to clipboard