Apache Airflow SSH Provider

4.3.3 · active · verified Sun Apr 05

This package provides operators, hooks, and sensors for interacting with SSH, SFTP, and SCP within Apache Airflow DAGs. It enables automation of tasks on remote servers via SSH protocol, including command execution and file transfers, supporting various authentication methods. The current version is 4.3.3, and its release cadence is tied to the broader Apache Airflow provider release schedule, with frequent updates.

Warnings

Install

Imports

Quickstart

This example demonstrates a basic DAG using the `SSHOperator` to connect to a remote server and execute a shell command. Before running, configure an 'SSH' connection in your Airflow UI (Admin -> Connections) with `Conn Id` as `ssh_default`, providing `Host`, `Login (Username)`, and `Port`. For authentication, you can specify `Password`, `Key File` path, or `Private Key` content in the 'Extra' field as a JSON object (e.g., `{"key_file": "/path/to/your/key.pem"}`).

import os
from datetime import datetime
from airflow.models.dag import DAG
from airflow.providers.ssh.operators.ssh import SSHOperator

# For local testing, ensure you have an 'ssh_default' connection configured in Airflow UI
# or via an environment variable, e.g., AIRFLOW_CONN_SSH_DEFAULT=ssh://user@hostname:22/?key_file=/path/to/key
# For this example, we'll mock the connection_id.

# Example of setting connection details via environment variable for local execution
# os.environ['AIRFLOW_CONN_SSH_DEFAULT'] = 'ssh://your_user@your_host:22'
# If using a private key file:
# os.environ['AIRFLOW_CONN_SSH_DEFAULT'] = 'ssh://your_user@your_host:22?key_file=/path/to/your/private_key.pem'

with DAG(
    dag_id='ssh_operator_quickstart',
    start_date=datetime(2023, 1, 1),
    schedule_interval=None,
    catchup=False,
    tags=['ssh', 'example'],
) as dag:
    ssh_task = SSHOperator(
        task_id='run_remote_command',
        ssh_conn_id='ssh_default',  # Ensure this connection exists in Airflow
        command='echo "Hello from remote host $(hostname)" && ls -l',
        cmd_timeout=10,  # Timeout for the command execution
    )

view raw JSON →