How to enter environment variables in varnish settings

I have 2 environment variables:

echo $FRONT1_PORT_8080_TCP_ADDR # 172.17.1.80
echo $FRONT2_PORT_8081_TCP_ADDR # 172.17.1.77

I want to add them to my default.vcl as:

backend front1 {
    .host = $FRONT1_PORT_8080_TCP_ADDR;
}

But I got a syntax error on $char.

I also tried with custom variables , but I cannot define them outside vcl_recv.

How can I get 2 values ​​in VCL?

+4
source share
3 answers

I managed to parse my vcl

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

Using 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
+5
source

You can use the use of echo for eval strings.

You can usually do something like:

VAR=test # Define variables

echo "my $VAR string" # Eval string

, , "eval", :

VAR=test # Define variables

eval echo $(cat file.vcl) # Eval string from the given file
+3

. , . -, , , Expected CSTR got 'std.fileread'.

fileread std .

varnishd :

mkdir -p /env; \
env | while read envline; do \
    k=${envline%%=*}; \
    v=${envline#*=}; \
    echo -n "$v" >"/env/$k"; \
done

, :

import std;

...

backend front1 {
    .host = std.fileread("/env/FRONT1_PORT_8080_TCP_ADDR");
    .port = std.fileread("/env/FRONT1_PORT_8080_TCP_PORT");
}

. , , . :

.port = std.integer(std.fileread("/env/FRONT1_PORT_8080_TCP_PORT"), 0);
+2

All Articles