# Playbook

> Run playbooks with ansible
---

Pigsty implements admin controllers with idempotent [Ansible playbooks](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_intro.html).
Playbooks require the `ansible-playbook` executable bin in your `PATH`. You'll have to [install ansible](/docs/admin/ansible#install) to run playbooks.

Here are built-in playbooks in Pigsty, you can also add your own.

|           Module           | Playbook                                                                               | Function                                                    |
|:--------------------------:|----------------------------------------------------------------------------------------|-------------------------------------------------------------|
|  [INFRA](/docs/infra/playbook)  | [`install.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/install.yml)                 | Install Pigsty on current node in one-pass                  |
|  [INFRA](/docs/infra/playbook)  | [`infra.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/infra.yml)                     | Init pigsty infrastructure on infra nodes                   |
|  [INFRA](/docs/infra/playbook)  | [`infra-rm.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/infra-rm.yml)               | Remove infrastructure components from infra nodes           |
|  [INFRA](/docs/infra/playbook)  | [`cache.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/cache.yml)                     | Make offline install packages from target node              |
|  [INFRA](/docs/infra/playbook)  | [`cert.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/cert.yml)                       | Issue cert with pigsty self-signed CA (e.g. for pg clients) |
|   [NODE](/docs/node/playbook)   | [`node.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/node.yml)                       | Init node for pigsty, tune node into desired status         |
|   [NODE](/docs/node/playbook)   | [`node-rm.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/node-rm.yml)                 | Remove node from pigsty                                     |
|  [PGSQL](/docs/pgsql/playbook)  | [`pgsql.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/pgsql.yml)                     | Init HA PostgreSQL clusters, or adding new replicas         |
|  [PGSQL](/docs/pgsql/playbook)  | [`pgsql-rm.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/pgsql-rm.yml)               | Remove PostgreSQL cluster, or remove replicas               |
|  [PGSQL](/docs/pgsql/playbook)  | [`pgsql-db.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/pgsql-db.yml)               | Add new business database to existing PostgreSQL cluster    |
|  [PGSQL](/docs/pgsql/playbook)  | [`pgsql-user.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/pgsql-user.yml)           | Add new business user to existing PostgreSQL cluster        |
|  [PGSQL](/docs/pgsql/playbook)  | [`pgsql-pitr.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/pgsql-pitr.yml)           | Run point-in-time-recovery on existing PostgreSQL cluster   |
|  [PGSQL](/docs/pgsql/playbook)  | [`pgsql-monitor.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/pgsql-monitor.yml)     | Monitor remote postgres instance with local exporters       |
|  [PGSQL](/docs/pgsql/playbook)  | [`pgsql-migration.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/pgsql-migration.yml) | Generate Migration manual & scripts for existing PostgreSQL |
|  [PGSQL](/docs/pgsql/playbook)  | [`slim.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/slim.yml)                       | Install Pigsty with minimal components                      |
|  [REDIS](/docs/redis/playbook)  | [`redis.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/redis.yml)                     | Init redis cluster/node/instance                            |
|  [REDIS](/docs/redis/playbook)  | [`redis-rm.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/redis-rm.yml)               | Remove redis cluster/node/instance                          |
|   [ETCD](/docs/etcd/playbook)   | [`etcd.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/etcd.yml)                       | Init etcd cluster, or append new member                     |
|   [ETCD](/docs/etcd/playbook)   | [`etcd-rm.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/etcd-rm.yml)                 | Remove etcd cluster, or remove existing member              |
|  [MINIO](/docs/minio/playbook)  | [`minio.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/minio.yml)                     | Init minio cluster                                          |
|  [MINIO](/docs/minio/playbook)  | [`minio-rm.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/minio-rm.yml)               | Remove minio cluster                                        |
| [DOCKER](/docs/docker/playbook) | [`docker.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/docker.yml)                   | Install docker on nodes                                     |
| [DOCKER](/docs/docker/playbook) | [`app.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/app.yml)                         | Install application with docker compose                     |
|     [FERRET](/docs/ferret/)     | [`mongo.yml`](https://github.com/pgsty/pigsty/blob/v3.7.0/mongo.yml)                     | Install Mongo/FerretDB on nodes                             |


--------

## Deployment

The special playbook `install.yml` will deploy everything with ad hoc playbooks:

| Playbook    | Command       | Group      | `infra` | `[nodes]` | `etcd` | `minio` | `[pgsql]` |
|-------------|---------------|------------|:-------:|:---------:|:------:|:-------:|:---------:|
| `infra.yml` | `./infra.yml` | `-l infra` |    ✓    |     ✓     |        |         |           |
| `node.yml`  | `./node.yml`  |            |         |     ✓     |   ✓    |    ✓    |     ✓     |
| `etcd.yml`  | `./etcd.yml`  | `-l etcd`  |         |           |   ✓    |         |           |
| `minio.yml` | `./minio.yml` | `-l minio` |         |           |        |    ✓    |           |
| `pgsql.yml` | `./pgsql.yml` |            |         |           |        |         |     ✓     |

**Circular Dependency Between NODE and INFRA**

There is a weak circular dependency between [**NODE**](/docs/node/) and [**INFRA**](/docs/infra/): to register a NODE
to INFRA, the INFRA should already exist, while the INFRA module relies on NODE to work.

One way to work around it is to init `infra` nodes first, then add other nodes, if you wish to deploy them all in
one-pass, `install.yml` is the way to go.




--------

## Safety Considerations

**Idempotent but Potentially Destructive**

Most playbooks are idempotent, meaning that some deployment playbooks may **erase existing databases** and create
new ones without the protection option turned on. Especially care with `pgsql`, `minio`, and `infra` playbooks.

Please read the documentation carefully and operate with caution.
The author is not responsible for any loss of databases due to misuse.

**Safety Best Practices**

1. **Read playbook documentation carefully** before execution
2. **Ctrl-C to stop immediately** if you see something wrong
3. **Start with non-production environments** for testing
4. **Limit execution hosts** (`-l`) to avoid unintended hosts if applicable
5. **Use specific tags** (`-t`) to run subset of tasks if possible

**Dry Run Mode**

```bash
# Preview what would be changed without actually executing
./pgsql.yml -l pg-test --check --diff

# Combine with tags to check specific tasks
./pgsql.yml -l pg-test -t pg_config --check --diff
```
