https://www.runatlantis.io/ logo
Title
j

JT

05/23/2023, 2:52 PM
does anyone have a working example of using an env var in a template override? For instance in the following example, I'd like to print out `TERRAGRUNT_TFPATH`in my template for plan. This is what I've tried but it doesn't work, the template is being ignored or not used I believe I have set the template override variable to my templates directory using the env var
ATLANTIS_MARKDOWN_TEMPLATE_OVERRIDES_DIR
single_project_plan_success.tmpl:
## everything is the same as in main branch and then at bottom adding this
# TF Terragrunt Path
```diff
{{ .Env.TERRAGRUNT_TFPATH }}
```
repo.yaml:
repos:
- id: "/.*/"
  workflow: terragrunt
workflows:
  terragrunt:
    plan:
      steps:
      - env:
          name: TERRAGRUNT_TFPATH
          command: 'echo "terraform${ATLANTIS_TERRAFORM_VERSION}"'
      - run: terragrunt plan -input=false -out=$PLANFILE
      - run: terragrunt show -json $PLANFILE > $SHOWFILE
r

ross strickland

05/23/2023, 2:55 PM
have you tried the
env
os function from sprig? https://masterminds.github.io/sprig/os.html
j

JT

05/23/2023, 2:57 PM
if this is needed shouldn't this be built in to the template function?
r

ross strickland

05/23/2023, 2:57 PM
sprig functions are available in templates by default
j

JT

05/23/2023, 2:59 PM
oh cool, it doesn't seem ot be using my template though - even if the var is incorrect I should still see the snippet at the bottom even if it's with a blank between the three `
r

ross strickland

05/23/2023, 3:05 PM
i don't remember exactly, but i think that if there is an issue with loading the custom templates (i.e. not formatted properly), the templating engine falls back to the default templates.
j

JT

05/23/2023, 3:06 PM
any idea how to debug that?
turning on the debug flag didn't give any output regarding templates
r

ross strickland

05/23/2023, 3:37 PM
how are your markdown template override dir/files structured? does the dir only contain the
single_project_plan_success.tmpl
?
j

JT

05/23/2023, 3:38 PM
I keep it in a
/home/atlantis/templates
directory, it contains two templates only
single_project_plan_success.tmpl
and
plan_success_wrapped.tmpl
I've copied and pasted their code from here
r

ross strickland

05/23/2023, 3:39 PM
and the only difference is the env statement mentioned above?
j

JT

05/23/2023, 3:40 PM
Even if I add just a setence, like "TF is neat" at the bottom it won't add that when I run
atlantis plan
{{ define "planSuccessWrapped" -}}
<details><summary>Show Output</summary>

```diff
{{ if .EnableDiffMarkdownFormat }}{{ .DiffMarkdownFormattedTerraformOutput }}{{ else }}{{ .TerraformOutput }}{{ end }}
{{ if .PlanWasDeleted -}} This plan was not saved because one or more projects failed and automerge requires all plans pass. {{ else -}} {{ if not .DisableApply -}} * ▶️ To apply this plan, comment: *
{{ .ApplyCmd }}
{{ end -}} {{ if not .DisableRepoLocking -}} * 🚮 To delete this plan click [here]({{ .LockURL }}) {{ end -}} * 🔁 To plan this project again, comment: *
{{ .RePlanCmd }}
{{ end -}} </details> {{ .PlanSummary -}} {{ template "diverged" . -}} {{ end -}} TF is neat``` Won't show
TF is neat
at the bottom
r

ross strickland

05/23/2023, 3:41 PM
is
TF is neat
outside of the define block?
j

JT

05/23/2023, 3:42 PM
define block? I figured it would just print it as a string literal edit: yes it's outside the block
r

ross strickland

05/23/2023, 3:43 PM
so the template is encapsulated between
{{ define "template" }}
...
...
{{ end }}
anything outside of that will not be included in the rendered output
j

JT

05/23/2023, 4:02 PM
That did help, moving it inside the block helped it
{{ define "singleProjectPlanSuccess" -}}
    {{ $result := index .Results 0 -}}
    Ran {{ .Command }} for {{ if $result.ProjectName }}project: `{{ $result.ProjectName }}` {{ end }}dir: `{{ $result.RepoRelDir }}` workspace: `{{ $result.Workspace }}`

    {{ $result.Rendered }}
    {{ if ne .DisableApplyAll true }}
    ---
    * :fast_forward: To **apply** all unapplied plans from this pull request, comment:
        * `{{ .ExecutableName }} apply`
    * :put_litter_in_its_place: To delete all plans and locks for the PR, comment:
        * `{{ .ExecutableName }} unlock`
    {{ end -}}
    {{- template "log" . -}}

    ### TFGrunt Path Output
    ```diff
      {{ .Env.TERRAGRUNT_TFPATH }}
{{ end -}}``` but going back to the original question about the env var, I get the following issue:
ailed to render template, this is a bug: template: single_project_plan_success.tmpl:17:9: executing "singleProjectPlanSuccess" at <.Env.TERRAGRUNT_TFPATH>: can't evaluate field Env in type events.resultData
r

ross strickland

05/23/2023, 4:11 PM
i don't believe
.Env
is passed to the templates currently. can you try using the sprig function to access the OS ENV vars?
j

JT

05/23/2023, 4:12 PM
Sure, how do you do that? 😅 I didn't even know what sprig was until this conversation so I'm not sure how to use it from within atlantis
r

ross strickland

05/23/2023, 4:19 PM
probably something like:
{{ env "ENVVAR" }}
though it might require using that to set a variable and the referencing that var.
j

JT

05/23/2023, 4:40 PM
I tried that but it doesn't work either, do you know of an example of this being done anywhere?
r

ross strickland

05/23/2023, 4:56 PM
when you say it doesn't work, what happens exactly?
j

JT

05/23/2023, 5:01 PM
there's no output for
{{ env "TERRAGRUNT_TFPATH" }}
it rendered this for that portion:
```diff
  
```
r

ross strickland

05/23/2023, 5:02 PM
at what level/where is the env var being set?
j

JT

05/23/2023, 5:06 PM
Its being set by the workflow like this:
repos:
- id: "/.*/"
  workflow: terragrunt
workflows:
  terragrunt:
    plan:
      steps:
      - env:
          name: TERRAGRUNT_TFPATH
          command: 'echo "terraform${ATLANTIS_TERRAFORM_VERSION}"'
Trying to use it in the template like this:
{{ define "singleProjectPlanSuccess" -}}
    {{ $result := index .Results 0 -}}
    Ran {{ .Command }} for {{ if $result.ProjectName }}project: `{{ $result.ProjectName }}` {{ end }}dir: `{{ $result.RepoRelDir }}` workspace: `{{ $result.Workspace }}`

    {{ $result.Rendered }}
    {{ if ne .DisableApplyAll true }}
    ---
    * :fast_forward: To **apply** all unapplied plans from this pull request, comment:
        * `{{ .ExecutableName }} apply`
    * :put_litter_in_its_place: To delete all plans and locks for the PR, comment:
        * `{{ .ExecutableName }} unlock`
    {{ end -}}
    {{- template "log" . -}}
    ```diff
      {{ env "TERRAGRUNT_TFPATH" }}
{{ end -}}```
r

ross strickland

05/23/2023, 5:58 PM
unfortunately, the scope in which that env var is set and used is within a separate process fork, which is not accessible to the process which is rendering the template.
what you could probably get away with though is adding a custom run step in the plan workflow to append that data to the plan output
so, something like:
repos:
- id: "/.*/"
  workflow: terragrunt
workflows:
  terragrunt:
    plan:
      steps:
        - init
        - plan
        - run: 'echo "TERRAGRUNT_TFPATH: terraform${ATLANTIS_TERRAFORM_VERSION}"'
... the run command could be crafted to form the message you want to display more clearly.
j

JT

05/23/2023, 6:30 PM
is it possible to output a file from the templates? like run
echo "FOO" > /tmp/run-output.txt
and then grab it from within the template
it doesn't have to be an environment variable
r

ross strickland

05/23/2023, 6:47 PM
i don't think it's possible to read a file into the template
1
j

JT

05/23/2023, 7:02 PM
alrighty, thanks for the help
r

ross strickland

05/23/2023, 7:03 PM
did you check to see if
ATLANTIS_TERRAFORM_VERSION
env var is available in the process scope for rendering templates?
i doubt it would be, but worth a shot
j

JT

05/23/2023, 7:47 PM
well, that variable was just an example I was actually going to render a very large output for the template so that my users could get the plan + another process'es output
r

ross strickland

05/23/2023, 7:54 PM
got it. there are definitely ways to make it work -- in the past i've parsed out specific parts of output and used it to render other custom templates. for instance, if your output maps 1:1 with a project plan command, you can use a custom run step in the plan workflow to append that data to the plan output, and use loops to generate the specific variables representing the different data structures. it just might take some back and forth to tease evertyhing out.
i wonder if there would be value in opening an issue to track the outcome of this so that others can have an example of such a use case.