Backend access with Python

With your opsi server up and running you probably will spend some time adjusting everything for your needs and requirements. This post will give a short introduction into how you can use Python for accessing the data in your opsi backend.

Many system administrators tend to automate tasks they do on a regular basis via shell scripts. Accessing the data opsi offers is very easy with opsi-admin and you can combine this program with your shell scripting magic.

But there are some disadavantages in my eyes that make switching to a different programming language a desired goal. The first problem you suffer with opsi-admin is comparatively slow speed. This is because the Python interpreter needs some time to start, load all the required modules and then get a connection to the backend. opsi-admin does these steps every time you call it. The second problem I often face is that more sophisticated logic isn't very easy or straight forward to implement in bash.

One simple solution to that problem is to write your scripts with Python and make use of the OPSI package. That is the same package that most opsi-applications on your server use!

It's so easy that we will start right away to write our first script. For a fast start we will write a script that will be run on the opsi server itself because there is already everything installed that you need. So please connect to your opsi server and start an interactive Python prompt by typing "python" into your shell.

First we need to import the backend manager from the opsi library:

from OPSI.Backend.BackendManager import BackendManager

The BackendManager takes care to set up the backend access regardless of the backends chosen. This is universal - you will end up with a script that will work with all possible backends.

Now we set up an instance of the BackendManager.

backend = BackendManager()

We can now address our backend with the variable backend. That's really all you need!

We continue by requesting all hosts that are a client. A host could also be a depot or a configserver but we only want clients for this.

clients = backend.host_getObjects(type="OpsiClient")

Now we print a message for every client that misses a description. We want them all to be properly described, don't we?

for client in clients:
    if not client.description:
        print("Client {0} has no description!".format(client.id))

Please keep the indentation the way it is shown here because Python may refuse to work if this get's mixed up.

And we are done - this is really all we needed!

Here is the complete script for a better overview:

from OPSI.Backend.BackendManager import BackendManager

backend = BackendManager()

clients = backend.host_getObjects(type="OpsiClient")
for client in clients:
    if not client.description:
        print("Client {0} has no description!".format(client.id))

You can of course save this file, make it executable and run it again every time you need it. If you want to learn more about the methods the backend offers I'd suggest to dive right into the docs!