Run admin commands with ansible
This is the multi-page printable view of this section. .
Administration
- 1: Ansible
- 2: Playbook
- 3: Nginx Portal
- 4: Local Repo
- 5: DNS Domain
- 6: SSL Certs
Built-in playbooks in Pigsty
Introduction to grafana dashboards
Introduction to prometheus & alertmanager
Nginx Portal for WebUI services
Manage local APT / YUM repository
Use local / public domain names
Use self-signed or real HTTPS certificates
PostgreSQL Cluster with HA, PITR, IaC, ACL, Monitoring, Pooling
Nginx, Repo, DNS, NTP, Prometheus and Grafana stack for Observability
Enroll nodes into the desired state and monitor it, and VIP, HAProxy
Reliable distributed consensus storage (DCS), empowering PGSQL HA
S3 compatible object storage, optional backup storage
High-performance in-memory cache, optional data structure server
1 - Ansible
Pigsty implements admin controllers with Ansible, which is an open source automation tool for managing large-scale infrastructure in an Infra-as-Code (IaC) manner. Widely used in the industry by operators.
Install
Pigsty will try its best to install ansible and its dependencies during bootstrap.
But you can always install it manually, it is available on most OS’s official repos and can be installed with one command.
Playbooks also require a weak dependency: the jmespath python package.
Beware that el10 epel does not offer the ansible package, which is fixed by Pigsty PGDG el10 repo
macOS
Ansible is available on macOS too. You can install Ansible on your Mac with Homebrew. And use it as the admin node to manage remote cloud server. It’s convenient if you are deploying a single-node pigsty on cloud VPS. But not recommended for production use.
Basics
Knowledge about Ansible is good but NOT REQUIRED. You only need to know how to run Ansible Playbooks. Playbooks are executable YAML files that contain a series of tasks to be executed.
Running the ./node.yml playbook essentially translates to ansible-playbook node.yml.
The hashbang at the top of the file makes it directly executable.
And you can use Args to control the playbook execution:
The following 4 parameters need your attention to use ansible effectively:
| Purpose | Parameter | Description |
|---|---|---|
| Where | -l|--limit <pattern> |
Limit execution target on specific group/host/pattern |
| What | -t|--tags <tags> |
Only run tasks with specific tags |
| How | -e|--extra-vars <vars> |
Extra command line arguments |
| Config | -i|--inventory <path> |
Using a specific inventory file |
Limit Host
The execution target of a playbook can be limited with -l|--limit <selector>.
It is handy when trying to run playbooks on a specific host/node or group/clusters.
Here are some examples of host limits:
Check all details in the ansible docs: Patterns: targeting hosts and groups
Missing this value could be dangerous, since most playbooks will execute on all hosts. DO USE WITH CAUTION.
Limit Task
The execution tasks can be controlled with -t|--tags <tags>.
If specified, tasks with given tags will be executed instead of the ENTIRE playbook.
Here are some task limit examples:
To run multiple tasks, specify multiple tags and separate with comma: -t tag1,tag2:
Extra Vars
You can override config param at runtime with cli args, it has the highest precedence.
Extra command-line args can be passed via -e|--extra-vars KEY=VALUE, it can be used multiple times:
for complex parameters, JSON string can be used:
Designate Inventory
The default config file is pigsty.yml in the pigsty home directories.
You can use the -i <path> parameter to specify a different Inventory file path.
To permanently change the default config file, change the inventory parameter in the ansible.cfg.
2 - Playbook
Pigsty implements admin controllers with idempotent Ansible playbooks.
Playbooks require the ansible-playbook executable bin in your PATH. You’ll have to install ansible to run playbooks.
Here are built-in playbooks in Pigsty, you can also add your own.
| Module | Playbook | Function |
|---|---|---|
| INFRA | install.yml |
Install Pigsty on current node in one-pass |
| INFRA | infra.yml |
Init pigsty infrastructure on infra nodes |
| INFRA | infra-rm.yml |
Remove infrastructure components from infra nodes |
| INFRA | cache.yml |
Make offline install packages from target node |
| INFRA | cert.yml |
Issue cert with pigsty self-signed CA (e.g. for pg clients) |
| NODE | node.yml |
Init node for pigsty, tune node into desired status |
| NODE | node-rm.yml |
Remove node from pigsty |
| PGSQL | pgsql.yml |
Init HA PostgreSQL clusters, or adding new replicas |
| PGSQL | pgsql-rm.yml |
Remove PostgreSQL cluster, or remove replicas |
| PGSQL | pgsql-db.yml |
Add new business database to existing PostgreSQL cluster |
| PGSQL | pgsql-user.yml |
Add new business user to existing PostgreSQL cluster |
| PGSQL | pgsql-pitr.yml |
Run point-in-time-recovery on existing PostgreSQL cluster |
| PGSQL | pgsql-monitor.yml |
Monitor remote postgres instance with local exporters |
| PGSQL | pgsql-migration.yml |
Generate Migration manual & scripts for existing PostgreSQL |
| PGSQL | slim.yml |
Install Pigsty with minimal components |
| REDIS | redis.yml |
Init redis cluster/node/instance |
| REDIS | redis-rm.yml |
Remove redis cluster/node/instance |
| ETCD | etcd.yml |
Init etcd cluster, or append new member |
| ETCD | etcd-rm.yml |
Remove etcd cluster, or remove existing member |
| MINIO | minio.yml |
Init minio cluster |
| MINIO | minio-rm.yml |
Remove minio cluster |
| DOCKER | docker.yml |
Install docker on nodes |
| DOCKER | app.yml |
Install application with docker compose |
| FERRET | 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 |
✓ |
There is a weak circular dependency between NODE and 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
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.
- Read playbook documentation carefully before execution
- Ctrl-C to stop immediately if you see something wrong
- Start with non-production environments for testing
- Limit execution hosts (
-l) to avoid unintended hosts if applicable - Use specific tags (
-t) to run subset of tasks if possible
3 - Nginx Portal
Pigsty installs Nginx on the INFRA Node as a web service proxy, using ports 80/443 by default.
The global parameter infra_portal configures Nginx proxy rules and upstream services.
The Nginx server configuration is specified through the infra_portal parameter.
Users declare all domains to be proxied through Nginx, along with corresponding upstream server endpoints or local directory paths.
Basic Example
Complex Example
Playbook Configuration
Nginx can be reconfigured using Ansible playbooks:
Server
Each server record in infra_portal supports the following configuration options:
Core Parameters
domain- Optional proxy domain nameendpoint- Upstream service address (IP:PORT or socket path)path- Local web server root directory for static contentscheme- Protocol specification (http/https/tcp/udp)
SSL/TLS Parameters
certbot- Enable Let’s Encrypt certificate managementcert- Custom SSL certificate file pathkey- Custom SSL private key file path
Advanced Parameters
conf- Custom Nginx configuration templatedomains- Additional domain names for the serviceindex- Enable directory listing for static contentlog- Custom log file configurationwebsocket- Enable WebSocket support for real-time applications
Parameter Usage Examples
Using Domain Names
DNS Resolution Methods
- Public internet domain via DNS provider
- Internal network DNS server
- Local
/etc/hostsfile modification
Recommended Local Configuration
For local development and testing, add entries to your /etc/hosts file:
Replace <your_public_ip_address> with your actual admin node IP address.
HTTPS Configuration
Configure HTTPS access via the nginx_sslmode parameter with the following options:
disabled- HTTP only, no SSLself-signed- Use self-signed certificates (default)provided- Use provided certificatesletsencrypt- Use Let’s Encrypt certificates
Certificate Management
HTTPS Access Methods
For self-signed certificates, you can:
- Trust the self-signed CA in your browser
- Use browser security bypass options (type
thisisunsafein Chrome) - Configure proper CA-signed certificates for production
Service Access Examples
With the default configuration, services are accessible via:
- Home Page:
http://h.pigstyorhttps://h.pigsty - Grafana Dashboard:
http://g.pigstyorhttps://g.pigsty - Prometheus Metrics:
http://p.pigstyorhttps://p.pigsty - Alertmanager:
http://a.pigstyorhttps://a.pigsty
Best Practices
- Use domain names for service access rather than direct IP:PORT
- Configure DNS resolution or update local hosts file appropriately
- Enable WebSocket support for services that require it (like Grafana, Jupyter)
- Use HTTPS in production environments with proper certificates
- Organize services logically with meaningful subdomain naming
- Monitor certificate expiration for Let’s Encrypt certificates
- Centralize web service proxy through Nginx for better management
- Use static file serving for documentation and repository browsing
4 - Local Repo
Quick Start
If you want to add some packages to the local repo, add them to:
repo_packagesfor default packagesrepo_extra_packagesfor extra packages
Then run the make repo shortcut to update the local repo and node repo cache:
Using Alias
You can use alias to specify a bundle of packages, check roles/node_id/vars/<os>.<arch>.yml for available aliases:
EL
| Distro | x86_64 | aarch64 |
|---|---|---|
| EL 7 | el7.x86_64.yml |
- |
| EL 8 | el8.x86_64.yml |
el8.aarch64.yml |
| EL 9 | el9.x86_64.yml |
el9.aarch64.yml |
| EL 10 | el10.x86_64.yml |
el10.aarch64.yml |
Debian
| Distro | x86_64 | aarch64 |
|---|---|---|
| Debian 11 | d11.x86_64.yml |
- |
| Debian 12 | d12.x86_64.yml |
d12.aarch64.yml |
| Debian 13 | d13.x86_64.yml |
d13.aarch64.yml |
| Ubuntu 22.04 | u22.x86_64.yml |
u22.aarch64.yml |
| Ubuntu 24.04 | u24.x86_64.yml |
u24.aarch64.yml |
Reference
Use these playbook tasks to manage local package repositories (YUM/APT) on Infra nodes:
Commonly used commands:
5 - DNS Domain
After installing Pigsty, users can access most Infra components’ web interfaces via IP + Port.
Let’s say your node’s internal IP is 10.10.10.10, then by default:
- http://10.10.10.10:3000 is Grafana dashboard (your daily command center)
- http://10.10.10.10:9058 is Prometheus TSDB console
- http://10.10.10.10:9059 is AlertManager console
- http://10.10.10.10 is Nginx HTTP entry point (default port 80)
While IP + Port works fine for dev/test environments (hey, we’re all lazy sometimes!), for more serious deployments, I strongly recommend accessing these services via domain names.
Using domains has numerous advantages, doesn’t cost extra, and requires just one simple config line.
Let’s dive into these topics:
- TL;DR
- Why Use Domains?
- How DNS Works
- Pigsty Default Domains
- Local Static Resolution
- Internal Dynamic Resolution
- Local HTTPS Access
- Trust Self-signed CA
- Public Domain Resolution
TL;DR
Add this static resolution record to your /etc/hosts (Linux/MacOS) or C:\Windows\System32\drivers\etc\hosts (Windows):
Replace placeholder IP
10.10.10.10with your Pigsty node’s IP (public/private, as long as it’s reachable).If you modified default domains in
infra_portal, replace them with your custom domains.
Why Use Domains?
Pigsty strongly recommends using domains instead of direct IP+Port access for several reasons:
- Domains are easier to remember (unless you’re a robot 🤖)
- More flexible — point to different IPs without changing configs
- Consolidate all services behind Nginx for better management, auditing, and reduced attack surface
- Enable HTTPS encryption to prevent traffic snooping
- In China, HTTP access to unregistered domains gets hijacked by ISPs, but HTTPS doesn’t
- Access services bound to 127.0.0.1 or internal Docker networks via Nginx proxy
Pigsty uses internal static domains by default — just add DNS records locally, no need to register real domains.
For internet-facing deployments, consider using real domains with free HTTPS certs.
How DNS Works
If you’re not familiar with HTTP/DNS protocols, here’s a quick primer on how Nginx serves multiple domains on a single port (80 + HTTPS 443):
DNS Protocol
- When a client (e.g., browser) accesses https://a.pigsty.cc, it first resolves the domain via DNS
- Resolution can use local static files, internal DNS servers, or public DNS
- DNS returns an IP - multiple domains can point to the same IP
- The client just needs to know: which IP to send requests to
HTTP Protocol
- HTTP requests (HTTP/1.1+) include a Host header with the requested domain
- This Host header is crucial — HTTP/1.1 spec requires clients to include it
- Nginx uses the Host header to match and route requests to different sites
- Thus, one port can serve different content based on the Host value
Pigsty Default Domains
Pigsty configures these four internal domains by default:
| Domain | Name | Port | Component | Description |
|---|---|---|---|---|
h.pigsty |
home |
80/443 | Nginx | Default server, local repo |
g.pigsty |
grafana |
3000 | Grafana | Monitoring & visualization |
p.pigsty |
prometheus |
9058 | Prometheus | Time series DB |
a.pigsty |
alertmanager |
9059 | AlertManager | Alert aggregation & routing |
Since these domains don’t use TLDs, you’ll need local static or internal dynamic resolution.
Don’t worry — it’s just one config line away! 🚀
Local Static Resolution
Assuming Pigsty’s internal IP is 10.10.10.10, add this to your client machine’s hosts file:
Adding Resolution
The client machine is where you browse Pigsty services — your laptop, desktop, VM, etc.
For Linux / macOS: sudo nano /etc/hosts For Windows: Run notepad as admin, edit C:\Windows\System32\drivers\etc\hosts
After adding the record, you can access Pigsty web services via these domains.
Custom Domains
Not a fan of default domains? Modify them in infra_portal before installation:
Then update your hosts file accordingly:
Use any domain you like — real or made-up - as long as it resolves to Pigsty’s IP via local, internal, or public DNS.
Additional Records
Running other Pigsty extensions? Add these records too:
Public IP Resolution
For cloud deployments, resolve to your public IP, not internal IP.
If your server has internet access, it typically has two NICs - one for internet (public IP) and one for internal network (private IP).
Example: If your cloud server’s public IP is 1.2.3.4 and VPC IP is 10.10.10.10:
Internal Dynamic Resolution
Want your office colleagues to access Pigsty via domains? Use internal dynamic resolution.
The simplest way: Ask your network admin to add the DNS records to your internal DNS server.
Using Internal DNS
If your internal DNS server is 192.168.1.1, on Linux/MacOS edit /etc/resolv.conf:
On Windows: Network Settings → Network Adapter → TCP/IPv4 Properties → DNS config
Test internal DNS resolution:
Using Pigsty’s DNS
Pigsty Infra module includes DNS server (port 53).
⚠️ Warning for China deployments: Public servers typically cannot run DNS services (port 53)!
Local HTTPS Access
HTTP access to Pigsty shows “Not Secure” - it’s plaintext, susceptible to MITM attacks.
By default, Pigsty uses a local self-signed CA to issue certs for all Nginx virtual hosts.
HTTPS access shows “Certificate Error” - these are self-signed certs, not from a trusted CA.
Your options:
- Ignore it, use HTTP or IP+Port (it’s internal anyway, right? 😅)
- Use HTTPS, click “Advanced → Proceed anyway”
- Chrome users: Type
thisisunsafewhen warned (magic words!) - Trust the self-signed certs by adding Pigsty’s CA to your browser/OS
- Use a real CA cert for Pigsty
- Use real domains with proper HTTPS certs
For internal access needing HTTPS without constant warnings, trust Pigsty’s self-signed CA.
For production, we recommend using public domains with free HTTPS certs via certbot.
Trust Self-signed CA
Pigsty generates a self-signed CA in the admin node source directory (~/pigsty) during init.
To use HTTPS, distribute Pigsty’s CA cert to client trust stores (or use real CAs — expensive!).
Pigsty-managed Linux nodes auto-trust the CA. For other Linux systems:
- Trust CA Cert
- EL
- Debian / Ubuntu
MacOS: Double-click ca.crt, add to Keychain, search pigsty-ca, open and “Trust” the root cert.
Windows: Add ca.crt to “Trusted Root Certification Authorities”.
After trusting Pigsty’s CA, no more “untrusted certificate” warnings! 🎉
Public Domain Resolution
Use DNS providers like Cloudflare, Godaddy, Aliyun, or Tencent Cloud DNSPod.
Requires purchasing a domain - basic ones cost ~$10/year.
Add DNS records via provider’s console/API to point domains to Pigsty’s public IP.
Example: With domain pigsty.xxx, add wildcard * A record or individual A records:
- h.pigsty.xxx → 1.2.3.4
- a.pigsty.xxx → 1.2.3.4
- p.pigsty.xxx → 1.2.3.4
- g.pigsty.xxx → 1.2.3.4
Pigsty includes Certbot support for free HTTPS certs (renew every 3 months).
Further Reading
For more advanced configurations, check the Pigsty documentation for DNS, Nginx, and HTTPS certificate management.
6 - SSL Certs
Pigsty comes with Certbot pre-installed on the Infra node, enabling you to obtain free Let’s Encrypt HTTPS certificates for Nginx servers and public domains.
Prerequisites
Before obtaining Let’s Encrypt certificates, ensure you have:
- A public domain name
- DNS records pointing to your server’s public IP
- Nginx properly configured with your domains
Step 1: Determine Which Domains Need Certificates
First, identify which upstream services require public certificates by configuring domains in your infra_portal:
Step 2: Point Domains to Your Server
Configure DNS A records to point all your domains to your server’s public IP address:
Verify that your domains are properly pointing to your server:
Step 3: Request Certificates with Certbot
Use Certbot to request Let’s Encrypt certificates for your domains:
Interactive Method (First Time)
During the first run, you’ll be prompted to:
- Provide an email address for Let’s Encrypt account registration
- Agree to the Terms of Service
- Choose whether to share your email with the Electronic Frontier Foundation
Non-Interactive Method
For automated deployments, use the non-interactive mode:
Example for multiple domains:
Step 4: Update Nginx Configuration
After successfully obtaining certificates, update your infra_portal configuration to use them by adding the certbot: true parameter:
Then regenerate the Nginx configuration and restart the service:
Step 5: Configure Certificate Renewal
Let’s Encrypt certificates expire every 90 days. Set up automatic renewal to ensure continuous HTTPS coverage:
Test Renewal (Dry Run)
Before setting up automatic renewal, test the process:
Manual Renewal
To manually renew all certificates:
To renew a specific certificate:
Automatic Renewal
Set up a monthly cron job for automatic renewal:
Alternatively, use a systemd timer if available:
Certificate Management Commands
Here are useful Certbot commands for managing your certificates:
Troubleshooting
Common Issues
- Domain not accessible: Ensure DNS records are properly configured and propagated
- Port 80 blocked: Let’s Encrypt requires port 80 for domain validation
- Rate limits: Let’s Encrypt has rate limits; avoid requesting too many certificates quickly
- Firewall issues: Ensure ports 80 and 443 are open in your firewall
Verification Commands
Best Practices
- Use wildcard certificates for multiple subdomains when appropriate
- Monitor certificate expiration with automated alerts
- Test renewal process regularly with dry runs
- Keep backups of your certificate files
- Use staging environment for testing before production deployment
- Set up monitoring for certificate expiration dates
- Document your domain configuration for team reference
Security Considerations
- Protect private keys: Ensure certificate private keys have restricted permissions
- Use strong SSL configuration: Configure Nginx with modern SSL settings
- Enable HTTP to HTTPS redirection: Force secure connections
- Implement HSTS: Add HTTP Strict Transport Security headers
- Regular security audits: Test your SSL configuration with tools like SSL Labs