OpenStack Command-line Client
OpenStackClient (OSC) is a command-line client for OpenStack that unifies the command sets for core services like Compute, Identity, Image, Network, Object Store, and Block Storage into a single shell with a uniform structure. It is currently at version 9.0.0 and aligns its release cadence with the broader OpenStack project releases, offering regular updates and support for new features.
Warnings
- breaking Python 2.x is no longer supported. `python-openstackclient` 9.0.0 requires Python >=3.10. Older versions of the client (especially pre-3.x) might have exhibited syntax errors if run with Python 2.
- gotcha There are two PyPI packages: `python-openstackclient` (the core client) and `openstackclient` (a metapackage that installs `python-openstackclient` and common plugins). The latter is often recommended for a more complete experience.
- deprecated Legacy project-specific command-line clients (e.g., `nova`, `glance`, `cinder` CLIs) are being actively superseded by `python-openstackclient` for a unified experience. Some advanced or less common features might still exist only in the legacy clients but are being migrated.
- gotcha Installing Python packages directly into the system's Python environment using `pip install` without a virtual environment can lead to conflicts and break system tools.
- gotcha Authentication can be configured via environment variables (e.g., sourcing an `openrc` file), a `clouds.yaml` file, or command-line options. Inconsistent or missing authentication details are a common source of errors.
Install
-
python3 -m pip install python-openstackclient -
python3 -m pip install openstackclient
Imports
- openstack
import subprocess # ... or use openstacksdk for direct Python API interaction
Quickstart
import os
import subprocess
# --- Configuration (choose one method) ---
# Method 1: Environment Variables (recommended for scripts)
os.environ['OS_AUTH_URL'] = os.environ.get('OS_AUTH_URL', 'http://<your-keystone-url>/v3')
os.environ['OS_USERNAME'] = os.environ.get('OS_USERNAME', 'admin')
os.environ['OS_PASSWORD'] = os.environ.get('OS_PASSWORD', 'your_password')
os.environ['OS_PROJECT_NAME'] = os.environ.get('OS_PROJECT_NAME', 'admin')
os.environ['OS_USER_DOMAIN_NAME'] = os.environ.get('OS_USER_DOMAIN_NAME', 'Default')
os.environ['OS_PROJECT_DOMAIN_NAME'] = os.environ.get('OS_PROJECT_DOMAIN_NAME', 'Default')
os.environ['OS_IDENTITY_API_VERSION'] = os.environ.get('OS_IDENTITY_API_VERSION', '3')
# Method 2: clouds.yaml (often found in ~/.config/openstack/clouds.yaml)
# Ensure your clouds.yaml is configured and replace 'my-cloud' with your cloud name
# If using clouds.yaml, you might not need to set environment variables.
# Example clouds.yaml content (place in ~/.config/openstack/clouds.yaml or specify via OS_CLIENT_CONFIG_FILE):
# clouds:
# my-cloud:
# auth:
# auth_url: 'http://<your-keystone-url>/v3'
# username: 'admin'
# password: 'your_password'
# project_name: 'admin'
# user_domain_name: 'Default'
# project_domain_name: 'Default'
# region_name: 'RegionOne'
# Example: List OpenStack projects using the CLI
try:
print("\n--- Listing OpenStack Projects ---")
# Use 'openstack --os-cloud my-cloud project list' if using clouds.yaml
result = subprocess.run(['openstack', 'project', 'list'], capture_output=True, text=True, check=True)
print(result.stdout)
if result.stderr:
print("Stderr:\n", result.stderr)
print("\n--- Listing OpenStack Services ---")
result = subprocess.run(['openstack', 'service', 'list'], capture_output=True, text=True, check=True)
print(result.stdout)
if result.stderr:
print("Stderr:\n", result.stderr)
except subprocess.CalledProcessError as e:
print(f"Error executing OpenStack CLI command: {e.cmd}")
print(f"Return Code: {e.returncode}")
print(f"Stdout:\n{e.stdout}")
print(f"Stderr:\n{e.stderr}")
except FileNotFoundError:
print("Error: 'openstack' command not found. Is OpenStackClient installed and in your PATH?")