Oslo Configuration API

10.3.0 · active · verified Sat Apr 11

Oslo.config is a Python library from the OpenStack project that provides a robust API for parsing command-line arguments and .ini-style configuration files. It's a fundamental component for OpenStack services, offering a unified and flexible way to manage application settings. The library supports multiple configuration sources, including defaults, configuration files, environment variables, and command-line arguments, with a defined precedence order. The current version is 10.3.0, and it maintains an active release cadence tied to the OpenStack development cycle.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define configuration options, register them (both globally and within groups), and then parse values from a simulated configuration file and command-line arguments. It highlights the explicit parsing step required for oslo.config to load settings from external sources. The example also shows how to access the parsed configuration values.

import sys
from oslo_config import cfg
import os

# Define a configuration group and options
CONF = cfg.CONF
common_opts = [
    cfg.StrOpt('bind_host', default='0.0.0.0', help='IP address to listen on'),
    cfg.IntOpt('bind_port', default=8080, help='Port to listen on'),
    cfg.BoolOpt('debug', default=False, help='Enable debug logging')
]

# Register options
CONF.register_opts(common_opts, group='DEFAULT')

# You can also register options in a specific group
api_group = cfg.OptGroup(name='api', title='API Options')
CONF.register_group(api_group)
api_opts = [
    cfg.StrOpt('url', default='http://localhost:8080', help='Base URL for the API'),
    cfg.IntOpt('timeout', default=30, help='API request timeout in seconds')
]
CONF.register_opts(api_opts, group=api_group)

# Example config file content (save as 'app.conf')
# [DEFAULT]
# bind_host = 127.0.0.1
# debug = True
#
# [api]
# url = https://api.example.com

# Parse command line arguments and config files
# For testing, we simulate args and a config file
# In a real app, you'd use: CONF(sys.argv[1:], project='myproject', default_config_files=['/etc/myproject/app.conf'])

# Create a dummy config file for demonstration
config_file_path = 'app.conf'
with open(config_file_path, 'w') as f:
    f.write('[DEFAULT]\n')
    f.write('bind_host = 127.0.0.1\n')
    f.write('debug = True\n')
    f.write('\n[api]\n')
    f.write('url = https://api.example.com\n')

# Simulate CLI args (e.g., --debug=false --api-timeout=60)
sys_args = ['program_name', '--config-file', config_file_path, '--api-timeout', '60']

# oslo.config needs to be explicitly parsed. The first argument is the program name.
# We use os.environ.get for security (e.g., if a secret was passed as an env var)
CONF(args=sys_args[1:], project='myproject')

print(f"Bind Host: {CONF.bind_host}")
print(f"Bind Port: {CONF.bind_port}")
print(f"Debug Mode: {CONF.debug}")
print(f"API URL: {CONF.api.url}")
print(f"API Timeout: {CONF.api.timeout}")

# Clean up the dummy config file
os.remove(config_file_path)

view raw JSON →