Python
Python Automation
Scripts and examples using the ONTAP REST API, netapp-ontap Python library, and Ansible for storage automation.
01 Installation & Setup
Install the official NetApp ONTAP Python client library and dependencies. Requires Python 3.8+.
bash
# Install NetApp ONTAP Python client
pip install netapp-ontap
# Also useful
pip install requests urllib3 pandas
02 Connect to ONTAP
Establish a connection to the ONTAP cluster using the HostConnection context manager. Always use environment variables or a secrets manager for credentials โ never hardcode them.
python
import os
from netapp_ontap import HostConnection, NetAppRestError
from netapp_ontap.resources import Volume
cluster_ip = os.getenv("ONTAP_HOST")
username = os.getenv("ONTAP_USER", "admin")
password = os.getenv("ONTAP_PASS")
with HostConnection(
cluster_ip,
username=username,
password=password,
verify=False # use True with valid cert in prod
):
# All API calls inside this block use this connection
for vol in Volume.get_collection():
print(vol.name)
03 Volume Operations
List volumes, create a new FlexVol, and modify volume properties. All size values are in bytes.
python ยท List Volumes
from netapp_ontap.resources import Volume
for vol in Volume.get_collection(
fields="name,svm,size,state,space"
):
print(
f"Name: {vol.name}",
f"SVM: {vol.svm.name}",
f"Size: {vol.size // (1024**3)} GB",
f"State: {vol.state}"
)
python ยท Create Volume
from netapp_ontap.resources import Volume
from netapp_ontap import NetAppRestError
vol = Volume.from_dict({
"name": "my_new_vol",
"svm": {"name": "svm0"},
"aggregates": [{"name": "aggr1"}],
"size": 10 * 1024 ** 3, # 10 GB in bytes
"space.snapshot.reserve_percent": 5,
"nas": {"path": "/my_new_vol"},
})
try:
vol.post(hydrate=True)
print(f"Volume created: {vol.name} (UUID: {vol.uuid})")
except NetAppRestError as err:
print(f"Error: {err}")
04 Snapshot Management
python ยท Create & List Snapshots
from netapp_ontap.resources import Snapshot
vol_uuid = "your-volume-uuid-here"
# Create a snapshot
snap = Snapshot(vol_uuid)
snap.name = "my_snap_20240801"
snap.post(hydrate=True)
print(f"Snapshot created: {snap.name}")
# List all snapshots for a volume
for snap in Snapshot.get_collection(vol_uuid):
print(f" {snap.name} created: {snap.create_time}")
# Delete oldest snapshot
old_snap = Snapshot(vol_uuid, name="old_snap_20240101")
old_snap.delete()
05 Direct REST API with requests
Sometimes you want raw HTTP control โ useful for endpoints not yet wrapped by the SDK, or for quick scripting.
python
import requests
import urllib3
urllib3.disable_warnings()
BASE = "https://192.168.1.100/api"
AUTH = ("admin", "password")
HDRS = {"Content-Type": "application/json"}
# List SVMs
r = requests.get(
f"{BASE}/svm/svms",
auth=AUTH, headers=HDRS, verify=False,
params={"fields": "name,state,uuid"}
)
r.raise_for_status()
for svm in r.json()["records"]:
print(f"SVM: {svm['name']} โ {svm['state']}")
# Handle pagination
next_link = r.json().get("_links", {}).get("next", {}).get("href")
while next_link:
r = requests.get(
f"https://192.168.1.100{next_link}",
auth=AUTH, headers=HDRS, verify=False
)
next_link = r.json().get("_links", {}).get("next", {}).get("href")
06 Aggregate Capacity Report
python
from netapp_ontap.resources import Aggregate
print(f"{'Aggregate':<25} {'Total GB':>10} {'Used GB':>10} {'Used %':>8}")
print("-" * 55)
for aggr in Aggregate.get_collection(
fields="name,space"
):
space = aggr.space.block_storage
total_gb = space.size / 1024**3
used_gb = space.used / 1024**3
pct = (space.used / space.size) * 100
print(f"{aggr.name:<25} {total_gb:>10.1f} {used_gb:>10.1f} {pct:>7.1f}%")