Conditional variable export in chezmoi.<format>.tmpl throwing undefined error on chezmoi init
#2907
-
I've been making the switch to chezmoi for my dotfiles, starting with my work configs, but I'm running into the same problem as #2414 where certain variables conditionally set are not available in later parts of the template. Here's a slightly redacted snippet of my {{/* The following variables are only available within this template file */}}
{{/* boolean feature tags */}}
{{- $docker := false -}}{{/* true if this machine should have Docker installed */}}
{{- $ephemeral := false -}}{{/* true if this machine is ephemeral, e.g. a cloud or VM instance */}}
{{- $headless := false -}}{{/* true if this machine does not have a screen and keyboard */}}
{{- $remote := false -}}{{/* true if this machine is connected to via SSH */}}
{{- $work := true -}}{{/* true if this machine is used for work, changed from default of false for testing */}}
{{- "" -}}
# ... jumping down to a later part of the template
{{/* variables to set if the machine is used for work */}}
{{- if $work -}}
{{- $artifactory_host := "artifactory.some-domain.name" -}}
{{- $artifactory_base := printf "https://%s:443/artifactory" $artifactory_host -}}
{{- $docker_base := "docker.some-domain.name:443" -}}
{{- end -}}
sourceDir: {{ .chezmoi.sourceDir | quote }}
data:
# Here we "export" the variables, so we can access them outside this file
name: {{ $name | quote }}
email: {{ $email | quote }}
{{- if $work -}}
artifactory_host: {{ $artifactory_host | quote }}
artifactory_base: {{ $artifactory_base | quote }}
docker_base: {{ $docker_base | quote }}
{{- end -}}
docker: {{ $docker }}
ephemeral: {{ $ephemeral }}
headless: {{ $headless }}
remote: {{ $remote }}
work: {{ $work }} When I run chezmoi: template: chezmoi.yaml:61: undefined variable "$artifactory_host" If I remove only the first chezmoi: /Users/robellegate/src/git.some-domain.name/robellegate/dotfiles/home/.chezmoi.yaml.tmpl: yaml: line 21: mapping values are not allowed in this context The template works only after removing both Maybe I can get the outcome I'm looking for by using I'm using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You must initialize these variables outside |
Beta Was this translation helpful? Give feedback.
You must initialize these variables outside
if
statement, using:=
. Then insideif
you update the values with=
.