# Using a variable within consul-template template annotation

**URL:** <https://discuss.hashicorp.com/t/using-a-variable-within-consul-template-template-annotation/36255>\
**Category:** Vault\
**Tags:** consul-template, vault\
**Created:** [February 25, 2022, 8:52pm UTC](https://discuss.hashicorp.com/t/using-a-variable-within-consul-template-template-annotation/36255 "2022-02-25T20:52:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![popopanda](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/popopanda/32/1238_2.png) [@popopanda](https://discuss.hashicorp.com/u/popopanda)\
**Post date:** [February 25, 2022, 8:52pm UTC](https://discuss.hashicorp.com/t/using-a-variable-within-consul-template-template-annotation/36255/1 "2022-02-25T20:52:44Z")

</div>

Hello,

I am trying to use a variable within my template when using annotations to render my secret file

I have:

```auto
        vault.hashicorp.com/agent-inject-template-parameters.yml: |
          {{- with secret "kv/default/restricted/service/empty" -}}
          {{- $globalSecret := printf "{{ with secret \"kv/devops/restricted/service-global\" }}{{ index .Data.data \"nr.license\" }}{{- end }}" }}
          db:
            database: '{{ index .Data.data "parameters.yml/db.database" }}'
            host: test.abcd.com
          NRTEST: {{ $globalSecret }}
          {{- end }}

```

I am able to get the database to render from vault, but for `NRTEST`, it renders it as exact:

```auto
NRTEST: {{ with secret "kv/devops/restricted/service-global" }}{{ index .Data.data "nr.license" }}{{- end }}

```

Instead, I am trying to get it to render the actual secret from `kv/devops/restricted/service-global`. I’ve tried different variations without success

Any advice appreciated

Thank you

---

<div class="post-metadata">

**Author:** ![eikenb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/eikenb/32/1173_2.png) [@eikenb](https://discuss.hashicorp.com/u/eikenb)\
**Post date:** [March 1, 2022, 12:42am UTC](https://discuss.hashicorp.com/t/using-a-variable-within-consul-template-template-annotation/36255/2 "2022-03-01T00:42:29Z")

</div>

Try replacing this line…

```auto
          {{- $globalSecret := printf "{{ with secret \"kv/devops/restricted/service-global\" }}{{ index .Data.data \"nr.license\" }}{{- end }}" }}

```

With this…

```auto
          {{- $globalService := secret "kv/devops/restricted/service-global" -}}
          {{- $globalSecret := index $globalService.Data.data "nr.license" -}}

```

That might get you what you want, or at least closer.

Let me know if that helps or if you have any other questions. Thanks for using consul-template!

---

<div class="post-metadata">

**Author:** ![popopanda](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/popopanda/32/1238_2.png) [@popopanda](https://discuss.hashicorp.com/u/popopanda)\
**Post date:** [March 1, 2022, 2:09am UTC](https://discuss.hashicorp.com/t/using-a-variable-within-consul-template-template-annotation/36255/3 "2022-03-01T02:09:19Z")

</div>

Thank you for your reply. I actually modified this to make it more flexible for our use case.

I changed this to:

```auto
        vault.hashicorp.com/agent-inject-template-parameters.yml: |
          {{ $globalSecret := }}
          {{- with secret "kv/default/restricted/service/empty" -}}
          {{- with secrets "kv/devops/restricted/service-global" -}}
          {{- $globalSecret = .Data.data }}
          {{- end }}
          db:
            database: '{{ index .Data.data "parameters.yml/db.database" }}'
            host: test.abcd.com
          NRTEST: '{{ index $globalSecret "nr.license" }}'
          {{- end }}

```

I’m hoping to pull secrets from the `kv/devops/restricted/service-global` path only when $globalSecret is defined.

I think my problem now is defining `{{ $globalSecret := }}` as a list. If I were to set it as a string like `"test"`, It errors out with

```auto
executing "" at <index $globals "nr.license">: error calling index: cannot index slice/array with type string"

```

I haven’t found information from consul-template documentation to initialize a variable of type list, but I did find that `list` function is available from sprig library. But it says `list` is not defined when i try to use it in my template.

I’m hoping that config is a viable solution once we figure out the variable type.

---

<div class="post-metadata">

**Author:** ![popopanda](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/popopanda/32/1238_2.png) [@popopanda](https://discuss.hashicorp.com/u/popopanda)\
**Post date:** [March 1, 2022, 8:59pm UTC](https://discuss.hashicorp.com/t/using-a-variable-within-consul-template-template-annotation/36255/4 "2022-03-01T20:59:54Z")

</div>

I got it working, the solution was rather simple

```auto
        vault.hashicorp.com/agent-inject-template-parameters.yml: |
          
          {{- with secret "kv/default/restricted/service/empty" -}}
          {{ $globalSecret := .Data.data }}
          {{- with secrets "kv/devops/restricted/service-global" -}}
          {{- $globalSecret = .Data.data }}
          {{- end }}
          db:
            database: '{{ index .Data.data "parameters.yml/db.database" }}'
            host: test.abcd.com
          NRTEST: '{{ index $globalSecret "nr.license" }}'
          {{- end }}

```

---

<div class="post-metadata">

**Author:** ![eikenb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/eikenb/32/1173_2.png) [@eikenb](https://discuss.hashicorp.com/u/eikenb)\
**Post date:** [March 8, 2022, 8:40pm UTC](https://discuss.hashicorp.com/t/using-a-variable-within-consul-template-template-annotation/36255/5 "2022-03-08T20:40:38Z")

</div>

Great to hear! Thanks for letting me know.  
Best of luck.
