InfraHub Schema Library
In my previous InfraHub introductory post, we covered installation and the basics of InfraHub. In this second post, let’s explore the ‘Schema Library’ provided by OpsMill, the team behind InfraHub. As mentioned in the previous post, InfraHub doesn’t include any user-defined schemas out of the box, so we need to create our own. However, the Schema Library repository offers a collection of schemas that we can easily import into InfraHub. In this post, we’ll take a closer look at the Schema Library and how to use it.
If you are new to Infrahub and want to learn the basics of what it is and how to install it, feel free to check out my introductory post below.
Recap on Schema
The way I think about schema is that it is a blueprint that defines the structure of your data. It specifies the nodes (like devices and interfaces), their attributes, and the relationships between them. This allows you to customize how you organize and manage your inventory in Infrahub.
When you install InfraHub for the first time, you won’t find any user-defined schemas available to add to your inventory. Out of the box, I can only see IPAM and nothing else.
Schema Library
Instead of reinventing the wheel by creating your own schema, you can simply import the schemas you need into InfraHub. First, clone the schema-library
repository to your InfraHub server instance using the following command.
git clone https://github.com/opsmill/schema-library.git
From here, you have two options. You can either import the schema directly into your main branch or create a new branch, import the schema, verify everything works as expected, and then merge it into the main branch. The recommended approach is the latter, creating a new branch. In fact, InfraHub suggests creating a new branch every time you make changes (schema or even the data stored in Infrahub).
So, let's first look at how to create a new branch. You can create a new branch either on the CLI or directly via the web GUI. The way you interact with InfraHub on the CLI is through infrahubctl
. If you're not familiar with infrahubctl
or how to install it, feel free to check out my introductory post here. However, here is the command to install it.
pip install 'infrahub-sdk[ctl]'
Once installed, you can create a new branch as shown below. I you prefer, you can also create a new branch from the web GUI.
infrahubctl branch create base-schema
Branch 'base-schema' created successfully (18178e09-be9a-36fa-2d12-c51ef598f078).
Next, use infrahubctl
to import the schema into Infrahub, here you can specify the branch you want to use.
infrahubctl schema load --branch base-schema schema-library/base/
infrahubctl schema load --branch base-schema schema-library/extensions/vlan/
infrahubctl schema load --branch base-schema schema-library/extensions/location_minimal/
You will immediately notice that various options now appear in the InfraHub GUI. You can start adding your network devices, VLANs, interfaces, and more into InfraHub, as shown below.
Proposed Changes
But remember, the new schema is still in the new branch. If you switch back to your main branch, you won't see the changes. Once you're satisfied with the schema, you can either merge it directly or create a 'Proposed Change' to have someone review and approve it.
When you create a proposed change, both you and the reviewer can see the modifications. After the review and approval, you can merge it into the main branch.
After you merge, the new schema becomes part of your main branch, and you can start adding your devices.
More Schema and Closing Up
Before we wrap up, I’ll import a few more schemas into a new branch. Since I’m working on a branch, if I’m not satisfied with the new schemas, I can simply discard the branch instead of manually deleting each schema I imported.
infrahubctl schema load --branch compute schema-library/extensions/compute/
infrahubctl schema load --branch compute schema-library/extensions/patch_panel/
infrahubctl schema load --branch compute schema-library/extensions/cable
infrahubctl schema load --branch compute schema-library/extensions/circuit/
I hope this blog post gives you a clear idea of how schemas work. Remember, you can always modify schemas to suit your needs. The schema file is just a YAML file, and with a few hours of practice, you’ll start to understand how the schema is defined. I’ll try to cover schemas in more detail in upcoming posts.