Overview

Requirements

HapiDeploy requires Python 3.13+, so you need to check your python version.

python --version

Installation

You can install the hapideploy library using pip.

pip install hapideploy

Check if the hapi command is available.

hapi about

If you see output like this, the installation process is successful.

Hapi CLI <version>

You can use uv, pyenv or any other python package managers.

Usage

Create the inventory.yml file like below.

remotes:
  server-1:
    host: 192.168.33.11
    user: vagrant
  server-2:
    host: 192.168.33.12
    user: vagrant
  server-3:
    host: 192.168.33.13
    user: vagrant

Create the deploy.py file and define the whoami task.

from hapi import Context
from hapi.cli import app

@app.task(name='whoami', desc='Run whoami command')
def whoami(c: Context):
    c.run('whoami')

Execute the "whoami" task on all remotes.

hapi whoami --debug
[server-1] TASK whoami
[server-1] RUN whoami
[server-1] vagrant
[server-2] TASK whoami
[server-2] RUN whoami
[server-2] vagrant
[server-3] TASK whoami
[server-3] RUN whoami
[server-3] vagrant

Try "hapi --help" for more information.

hapi --help

Example

Here is a simple deploy.py file that gives you the first sign of how HapiDeploy works.

from hapi import Context
from hapi.cli import app

# Configuration

app.put('name', 'HapiDeploy')

# Tasks

@app.task(name='hello', desc='Print a hello message')
def hello(c: Context):
    # It will inject "name" into this command and run on a remote.
    c.run('echo "Hello! Thanks for give {{name}} a try"')

@app.task(name='inspect', desc='Display inspect information')
def inspect(c: Context):
    # It prints an info message right in your terminal.
    c.info('The remote OS is printed below:')

    # It checks if /etc/os-release exists on a remote, then run cat or command just print an info message.
    if c.test('[ -f /etc/os-release ]'):
        c.run('cat /etc/os-release')
    else:
        c.info('Cannot detect remote OS')

# Hooks

app.after('hello', 'inspect') # It'll run inspect task right after hello task

Run "hapi hello server-1" will execute hello, then inspect on server-1

[server-1] TASK hello
[server-1] RUN echo "Hello! Thanks for give HapiDeploy a try"
[server-1] Hello! Thanks for give HapiDeploy a try
[server-1] TASK inspect
[server-1] INFO The remote OS is printed below:
[server-1] RUN if [ -f /etc/os-release ]; then echo +yes; fi
[server-1] +yes
[server-1] RUN cat /etc/os-release
[server-1] PRETTY_NAME="Ubuntu 24.04.3 LTS"
[server-1] NAME="Ubuntu"
[server-1] VERSION_ID="24.04"
[server-1] VERSION="24.04.3 LTS (Noble Numbat)"
[server-1] VERSION_CODENAME=noble
[server-1] ID=ubuntu
[server-1] ID_LIKE=debian
[server-1] HOME_URL="https://www.ubuntu.com/"
[server-1] SUPPORT_URL="https://help.ubuntu.com/"
[server-1] BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
[server-1] PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
[server-1] UBUNTU_CODENAME=noble
[server-1] LOGO=ubuntu-logo
Previous
Introduction