2017-08-03 40 views
0

是否有可能从库存中为服务器选择将生成haproxy后端服务器配置的服务器的备用服务器。这背后的想法是,我们正在运行不同版本的软件的不同服务器和haproxy将请求路由到这些服务器取决于用户想要去的地方。 我希望能够自动生成haproxy配置,如果我能用V1.x或V2.x等标记某些服务器。Ansible仅为库存中的多台服务器生成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 

hosts文件看起来是这样的:

[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 
+0

有多种方式可以使用库存中的部分服务器(如使用模式或循环)。请分享一下您的工作流程(手册代码),以便其他人可以提供建议。 –

+0

不幸的是,我还没有一个剧本代码。我只想着如何做这项工作。 – zozo6015

+0

这个模板有什么问题?我看到你使用'for主机在groups.nginx'来遍历不同的主机 –

回答

1

如果我得到你的权利,你可能想尝试这是可能的解决方案之一:

库存:

[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 %} 
+0

这正是我需要的。让我支持并测试它。 – zozo6015

+0

任何想法,如果我有标签2.1和2.2,但我希望有2.1和2.2能够被添加到相同的后端是可能的吗? – zozo6015

+0

@ zozo6015你可以有复杂的if语句或OR,或几个if语句。 –

相关问题