config.j2

Overview

config.j2 is a Jinja2 template file designed to generate a YAML configuration for a proxy service. This configuration primarily sets up TLS certificates, network binding addresses, peer certificates, gossip protocol settings, and block keeper addresses. The template dynamically inserts values based on environment variables, host facts gathered by Ansible, and predefined inventory variables, allowing adaptive configuration tailored to the deployment environment.

File Functionality and Structure

The file is structured as a series of key-value pairs with some keys containing lists or nested mappings. It combines static values with dynamic content generated through Jinja2 templating logic, including conditional statements and loops.

Key Configuration Sections:

Detailed Explanation of Template Constructs

TLS Credentials

my_cert: "/workdir/certs/proxy.ca.pem"
my_key: "/workdir/certs/proxy.key.pem"
peer_certs: []

Binding Address (bind)

bind: {{ PROXY_BIND_IP | default(HOST_PUBLIC_IP) }}:{{ PROXY_PORT }}

Computing my_addr

my_addr:
{% if ansible_all_ipv4_addresses is defined %}
{% for ip in ansible_all_ipv4_addresses %}
- {{ ip }}:{{ PROXY_PORT }}
{% endfor %}
{% else %}
{% for iface in ansible_interfaces | default([]) %}
{% set fact = hostvars[inventory_hostname]['ansible_' ~ iface] | default({}) %}
{% if fact.ipv4 is defined and fact.ipv4.address is defined %}
- {{ fact.ipv4.address }}:{{ PROXY_PORT }}
{% endif %}
{% endfor %}
{% endif %}

Subscription List

subscribe: []

Block Keeper Addresses (bk_addrs)

bk_addrs:
{% if PROXY_BK_ADDRS %}
{% for addr in PROXY_BK_ADDRS %}
- {{ addr }}
{% endfor %}
{% else %}
- {{ hostvars[groups['block_keepers'][0]].HOST_PUBLIC_IP }}:8600
- {{ hostvars[groups['block_keepers'][1]].HOST_PUBLIC_IP }}:8600
- {{ hostvars[groups['block_keepers'][2]].HOST_PUBLIC_IP }}:8600
{% endif %}

Gossip Protocol Configuration

gossip:
  listen_addr: {{ HOST_PUBLIC_IP }}:{{ PROXY_GOSSIP_PORT }}
  advertise_addr: {{ HOST_PUBLIC_IP }}:{{ PROXY_GOSSIP_PORT }}
  seeds:
  {% for seed in SEEDS -%}
  - {{ seed }}
  {% endfor -%}
  cluster_id: {{ NETWORK_NAME }}

Environment and Inventory Dependencies

Usage Example

When rendered with proper variables and facts, the template produces a YAML configuration, for example:

my_cert: "/workdir/certs/proxy.ca.pem"
my_key: "/workdir/certs/proxy.key.pem"
peer_certs: []

bind: 192.168.1.10:8080
my_addr:
- 192.168.1.10:8080
- 10.0.0.1:8080

subscribe: []
bk_addrs:
- 192.168.1.20:8600
- 192.168.1.21:8600
- 192.168.1.22:8600

gossip:
  listen_addr: 192.168.1.10:7000
  advertise_addr: 192.168.1.10:7000
  seeds:
  - 192.168.1.20:7000
  - 192.168.1.21:7000
  cluster_id: mainnet

Interactions with Other System Components

All these integrations ensure this configuration facilitates secure and network-aware proxy operation within a distributed system.


Mermaid Flowchart Diagram

flowchart TD
A[Start: Render Template] --> B{TLS Credentials}
B --> C[my_cert path]
B --> D[my_key path]
B --> E[peer_certs list]
A --> F{Network Binding}
F --> G[bind: IP:Port]
F --> H[my_addr List]
H --> I{If ansible_all_ipv4_addresses defined}
I -->|Yes| J[Loop IPs -> my_addr]
I -->|No| K[Loop interfaces -> my_addr]
A --> L[subscribe: Empty List]
A --> M{Block Keeper Addresses}
M --> N{If PROXY_BK_ADDRS defined}
N -->|Yes| O[Use PROXY_BK_ADDRS]
N -->|No| P[Use block_keepers group IPs]
A --> Q[Gossip Configuration]
Q --> R[listen_addr]
Q --> S[advertise_addr]
Q --> T[seeds list]
Q --> U[cluster_id]
style B fill:#f9f,stroke:#333,stroke-width:1px
style F fill:#bbf,stroke:#333,stroke-width:1px
style M fill:#bfb,stroke:#333,stroke-width:1px
style Q fill:#fbf,stroke:#333,stroke-width:1px

This flowchart summarizes the main sections and their decision points during the template rendering process.