Ansible 仅为清单中的多个服务器生成 Haproxy 配置

Ansible generating Haproxy config only for several servers from inventory

是否有可能 select 库存中的少量服务器将为其生成 haproxy 后端服务器配置。这背后的想法是,我们是 运行 具有不同软件版本的不同服务器,而 haproxy 会根据用户想要去的地方将请求路由到这些服务器。 如果我可以用 V1.x 或 V2.x 等标记某些服务器,我希望能够自动生成 haproxy 配置。

这是我能想出的模板。到目前为止,我只做了涵盖所有节点的部分,因为我不知道如何做剩下的部分。

global
log /dev/log    local0
log /dev/log    local1 notice
chroot /var/lib/haproxy
user haproxy
group haproxy
daemon
maxconn     20000
tune.ssl.default-dh-param 2048

defaults
    log global
    mode    http
    option forwardfor
    option http-server-close
    option  httplog
    option  dontlognull
    timeout connect 5000ms
    timeout client 300s
    timeout server 300s

    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

frontend  http-in
    bind {{ ansible_default_ipv4.address }}:80
    redirect scheme https code 301 if !{ ssl_fc }

frontend  https-in
    bind {{  ansible_default_ipv4.address }}:443 ssl crt /etc/ssl/private/redcap.pem
    reqadd X-Forwarded-Proto:\ https

    acl host_staging hdr(host) -i {{ website_hostname }}
    use_backend staging_v2 if host_staging


    default_backend             redcap_all

    acl IsV1   urlp(rc_vers) v1
    acl IsV2   urlp(rc_vers) v2
    use_backend redcap_v1 if IsV1
    use_backend redcap_v2 if IsV2

    acl IsV1H hdr(rc_vers) eq v1
    acl IsV2H hdr(rc_vers) eq v2
    use_backend redcap_v1 if IsV1H
    use_backend redcap_v2 if IsV2H

    acl IsV1P path_dir v1.9
    acl IsV2P path_dir v2
    use_backend redcap_v1 if IsV1P
    use_backend redcap_v2 if IsV2P

    acl IsV2S path_dir swagger-ui
    use_backend redcap_v2 if IsV2S

    acl IsV2SJ path_end swagger.json
    use_backend redcap_v2 if IsV2SJ


backend redcap_all
    mode        http
    balance     leastconn
    timeout     connect 1s
    timeout     server  300s
    timeout     queue   30s
    option redispatch
    retries 3
    cookie rc_cookie_vers insert indirect nocache secure
    {% for host in groups.nginx %}
        server {{ host }} {{ hostvars[host]ansible_default_ipv4.address }}:8080 cookie {{ my_tag }} check inter 1000 fastinter 500 rise 2 fall 1
    {% endfor %}

backend redcap_v1
    mode        http
    balance     leastconn
    timeout     connect 1s
    timeout     server  300s
    timeout     queue   30s
    option redispatch
    retries 3
    cookie rc_cookie_vers insert indirect nocache secure
    #    {% for host in groups.jetty %}
    #       {% if hostvars[host].my_tag == 'v1\.*' %}
    #        server {{ host }} {{ ip }}:8080 cookie {{ my_tag }} check inter 1000 fastinter 500 rise 2 fall 1
    #       {% endif %}
    #    {% endfor %}

backend redcap_v2
    mode        http
    balance     leastconn
    timeout     connect 1s
    timeout     server  300s
    timeout     queue   30s
    option redispatch
    retries 3
    cookie rc_cookie_vers insert indirect nocache secure
    #    {% for host in groups.jetty %}
    #       {% if hostvars[host].my_tag == '2\.*' %}
    #       server {{ host }} {{ ip }}:8080 cookie {{ my_tag }} check inter 1000 fastinter 500 rise 2 fall 1
    #       {% endif %}
    #    {% endfor %}


backend staging_v2
    mode        http
    balance     leastconn
    timeout     connect 1s
    timeout     server  600s
    timeout     queue   30s
    option redispatch
    retries 3
    cookie rc_cookie_vers insert indirect nocache secure

主机文件如下所示:

[jetty]
test1 psql_host=test1 psql_db=testdb psql_user=testdb psql_pass=1vg324235dssdf871f2i1e2t14zx22yn14z51e2h1f1w1h8n1fg21f321imo1hhk1vgr psql_pass_plain='somepass' jasypt_pass=test jasypt_salt=my_test_salt psql_md_db=db_dictionary psql_md_user=db_dictionary psql_md_pass=15zm1l132432454twf1nlt1rag1t9g1tay1rbq1ni51tun1eau1n0o1w1y1kxy15yk my_tag='v1.2' ip=192.168.54.46

如果我没猜错,您可能想尝试将此作为可能的解决方案之一:

库存:

[nginx]
host1 mytag=A
host2 mytag=A
host3 mytag=B
host4 mytag=C

模板:

{% for host in groups.nginx %}
    {% if hostvars[host].mytag == 'A' %}
    server {{ host }} {{ hostvars[host].ansible_default_ipv4.address }}:8080 cookie v1.1 check inter 1000 fastinter 500 rise 2 fall 1
    {% endif %}
{% endfor %}