Use CMDB as Config Inventory
Use a PostgreSQL metabase as a dynamic configuration source instead of static YAML files.
Overview
A CMDB (Configuration Management Database) stores configuration in database tables.
By default, Pigsty uses static pigsty.yml as the Ansible inventory.
A static file is simple. Larger deployments can need finer control, concurrent editing, and external integration beyond one file’s practical limits.
| 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 |
The sample pg-meta.meta database baseline defines the CMDB schema.
How It Works
CMDB replaces the static inventory with an executable script that returns JSON, as Ansible supports. Enabling CMDB creates inventory.sh:
Whenever Ansible reads the inventory, this script queries pigsty.inventory in PostgreSQL and returns JSON.
The architecture is:
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
These views expose 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 converts database configuration to Ansible inventory JSON:
Utility Scripts
Use these three scripts to manage the 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 a YAML configuration file and import it into the CMDB:
The script performs these 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 these operations:
- Creates dynamic inventory script
${PIGSTY_HOME}/inventory.sh - Modifies
ansible.cfgto setinventorytoinventory.sh
The generated inventory.sh contains:
inventory_conf
Switch back to a static YAML configuration file:
The script modifies ansible.cfg to set inventory back to pigsty.yml.
Usage Workflow
First-time CMDB Setup
- Initialize the CMDB schema. Pigsty installation usually does this automatically:
- Load configuration into the database:
- Switch to CMDB mode:
- Verify configuration:
Query Configuration
After you enable CMDB, use SQL to query the configuration:
Modify Configuration
Use SQL to change the configuration directly:
Configuration changes are immediately available. You do not need to reload or restart a service.
Switch Back to Static Configuration
Switch back to a static configuration file:
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. You can integrate it with these systems:
- Web Management Interface: Expose configuration data through a REST API, such as PostgREST.
- CI/CD Pipelines: Read and write database data directly from deployment scripts.
- Monitoring & Alerting: Generate monitoring rules from configuration data.
- ITSM Systems: Synchronize data with enterprise CMDB systems.
Considerations
Data Consistency: After you change configuration, run the corresponding Ansible playbooks again to apply it to the environment.
Backup: CMDB configuration data is critical. Back it up regularly.
Permissions: Set appropriate CMDB database access permissions to prevent accidental changes.
Transactions: Make batch configuration changes in a transaction. This permits rollback if an error occurs.
Connection Pooling:
inventory.shopens a new connection each time it runs. Consider connection pooling if Ansible runs frequently.
Summary
Use CMDB for large cluster deployments, complex queries, external integration, or fine-grained access control. PostgreSQL stores the configuration and provides database functions to manage it.
| 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 |
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)