Use CMDB as Config Inventory
Pigsty allows you to use a PostgreSQL metabase as a dynamic configuration source, replacing static YAML configuration files for more powerful configuration management capabilities.
Overview
CMDB (Configuration Management Database) is a method of storing configuration information in a database for management.
In Pigsty, the default configuration source is a static YAML file pigsty.yml,
which serves as Ansible’s inventory.
This approach is simple and direct, but when infrastructure scales and requires complex, fine-grained management and external integration, a single static file becomes insufficient.
| Feature | Static YAML File | CMDB Metabase |
|---|---|---|
| Querying | Manual search/grep | SQL queries with any conditions, aggregation analysis |
| Versioning | Depends on Git or manual backup | Database transactions, audit logs, time-travel snapshots |
| Access Control | File system permissions, coarse-grained | PostgreSQL fine-grained access control |
| Concurrent Editing | Requires file locking or merge conflicts | Database transactions naturally support concurrency |
| External Integration | Requires YAML parsing | Standard SQL interface, easy integration with any language |
| Scalability | Difficult to maintain when file becomes too large | Scales to physical limits |
| Dynamic Generation | Static file, changes require manual application | Immediate effect, real-time configuration changes |
Pigsty provides the CMDB database schema in the sample database pg-meta.meta schema baseline definition.
How It Works
The core idea of CMDB is to replace the static configuration file with a dynamic script.
Ansible supports using executable scripts as inventory, as long as the script outputs inventory data in JSON format.
When you enable CMDB, Pigsty creates a dynamic inventory script named inventory.sh:
This script’s function is simple: every time Ansible needs to read the inventory, it queries configuration data from the PostgreSQL database’s pigsty.inventory view and returns it in JSON format.
The overall architecture is as follows:
flowchart LR
conf["bin/inventory_conf"]
tocmdb["bin/inventory_cmdb"]
load["bin/inventory_load"]
ansible["🚀 Ansible"]
subgraph static["📄 Static Config Mode"]
yml[("pigsty.yml")]
end
subgraph dynamic["🗄️ CMDB Dynamic Mode"]
sh["inventory.sh"]
cmdb[("PostgreSQL CMDB")]
end
conf -->|"switch"| yml
yml -->|"load config"| load
load -->|"write"| cmdb
tocmdb -->|"switch"| sh
sh --> cmdb
yml --> ansible
cmdb --> ansible
Data Model
The CMDB database schema is defined in files/cmdb.sql, with all objects in the pigsty schema.
Core Tables
| Table | Description | Primary Key |
|---|---|---|
pigsty.group |
Cluster/group definitions, corresponds to Ansible groups | cls |
pigsty.host |
Host definitions, belongs to a group | (cls, ip) |
pigsty.global_var |
Global variables, corresponds to all.vars |
key |
pigsty.group_var |
Group variables, corresponds to all.children.<cls>.vars |
(cls, key) |
pigsty.host_var |
Host variables, host-level variables | (cls, ip, key) |
pigsty.default_var |
Default variable definitions, stores parameter metadata | key |
pigsty.job |
Job records table, records executed tasks | id |
Table Structure Details
Cluster Table pigsty.group
Host Table pigsty.host
Global Variables Table pigsty.global_var
Group Variables Table pigsty.group_var
Host Variables Table pigsty.host_var
Core Views
CMDB provides a series of views for querying and displaying configuration data:
| View | Description |
|---|---|
pigsty.inventory |
Core view: Generates Ansible dynamic inventory JSON |
pigsty.raw_config |
Raw configuration in JSON format |
pigsty.global_config |
Global config view, merges defaults and global vars |
pigsty.group_config |
Group config view, includes host list and group vars |
pigsty.host_config |
Host config view, merges group and host-level vars |
pigsty.pg_cluster |
PostgreSQL cluster view |
pigsty.pg_instance |
PostgreSQL instance view |
pigsty.pg_database |
PostgreSQL database definition view |
pigsty.pg_users |
PostgreSQL user definition view |
pigsty.pg_service |
PostgreSQL service definition view |
pigsty.pg_hba |
PostgreSQL HBA rules view |
pigsty.pg_remote |
Remote PostgreSQL instance view |
pigsty.inventory is the core view that converts database configuration data to the JSON format required by Ansible:
Utility Scripts
Pigsty provides three convenience scripts for managing CMDB:
| Script | Function |
|---|---|
bin/inventory_load |
Load YAML configuration file into PostgreSQL database |
bin/inventory_cmdb |
Switch configuration source to CMDB (dynamic inventory script) |
bin/inventory_conf |
Switch configuration source to static config file pigsty.yml |
inventory_load
Parse and import YAML configuration file into CMDB:
The script performs the following operations:
- Clears existing data in the
pigstyschema - Parses the YAML configuration file
- Writes global variables to the
global_vartable - Writes cluster definitions to the
grouptable - Writes cluster variables to the
group_vartable - Writes host definitions to the
hosttable - Writes host variables to the
host_vartable
Environment Variables
PIGSTY_HOME: Pigsty installation directory, defaults to~/pigstyMETADB_URL: Database connection URL, defaults toservice=meta
inventory_cmdb
Switch Ansible to use CMDB as the configuration source:
The script performs the following operations:
- Creates dynamic inventory script
${PIGSTY_HOME}/inventory.sh - Modifies
ansible.cfgto setinventorytoinventory.sh
The generated inventory.sh contents:
inventory_conf
Switch back to using static YAML configuration file:
The script modifies ansible.cfg to set inventory back to pigsty.yml.
Usage Workflow
First-time CMDB Setup
- Initialize CMDB schema (usually done automatically during Pigsty installation):
- Load configuration to database:
- Switch to CMDB mode:
- Verify configuration:
Query Configuration
After enabling CMDB, you can flexibly query configuration using SQL:
Modify Configuration
You can modify configuration directly via SQL:
Changes take effect immediately without reloading or restarting any service.
Switch Back to Static Configuration
To switch back to static configuration file mode:
Advanced Usage
Export Configuration
Export CMDB configuration to YAML format:
Or use the ansible-inventory command:
Configuration Auditing
Track configuration changes using the mtime field:
Integration with External Systems
CMDB uses standard PostgreSQL, making it easy to integrate with other systems:
- Web Management Interface: Expose configuration data through REST API (e.g., PostgREST)
- CI/CD Pipelines: Read/write database directly in deployment scripts
- Monitoring & Alerting: Generate monitoring rules based on configuration data
- ITSM Systems: Sync with enterprise CMDB systems
Considerations
-
Data Consistency: After modifying configuration, you need to re-run the corresponding Ansible playbooks to apply changes to the actual environment
-
Backup: Configuration data in CMDB is critical, ensure regular backups
-
Permissions: Configure appropriate database access permissions for CMDB to avoid accidental modifications
-
Transactions: When making batch configuration changes, perform them within a transaction for rollback on errors
-
Connection Pooling: The
inventory.shscript creates a new connection on each execution; if Ansible runs frequently, consider using connection pooling
Summary
CMDB is Pigsty’s advanced configuration management solution, suitable for scenarios requiring large-scale cluster management, complex queries, external integration, or fine-grained access control. By storing configuration data in PostgreSQL, you can fully leverage the database’s powerful capabilities to manage infrastructure configuration.
| Feature | Description |
|---|---|
| Storage | PostgreSQL pigsty schema |
| Dynamic Inventory | inventory.sh script |
| Config Load | bin/inventory_load |
| Switch to CMDB | bin/inventory_cmdb |
| Switch to YAML | bin/inventory_conf |
| Core View | pigsty.inventory |