Ansible-Roles/roles/routing-policy-dns-via-balancer/templates/policy-routing.service.j2
2026-05-08 22:25:29 +03:00

54 lines
2.5 KiB
Django/Jinja

[Unit]
Description=Policy routing — DNS ответы через балансировщики
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
RemainAfterExit=yes
{% if policy_routing_balancers | length > 0 %}
# Режим MAC-matching: отдельная таблица и правило для каждого балансировщика
{% for balancer in policy_routing_balancers %}
ExecStartPre=-/usr/sbin/ip rule del fwmark {{ balancer.fwmark }} table {{ balancer.table_id }} priority {{ 100 + loop.index0 }}
ExecStartPre=-/usr/sbin/ip route flush table {{ balancer.table_id }}
{% endfor %}
{% for balancer in policy_routing_balancers %}
ExecStart=/usr/sbin/ip rule add fwmark {{ balancer.fwmark }} table {{ balancer.table_id }} priority {{ 100 + loop.index0 }}
ExecStart=/usr/sbin/ip route add default table {{ balancer.table_id }} via {{ balancer.ip }}
{% endfor %}
{% for route in policy_routing_local_routes %}
{% for balancer in policy_routing_balancers %}
ExecStart=/usr/sbin/ip route add {{ route }} table {{ balancer.table_id }}
{% endfor %}
{% endfor %}
{% for balancer in policy_routing_balancers %}
ExecStop=-/usr/sbin/ip rule del fwmark {{ balancer.fwmark }} table {{ balancer.table_id }} priority {{ 100 + loop.index0 }}
ExecStop=-/usr/sbin/ip route flush table {{ balancer.table_id }}
{% endfor %}
{% else %}
# Режим ECMP (обратная совместимость: один или несколько балансировщиков без MAC)
ExecStartPre=-/usr/sbin/ip rule del fwmark {{ policy_routing_fwmark }} table {{ policy_routing_table_id }} priority 100
ExecStartPre=-/usr/sbin/ip route flush table {{ policy_routing_table_id }}
ExecStart=/usr/sbin/ip rule add fwmark {{ policy_routing_fwmark }} table {{ policy_routing_table_id }} priority 100
ExecStart=/usr/sbin/ip route add default table {{ policy_routing_table_id }} \
{% for gw in policy_routing_gateways %}
nexthop via {{ gw }} weight 1 {% if not loop.last %} \{% endif %}
{% endfor %}
{% for route in policy_routing_local_routes %}
ExecStart=/usr/sbin/ip route add {{ route }} table {{ policy_routing_table_id }}
{% endfor %}
ExecStop=-/usr/sbin/ip rule del fwmark {{ policy_routing_fwmark }} table {{ policy_routing_table_id }} priority 100
{% for route in policy_routing_local_routes %}
ExecStop=-/usr/sbin/ip route del {{ route }} table {{ policy_routing_table_id }}
{% endfor %}
ExecStop=-/usr/sbin/ip route flush table {{ policy_routing_table_id }}
{% endif %}
[Install]
WantedBy=multi-user.target