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



是否有可能从清单中选择少量服务器,为其生成haproxy后端服务器配置。这背后的想法是,我们正在使用不同版本的软件运行不同的服务器,haproxy 根据用户想要去的位置将请求路由到这些服务器。 我希望能够自动生成haproxy配置,如果我可以用V1.x或V2.x等标记某些服务器。

这是我可以想出的模板。到目前为止,我只做了覆盖所有节点的部分,因为我不知道如何做其余的事情。

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 %}

最新更新