Search code examples
configurationenvironment-variablesvarnishvarnish-vcl

How to inject environment variables in Varnish configuration


I have 2 environments variables :

echo $FRONT1_PORT_8080_TCP_ADDR # 172.17.1.80
echo $FRONT2_PORT_8081_TCP_ADDR # 172.17.1.77

I want to inject them in a my default.vcl like :

backend front1 {
    .host = $FRONT1_PORT_8080_TCP_ADDR;
}

But I got an syntax error on the $ char.

I've also tried with user variables but I can't define them outside vcl_recv.

How can I retrieve my 2 values in the VCL ?


Solution

  • I've managed to parse my vcl

    backend front1 {
        .host = ${FRONT1_PORT_8080_TCP_ADDR};
    }
    

    With a script:

    envs=`printenv`
    
    for env in $envs
    do
        IFS== read name value <<< "$env"
    
        sed -i "s|\${${name}}|${value}|g" /etc/varnish/default.vcl
    done