MAAS Client Library

0.6.8 · active · verified Fri Apr 17

A client API library for interacting with MAAS (Metal as a Service). It provides a programmatic interface to manage MAAS resources like machines, networks, and storage. Currently at version 0.6.8, it has a release cadence of a few releases per year, primarily for bug fixes and minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to a MAAS server using an API key and listing available machines. Uses environment variables for sensitive connection details and provides basic error handling.

import os
from maas.client import MaasClient
from maas.client.enum import LinkMode # Example: for network configuration

# Configure MAAS connection from environment variables or provide directly
MAAS_URL = os.environ.get("MAAS_URL", "https://your.maas.server:5240/MAAS")
MAAS_API_KEY = os.environ.get("MAAS_API_KEY", "your-maas-api-key")

if "your-maas-api-key" in MAAS_API_KEY:
    print("Warning: Please set the MAAS_API_KEY environment variable or replace 'your-maas-api-key'.")
if "your.maas.server" in MAAS_URL:
    print("Warning: Please set the MAAS_URL environment variable or replace 'your.maas.server'.")

try:
    # Initialize the client
    client = MaasClient(MAAS_URL, api_key=MAAS_API_KEY)
    
    # Connect to the MAAS API
    maas = client.connect()

    # Example: List all machines
    print("Fetching machines...")
    machines = maas.machines.list()
    if machines:
        print(f"Found {len(machines)} machines:")
        for machine in machines[:3]: # Print details for up to 3 machines
            print(f"  - Hostname: {machine.hostname}, Status: {machine.status}, ID: {machine.system_id}")
            if machine.ip_addresses:
                print(f"    IPs: {[ip.ip for ip in machine.ip_addresses]}")
            else:
                print("    No IP addresses found.")
    else:
        print("No machines found in MAAS.")

    # Example: Get a specific machine (uncomment and replace with a known system_id)
    # machine_id = "your_machine_system_id" 
    # if machine_id != "your_machine_system_id":
    #     try:
    #         specific_machine = maas.machines.get(system_id=machine_id)
    #         print(f"\nDetails for machine {machine_id}: {specific_machine.hostname}, {specific_machine.status}")
    #     except Exception as e:
    #         print(f"\nCould not fetch machine {machine_id}: {e}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →