Ansible role: forward TCP ports to another host with redir, one systemd service per forward
- Jinja 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| defaults | ||
| meta | ||
| tasks | ||
| templates | ||
| README.md | ||
ansible-role-tcp-forward
Forwards TCP ports from this host to another one with redir, one systemd service per forward. Typical use: keep a service's non-HTTP port (e.g. Forgejo SSH) reachable at the same name as its HTTP side after the name moves to a reverse-proxy host.
What it does
- Fails unless every
tcp_forwardsentry hasname,listen_port,target_hostandtarget_port. - Installs
redir. - Templates
/etc/systemd/system/tcp-forward-<name>.serviceper entry:redir -n :<listen_port> <target_host>:<target_port>, restarted on failure, running as a dynamic unprivileged user with onlyCAP_NET_BIND_SERVICE(so ports below 1024 work). - Enables and starts each service, restarting it when its unit changed, then waits for the port to listen.
Removing an entry does not remove its service; stop and delete
tcp-forward-<name>.service by hand.
Variables
| Variable | Default | Description |
|---|---|---|
tcp_forwards |
[] |
{name, listen_port, target_host, target_port} |
The forwarder listens on all addresses. The target sees connections coming from this host, not from the original client.
Example playbook
- hosts: all
vars:
tcp_forwards:
- name: forgejo-ssh
listen_port: 222
target_host: 192.168.2.245
target_port: 222
roles:
- tcp-forward