Last Updated: February 25, 2016
·
1.728K
· dstoflet

Create a varnish director using groups

If you have varnish fronting your webservers you can use ansible's hostvars inventory variable and jinja2 to automatically generate the director VCL.
This below assumes you have a group named webservers that includes all the hosts varnish must reverse proxy to, and that that group was run in the current or previous playbook.

vcl.j2

{% for host in groups['webservers'] %}
backend web{{loop.index}} { 
        .host = "{{hostvars[host]['ansible_fqdn']}}"; 
        .probe = { .url = "/healthtest.html"; 
            .expected_response=200; 
            .interval = 15s; 
            .timeout = 7s; 
            .window = 5; 
            .threshold = 3; 
    }
}
{% endfor %}
director default_web round-robin {
{% for host in groups['webservers'] %}
        { .backend = web{{loop.index}}; }
{% endfor %}
}