HTTP File Server
playbooks/deploy-http.yaml brings pg http file servers
under Ansible management. pg http is a host process (no container, no add-on
type), so this uses a standalone pg_http role and a standalone playbook
instead of pg_addon / apply.yaml, mirroring how deploy-s3.yaml is
organized.
The role reconciles each server declared in pgcli_http_servers: it compares
the running spec by name and runs pg http stop + pg http start when
anything differs, creates the served directory, optionally copies the offline
binaries into it (sync_binaries: true), and registers boot autostart as needed.
Binary copying is disabled by default. Re-running converges to the declared state.
Configuration
Prepare an [http] group in the inventory (hosts.ini, gitignored), for
example:
The server list lives in group_vars/http/main.yml (gitignored; copy it from
the .example file next to it). Every field is optional; defaults are shown in
parentheses:
| Field | pg http flag | Description |
|---|---|---|
name |
--name (required) |
Server name, also the state file name |
host |
— | Reconcile only on this inventory host; omitted means all target hosts |
state |
— | started (default) / stopped / absent |
autostart |
— | Register boot autostart (default true; only allowed with state: started) |
dir |
--dir |
Served directory (default /srv/offline-images) |
dir_owner |
— | Directory owner (default deployment account); applies to the directory only |
dir_group |
— | Directory group (default deployment account); applies to the directory only |
dir_mode |
— | Directory permissions (default 0755); applies to the directory only |
listen |
--listen |
Listen address (default 127.0.0.1) |
port |
--port |
Listen port (default 18080) |
allow_upload |
--allow-upload |
Upload form (default false) |
allow_dir |
--allow-dir |
Create-directory form (default false) |
allow_delete |
--allow-delete |
Per-file delete button (default false) |
max_upload |
--max-upload |
Per-upload limit (default 1G, e.g. 512M) |
admin_ips |
--admin-ip |
Source-address allowlist for write modes (list, joined with commas) |
tls |
--tls |
HTTPS with a self-signed certificate generated and renewed under the pgcli base dir |
tls_cert |
--tls-cert |
PEM certificate path on the target host; implies TLS |
tls_key |
--tls-key |
Private key path paired with tls_cert |
auth |
--auth |
Basic Auth entries, each USER:PASSWORD (list) |
auth_env |
--auth-env |
Names of controller environment variables holding USER:PASSWORD |
auth_file |
--auth-file |
htpasswd files on the target host (bcrypt entries) |
force |
— | Restart even when the spec matches (default false) |
sync_binaries |
— | Copy the offline binaries into dir (default false) |
Examples
A directory writable by a scheduled job running as http-writer:
Minimal: expose the offline directory over HTTP on the LAN and register boot autostart.
Write modes restricted to specific sources, with a larger upload limit:
TLS, two ways (tls: true uses the auto-generated self-signed certificate;
certificate and key paths refer to the target host):
The three Basic Auth sources can be combined, but a username may appear in only one of them:
auth_env values are read on the controller with lookup('env', ...); only
the variable names are passed to the target host as --auth-env. When
plaintext auth or auth_env entries are present the start task runs with
no_log, so passwords do not appear in the default output or the recap.
Maintain htpasswd files with pgcli itself (incremental upsert, other accounts preserved):
Binary sync
Binary copying is disabled by default. Set sync_binaries: true on a server
to refresh the controller’s bin/pg into images/pg, then copy only
pgcli_http_binaries (default pg and podman-launcher-linux_amd64) into its
dir. Image tarballs are not copied here; export them into that directory
first with scripts/export-images.sh (see Offline image server).
The controller therefore needs a make build first, and the other binaries
must already exist under images/ when binary copying is enabled; otherwise the
playbook fails before any host is modified. Override the variables in
group_vars/http/main.yml:
A file-server host runs no containers, so the install stage skips container
images by default: the offline image server is not checked, no images are
loaded, and the installer runs with --skip-pull. That avoids a large
transfer on a distribution host that has no pg binary yet and needs no local
images. If the host must also carry images for other containers, override
with -e pgcli_load_images=true -e pgcli_install_skip_pull=false.
Running
The playbook has two stages: the pgcli tag installs and configures the host,
the http tag only reconciles servers. Servers run as the deployment account
(pgcli in rootless setups); served directories under /srv are created by
root and chowned to that account.
Idempotency and lifecycle
- Spec comparison covers
dir,listen,port, the three write-mode flags,max_upload,admin_ips, TLS, the certificate/key paths, and the auth usernames with their passwords/hashes. Any change restarts the server. - The role runs
pg http stopfirst so stale state files are cleaned up andpg http startcannot create animages-2style duplicate. - When the port is taken,
pg httpsilently increments it; the role re-reads the state file after starting and fails if the bound port differs from the declared one, so consumers never get a wrong URL. - The health probe hits
http(s)://<listen>:<port>/__pgcli_health(skipping certificate validation under TLS); the endpoint needs no authentication. - With
autostart: truethe role snapshots the running spec intopg.yamlviapg autostart enable --http --name, refreshing it on every restart.state: stoppedorautostart: falseremoves the snapshot. state: absentstops the server and deletes the state file.
Caveats
- Auth is compared by username and password: adding or removing accounts, and
changing the password of an existing one, restarts the server. Inline
authandauth_envpasswords are verified on the controller against the bcrypt hashes in the running spec;auth_fileentries are compared hash-for-hash, so accounts edited on the target withpg http htpasswdorhtpasswd -Bare picked up on the next run. A running process caches the hashes it read at start-up, which is why the role stops and starts it on a difference. auth_fileis read only at start-up: a manual edit takes effect after the next playbook run (or an explicitforce: truerestart).- A server started by hand with
pg http startis adopted: it is left alone when the spec matches, and restarted with the declared flags when it does not (manual flags missing from the configuration are lost). - Without auth the site is open to everyone; use
0.0.0.0only on trusted networks, combined withadmin_ipsor a firewall.--tlsencrypts traffic but does not authenticate clients. - During the install stage one “initialize the host” task always reports
changed(existing install-script behavior); the reconcile stage should bechanged=0when the state matches.