Getting started with OpenStack Oslo Config (oslo.config)

Post to Twitter

The Oslo configuration API supports parsing command line arguments and .ini style configuration files. Its probably safe to say that all OpenStack projects use oslo.config. Make sure to also check out the oslo.config documentation on the OpenStack documentation website.

Note: By the way, if you know of a better way to do this or see something flat out wrong please leave a comment or send me an email. As I mentioned prior, there is not a lot of documentation on this topic so I’m assuming I got the pieces, etc. correct.

I’m going to create a simple project to demonstrate one way you can use Oslo Config. I’ll be using Python 2.7.5 along with virtualenv on a Linux system. Let’s start by creating a simple project first:

$ virtualenv example-app
$ cd example-app
$ source bin/activate
$ pip install oslo.config
$ touch app.py
$ touch app.conf

That will setup the project under virtualenv, pip install oslo.config and create two files: app.py (where our code will go) and app.conf where we will put some simple configuration settings.

First, let’s add the settings to the app.conf file:

[simple]
enable = True

Here is the code for app.py:

from __future__ import print_function
from oslo.config import cfg


opt_group = cfg.OptGroup(name='simple',
                         title='A Simple Example')

simple_opts = [
    cfg.BoolOpt('enable', default=False,
                help=('True enables, False disables'))
]

CONF = cfg.CONF
CONF.register_group(opt_group)
CONF.register_opts(simple_opts, opt_group)


if __name__ == "__main__":
    CONF(default_config_files=['app.conf'])
    print(CONF.simple.enable)

You can see we first set the opt_group variable and then set the simple_opts list to hold the options in the simple section of the config file, in this case we only have the one setting: enable. The group is then registered as are the options. I then point to the configuration file app.conf (keep in mind you can point to multiple configuration files, your not limited to one).

For multiple configuration files I could do something like this:

CONF(default_config_files=['app.conf', 'mything.conf'])

Realistically you’ll probably have something more similar to:

CONF(default_config_files=['/etc/example-app/app.conf'])

Go ahead and run the program. You should see True printed out.

Edit the following in the app.conf:

enable = False

Run the program again and you should see False printed.

Let’s take this a step further now and modify the app.conf file as such:

[simple]

enable = True

[morestuff]

# StrOpt
message = Hello World

# ListOpt
usernames = ['Joe', 'Jessica', 'Peter']

# DictOpt
usermetadata = {'Joe': 'Manager', 'Jessica': 'CEO', 'Peter': 'Security Guard'}

# IntOpt
payday = 20

# FloatOpt
pi = 3.14

Some trivial data to be sure, but, we add in some more options for configuration values: StrOpt, ListOpt, DictOpt, IntOp, FloatOpt

Modify the app.py like so to fetch the new values:

from __future__ import print_function
from oslo.config import cfg


opt_simple_group = cfg.OptGroup(name='simple',
                         title='A Simple Example')

opt_morestuff_group = cfg.OptGroup(name='morestuff',
                         title='A More Complex Example')

simple_opts = [
    cfg.BoolOpt('enable', default=False,
                help=('True enables, False disables'))
]

morestuff_opts = [
    cfg.StrOpt('message', default='No data',
               help=('A message')),
    cfg.ListOpt('usernames', default=None,
                help=('A list of usernames')),
    cfg.DictOpt('jobtitles', default=None,
                help=('A dictionary of usernames and job titles')),
    cfg.IntOpt('payday', default=30,
                help=('Default payday monthly date')),
    cfg.FloatOpt('pi', default=0.0,
                help=('The value of Pi'))
]

CONF = cfg.CONF

CONF.register_group(opt_simple_group)
CONF.register_opts(simple_opts, opt_simple_group)

CONF.register_group(opt_morestuff_group)
CONF.register_opts(morestuff_opts, opt_morestuff_group)


if __name__ == "__main__":
    CONF(default_config_files=['app.conf'])
    print('(simple) enable: {}'.format(CONF.simple.enable))
    print('(morestuff) message :{}'.format(CONF.morestuff.message))
    print('(morestuff) usernames: {}'.format(CONF.morestuff.usernames))
    print('(morestuff) jobtitles: {}'.format(CONF.morestuff.jobtitles))
    print('(morestuff) payday: {}'.format(CONF.morestuff.payday))
    print('(morestuff) pi: {}'.format(CONF.morestuff.pi))

Now when you run the program you should see output like this:

(simple) enable: True
(morestuff) message :Hello World
(morestuff) usernames: ["['Joe'", "'Jessica'", "'Peter']“]
(morestuff) jobtitles: {“{‘Joe’”: “‘Manager’”, “‘Peter’”: “‘Security Guard’}”, “‘Jessica’”: “‘CEO’”}
(morestuff) payday: 20
(morestuff) pi: 3.14

There are a couple more option types you can use other than the ones I used above such as: MultiStrOpt, SubCommandOpt, etc. I don’t see these used as often but of course your mileage may vary.

Post to Twitter

This entry was posted in Open Source, OpenStack, Python. Bookmark the permalink.

2 Responses to Getting started with OpenStack Oslo Config (oslo.config)

  1. aelkikhia says:

    Much clearer than “The Oslo configuration API supports parsing command line arguments and .ini style configuration files.” (oslo.config’s read me at github)

  2. Loic says:

    I am not sure i understand why oslo.config exist, what about globals why do we need globals in my code for configuration?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>