Can I use an environment variable as map source?

I want to include a snippet file conditionally.
I am aware that I can just assign a value or a default value to en env var like:

{$TO_INCLUDE:import ./snippet.caddyfile}

but I’d like to have the env var as boolean and use it in a map like:

map {$INCLUDE_SNIPPET} {my_snippet} {
    default "import ./snippet.caddyfile"
    "true" ""
}

I couldn’t find in the documentation that this wont work, still, when I am trying it I get Error: adapting config using caddyfile: /etc/caddy/Caddyfile:48: unrecognized directive: {my_snippet}

Thank you!

Here’s the documentation on snippets.

I think maps is for mapping one value to another for use within other directives, not for controlling the inclusion of entire directives or files. The error unrecognized directive: {my_snippet} is because Caddy is trying to interpret {my_snippet} as a directive itself, which it isn’t.

I think import would work, but I’m unsure if it will be boolean. If you set an environmental variable like this:

services:
  caddy:
    environment:
      INCLUDE_DIRECTIVE: 'import ./snippet.caddyfile'

and then put this in a Caddyfile:

example.com {
    # other directives
    {$INCLUDE_DIRECTIVE}
    # more directives
}

it may work as you’d like. But I’m not entirely confident in that.

Using the env var as you mentioned in the example do work indeed.
But it’s kind of messy, I would prefer to keep the env var simple, like a boolean or a yes/no thing.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.