kea-dhcp-ha.j2 2.22 KB
Newer Older
Pietsch, Martin's avatar
Pietsch, Martin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
{% macro ha(ipversion = "ipv4" %}
{%     set ha_servers = [] %}
{%-    for fctnhst in function_hosts -%}
{%-      set nodenetwork = hostvars[fctnhst].network %}
{%-      if nodenetwork.devices is defined %}
{%-        for ifname, ifdata in nodenetwork.devices.items() if ifdata.services is defined and ("dhcp-ha" in (ifdata.services | default([]))) %}
{%-          set has = {"name": fctnhst}
{%-          if ipversion == "ipv4" %}
{%-            do has.update({"url": 'http://%s:%s/' | format(ifdata.ipv4 | default("127.0.0.1"), kea_ha_port)} %}
{%-          else %}
{%-            do has.update({"url": 'http://[%s]:%s/' | format(ifdata.ipv6 | default("::1"), kea_ha_port)} %}
{%-          endif %}
{%-          if fctnhst != dhcp_primary_server or fctnhst != dhcp_secondary_server %}
{%-            do has.update({"auto-failover": false, "role": "backup"}) %}
{%-          elif fctnhst == dhcp_primary_server %}
{%-            do has.update({"auto-failover": true, "role": "primary"}) %}
{%-          else %}
{%-            if kea_ha_mode == "loadbalancing" %}
{%-              do has.update({"auto-failover": true, "role": "secondary"}) %}
{%-            else %}
{%-              do has.update({"auto-failover": true, "role": "standby"}) %}
{%-            endif %}
{%-          endif %}
{%-          do ha_servers.update(has) -%}
{%-        endfor %}
{%-      endif %}
{%-    endfor %}   

{%-    if (ha_servers | length) > 0 -%}
    "hooks-libraries": [
       {
          "library": "/usr/lib/hooks/libdhcp_lease_cmds.so",
          "parameters": { }
       },
       {
          "library": "/usr/lib/hooks/libdhcp_ha.so",
          "parameters": {
              "high-availability": [ {
                 "this-server-name": "{{ inventory_hostname}}",
{%-    if kea_ha_mode = "loadbalancing" %}
                 "mode": "load-balancing",
{%-    else -%}
                 "mode": "hot-standby",
{%-    endif -%}
                 "heartbeat-delay": 10000,
                 "max-response-delay": 10000,
                 "max-ack-delay": 5000,
                 "max-unacked-clients": 5,
                 "peers": [
                    {{ ha_servers | to_nice_json(indent=8) }}
                 ]
              }
          ]
      }
    ]
{%-    endif -%}
{%- endmacro %}