PIGSTY

CMDB

Use PostgreSQL as config inventory

Pigsty allows you to use a database (CMDB) as a dynamic configuration source instead of a static configuration file. You can use the built-in PostgreSQL as config inventory for config management.

With Postgres CMDB, configuration is organized in structured relational tables, which can be easily queried and manipulated using SQL. This allows for easier integration with other systems and tools.


How does it work?

Ansible allows you to use a dynamic inventory script to generate the inventory config on-the-fly.

The idea is to replace static pigsty.yml in ansible.cfg with a dynamic shell script inventory.sh

~/pigsty/ansible.cfg
---
inventory = pigsty.yml
+++
inventory = inventory.sh

The content of inventory.sh is very simple, it will query the PostgreSQL CMDB and retrieve config.

~/pigsty/inventory.sh
psql ${METADB_URL} -AXtwc 'SELECT text FROM pigsty.inventory;'

Util Scripts for CMDB


CMDB Schema

The CMDB baseline schema is shipped with pigsty: files/cmdb.sql And most of the default config templates will use it as example baseline. Which means it can be used by default.

all:
  children:
    pg-meta:
      hosts:
        10.10.10.10: { pg_seq: 1, pg_role: primary }
      vars:
        pg_cluster: pg-meta
        pg_databases:
          - name: meta
            baseline: cmdb.sql  # <--- use this as database schema baseline

Load Config Data

CMDB is empty by default, load config file into the CMDB with the bin/inventory_load script.

Run bin/inventory_load without arguments will load the default pigsty.yml into the default CMDB.

usage: inventory_load [-h] [-p PATH] [-d CMDB_URL]

load config arguments

optional arguments:
  -h, --help            show this help message and exit
  -p PATH, --path PATH  config path, ${PIGSTY_HOME}/pigsty.yml by default
  -d DATA, --data DATA  postgres cmdb pgurl, ${METADB_URL} by default

Use -p to specify the config file path, and -d to specify the CMDB URL.

bin/inventory_load
bin/inventory_load -p conf/demo.yml
bin/inventory_load -p conf/prod.yml -d postgresql://dbuser_meta:DBUser.Meta@10.10.10.10:5432/meta

Switch Inventory

You can switch to dynamic CMDB inventory with:

bin/inventory_cmdb

Which essentially changes the inventory parameter in the ansible.cfg to use the inventory.sh script.