# Configuration

> Describe database and infrastructure as code using declarative Configuration
---

**Pigsty treats Infra & Database as Code.** You can describe the infrastructure & database clusters through a declarative interface. All your essential work is to describe your need in the [inventory](#inventory), then materialize it with a simple idempotent playbook.

----------------

## Inventory

Each pigsty deployment has a corresponding config **inventory**. It could be stored in a local git-managed file in [YAML](https://docs.ansible.com/ansible/2.9/user_guide/playbooks_variables.html) format or dynamically generated from [CMDB](https://docs.ansible.com/ansible/2.9/user_guide/intro_dynamic_inventory.html) or any ansible compatible format. Pigsty uses a monolith YAML config file as the default config inventory, which is [`pigsty.yml`](https://github.com/Vonng/pigsty/blob/v2.7.0/pigsty.yml),  [located](https://github.com/Vonng/pigsty/blob/v2.7.0/ansible.cfg#L3) in the pigsty home directory.

The inventory consists of two parts: **global vars** & multiple **group definitions**. You can define new clusters with inventory groups: `all.children`. And describe infra and set global default parameters for clusters with global vars: `all.vars`. Which may look like this:

```yaml
all:                  # Top-level object: all
  vars: {...}         # Global Parameters
  children:           # Group Definitions
    infra:            # Group Definition: 'infra'
      hosts: {...}        # Group Membership: 'infra'
      vars:  {...}        # Group Parameters: 'infra'
    etcd:    {...}    # Group Definition: 'etcd'
    pg-meta: {...}    # Group Definition: 'pg-meta'
    pg-test: {...}    # Group Definition: 'pg-test'
    redis-test: {...} # Group Definition: 'redis-test'
    # ...
```

There are lots of config examples under [`files/pigsty`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/README.md)


----------------

## Cluster

Each group may represent a cluster, which could be a Node cluster, PostgreSQL cluster, Redis cluster, Etcd cluster, or Minio cluster, etc... They all use the same format: **group vars** & **hosts**. You can define cluster members with `all.children.<cls>.hosts` and describe cluster with cluster parameters in `all.children.<cls>.vars`. Here is an example of 3 nodes PostgreSQL HA cluster named `pg-test`:

```yaml
pg-test:   # Group Name
  vars:    # Group Vars (Cluster Parameters)
    pg_cluster: pg-test
  hosts:   # Group Host (Cluster Membership)
    10.10.10.11: { pg_seq: 1, pg_role: primary } # Host1
    10.10.10.12: { pg_seq: 2, pg_role: replica } # Host2
    10.10.10.13: { pg_seq: 3, pg_role: offline } # Host3
```

You can also define parameters for a specific host, as known as **host vars**. It will override group vars and global vars. Which is usually used for assigning identities to nodes & database instances.



----------------

## Parameter

Global vars, Group vars, and Host vars are dict objects consisting of a series of K-V pairs. Each pair is a named **Parameter** consisting of a string name as the key and a value of one of five types:  boolean, string, number, array, or object. Check parameter reference for detailed syntax & semantics.

Every parameter has a proper default value except for mandatory **IDENTITY PARAMETERS**; they are used as identifiers and must be set explicitly, such as [`pg_cluster`](/docs/reference/param/#pg_cluster), [`pg_role`](/docs/reference/param/#pg_role), and [`pg_seq`](/docs/reference/param/#pg_seq).

Parameters can be specified & overridden with the following precedence.

```bash
Playbook Args  >  Host Vars  >  Group Vars  >  Global Vars  >  Defaults
```

For examples:

* Force removing existing databases with Playbook CLI Args `-e pg_clean=true`
* Override an instance role with Instance Level Parameter `pg_role` on Host Vars
* Override a cluster name with Cluster Level Parameter `pg_cluster` on Group Vars.
* Specify global NTP servers with Global Parameter `node_ntp_servers` on Global Vars
* If no `pg_version` is set, it will use the default value from role implementation (16 by default)






----------------

## Template

There are numerous preset config templates for different scenarios under the [`files/pigsty`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/README.md) directory.

During [`configure`](/docs/setup/install#configure)  process, you can specify a template using the -m parameter.
Otherwise, the single-node installation config template will be automatically selected based on your OS distribution.

- EL 8 / 9: [`el8.yml`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/el8.yml) / [`el9.yml`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/el9.yml)
- Debian 12: [`debian12.yml`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/debian12.yml)
- Ubuntu 22.04 jammy: [`ubuntu22.yml`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/ubuntu22.yml)

Although the Pigsty no longer officially supports these OS distros, you can still use the following templates for older major OS versions to perform online installation:

- EL 7：[`el7.yml`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/el7.yml)
- Debian 11 bullseye: [`debian11.yml`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/debian11.yml)
- Ubuntu 20.04 focal：[`ubuntu20.yml`](https://github.com/Vonng/pigsty/blob/v2.7.0/files/pigsty/ubuntu20.yml)



----------------

## Switch Config Inventory

To use a different config inventory, you can copy & paste the content into the `pigsty.yml` file in the home dir as needed.

You can also explicitly specify the config inventory file to use when executing Ansible playbooks by using the `-i` command-line parameter, for example:

```bash
./node.yml -i files/pigsty/rpmbuild.yml    # use another file as config inventory, rather than the default pigsty.yml
```

If you want to modify the **default** config inventory filename, you can change the `inventory` parameter in the [`ansible.cfg`](https://github.com/Vonng/pigsty/blob/v2.7.0/ansible.cfg#L6) file in the home dir to point to your own inventory file path.
This allows you to run the `ansible-playbook` command without explicitly specifying the `-i` parameter.

Pigsty allows you to use a database (CMDB) as a dynamic configuration source instead of a static configuration file. Pigsty provides three convenient scripts:

- [`bin/inventory_load`](https://github.com/Vonng/pigsty/blob/v2.7.0/bin/inventory_load): Loads the content of the `pigsty.yml` into the local PostgreSQL database (`meta`.`pigsty`)
- [`bin/inventory_cmdb`](https://github.com/Vonng/pigsty/blob/v2.7.0/bin/inventory_cmdb): Switches the configuration source to the local PostgreSQL database (`meta`.`pigsty`)
- [`bin/inventory_conf`](https://github.com/Vonng/pigsty/blob/v2.7.0/bin/inventory_cmdb): Switches the configuration source to the local static configuration file `pigsty.yml`


----------------

## Reference

Pigsty have 280+ parameters, check [Parameter](/docs/reference/param/) for details.

|                  Module                   | Section                                                 | Description                      | Count |
|:-----------------------------------------:|---------------------------------------------------------|----------------------------------|-------|
|  [`INFRA`](/docs/reference/param/#infra)  | [`META`](/docs/reference/param/#meta)                   | Pigsty Metadata                  | 4     |
|  [`INFRA`](/docs/reference/param/#infra)  | [`CA`](/docs/reference/param/#ca)                       | Self-Signed CA                   | 3     |
|  [`INFRA`](/docs/reference/param/#infra)  | [`INFRA_ID`](/docs/reference/param/#infra_id)           | Infra Portals & Identity         | 2     |
|  [`INFRA`](/docs/reference/param/#infra)  | [`REPO`](/docs/reference/param/#repo)                   | Local Software Repo              | 9     |
|  [`INFRA`](/docs/reference/param/#infra)  | [`INFRA_PACKAGE`](/docs/reference/param/#infra_package) | Infra Packages                   | 2     |
|  [`INFRA`](/docs/reference/param/#infra)  | [`NGINX`](/docs/reference/param/#nginx)                 | Nginx Web Server                 | 7     |
|  [`INFRA`](/docs/reference/param/#infra)  | [`DNS`](/docs/reference/param/#dns)                     | DNSMASQ Nameserver               | 3     |
|  [`INFRA`](/docs/reference/param/#infra)  | [`PROMETHEUS`](/docs/reference/param/#prometheus)       | Prometheus Stack                 | 18    |
|  [`INFRA`](/docs/reference/param/#infra)  | [`GRAFANA`](/docs/reference/param/#grafana)             | Grafana Stack                    | 6     |
|  [`INFRA`](/docs/reference/param/#infra)  | [`LOKI`](/docs/reference/param/#loki)                   | Loki Logging Service             | 4     |
|   [`NODE`](/docs/reference/param/#node)   | [`NODE_ID`](/docs/reference/param/#node_id)             | Node Identity Parameters         | 5     |
|   [`NODE`](/docs/reference/param/#node)   | [`NODE_DNS`](/docs/reference/param/#node_dns)           | Node domain names & resolver     | 6     |
|   [`NODE`](/docs/reference/param/#node)   | [`NODE_PACKAGE`](/docs/reference/param/#node_package)   | Node Repo & Packages             | 5     |
|   [`NODE`](/docs/reference/param/#node)   | [`NODE_TUNE`](/docs/reference/param/#node_tune)         | Node Tuning & Kernel features    | 10    |
|   [`NODE`](/docs/reference/param/#node)   | [`NODE_ADMIN`](/docs/reference/param/#node_admin)       | Admin User & Credentials         | 7     |
|   [`NODE`](/docs/reference/param/#node)   | [`NODE_TIME`](/docs/reference/param/#node_time)         | Node Timezone, NTP, Crontabs     | 5     |
|   [`NODE`](/docs/reference/param/#node)   | [`NODE_VIP`](/docs/reference/param/#node_vip)           | Node Keepalived L2 VIP           | 8     |
|   [`NODE`](/docs/reference/param/#node)   | [`HAPROXY`](/docs/reference/param/#haproxy)             | HAProxy the load balancer        | 10    |
|   [`NODE`](/docs/reference/param/#node)   | [`NODE_EXPORTER`](/docs/reference/param/#node_exporter) | Node Monitoring Agent            | 3     |
|   [`NODE`](/docs/reference/param/#node)   | [`PROMTAIL`](/docs/reference/param/#promtail)           | Promtail logging Agent           | 4     |
| [`DOCKER`](/docs/reference/param/#docker) | [`DOCKER`](/docs/reference/param/#docker)               | Docker Daemon                    | 4     |
|   [`ETCD`](/docs/reference/param/#etcd)   | [`ETCD`](/docs/reference/param/#etcd)                   | ETCD DCS Cluster                 | 10    |
|  [`MINIO`](/docs/reference/param/#minio)  | [`MINIO`](/docs/reference/param/#minio)                 | MINIO S3 Object Storage          | 15    |
|  [`REDIS`](/docs/reference/param/#redis)  | [`REDIS`](/docs/reference/param/#redis)                 | Redis the key-value NoSQL cache  | 20    |
|  [`PGSQL`](/docs/reference/param/#pgsql)  | [`PG_ID`](/docs/reference/param/#pg_id)                 | PG Identity Parameters           | 11    |
|  [`PGSQL`](/docs/reference/param/#pgsql)  | [`PG_BUSINESS`](/docs/reference/param/#pg_business)     | PG Business Object Definition    | 12    |
|  [`PGSQL`](/docs/reference/param/#pgsql)  | [`PG_INSTALL`](/docs/reference/param/#pg_install)       | Install PG Packages & Extensions | 10    |
|  [`PGSQL`](/docs/reference/param/#pgsql)  | [`PG_BOOTSTRAP`](/docs/reference/param/#pg_bootstrap)   | Init HA PG Cluster with Patroni  | 39    |
|  [`PGSQL`](/docs/reference/param/#pgsql)  | [`PG_PROVISION`](/docs/reference/param/#pg_provision)   | Create in-database objects       | 9     |
|  [`PGSQL`](/docs/reference/param/#pgsql)  | [`PG_BACKUP`](/docs/reference/param/#pg_backup)         | Set Backup Repo with pgBackRest  | 5     |
|  [`PGSQL`](/docs/reference/param/#pgsql)  | [`PG_SERVICE`](/docs/reference/param/#pg_service)       | Exposing service, bind vip, dns  | 9     |
|  [`PGSQL`](/docs/reference/param/#pgsql)  | [`PG_EXPORTER`](/docs/reference/param/#pg_exporter)     | PG Monitor agent for Prometheus  | 15    |
