# Why do variables for a template need top be defined 'above' the template task?

**URL:** <https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007>\
**Category:** Get Help\
**Tags:** template, variables, jinja2\
**Created:** [March 13, 2025, 6:32am UTC](https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007 "2025-03-13T06:32:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://forum.ansible.com/u/halloleo)\
**Post date:** [March 13, 2025, 6:32am UTC](https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007/1 "2025-03-13T06:32:12Z")

</div>

I want to use `ansible.builtin.template`. So I tried as a task:

```auto
- name: Create cfg file
  ansible.builtin.template:
    src: path/to/my/template.j2
    dest: /path/to/output/file.conf
    vars:
      my_port: 1234

```

with the file `template.j2` containing somewhere

```
port = {{ my_port }}

```

This task does not work. It has no syntax error, but at run time it complains about `my_port` not beeing defined.

Moving the variable to the block ‘above’ works:

```auto
- name: Create cfg file
    vars:
      my_port: 1234
    block:

    - name: Use template
      ansible.builtin.template:
        src: path/to/my/template.j2
        dest: /path/to/output/file.conf

```

Why is that?

Thanks for any explanation!

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [March 13, 2025, 8:05am UTC](https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007/2 "2025-03-13T08:05:16Z")

</div>

Is it a mistake with whitespace, try this?

```yaml
- name: Create cfg file
  ansible.builtin.template:
    src: path/to/my/template.j2
    dest: /path/to/output/file.conf
  vars:
    my_port: 1234

```

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [March 13, 2025, 11:20am UTC](https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007/3 "2025-03-13T11:20:40Z")

</div>

@chris gave you the riight syntax, for your task. However, I just want to just supplement their answer. “vars” is not an attribte of the template module. If you look at the template module here:  
[ansible.builtin.template module – Template a file out to a target host — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html#ansible-collections-ansible-builtin-template-module). You will see “vars” is not a valid attribute you can use. However, “vars” is a valid attribute you can use at the task or block level.

```auto
# Valid for block
- name: my block
  vars:
     redfish: bluefish

# valid for task
- name: my task
  vars:
     redfiish: bluefish
  ansiblemodule: jasldasjfdlaj

# invalid for task
- name: my task
  ansiblemodule:
      vars:
         redfish: bluefish

```

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [March 13, 2025, 11:53am UTC](https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007/4 "2025-03-13T11:53:55Z")

</div>

> [@halloleo](#):
>
> Why is that?

Variables are ansible core feature, it allows you to create re-usable/configurable blocks of automation.

They can also control how ansible connects/acts with your targets.

To learn more about variables, see [the docs](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html).

---

<div class="post-metadata">

**Author:** ![halloleo](https://avatars.discourse-cdn.com/v4/letter/h/ccd318/32.png) [@halloleo](https://forum.ansible.com/u/halloleo)\
**Post date:** [March 13, 2025, 11:18pm UTC](https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007/5 "2025-03-13T23:18:00Z")

</div>

Thanks @chris! That was it.

I had thought of the `vars` at part of (common?) attributes to the module, but, as @binbashroot points out, they are not.

Also, I wasn’t aware that `vars` can be attached to any `name` section, i.e. can be attached to a task.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 12, 2025, 11:18pm UTC](https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007/6 "2025-04-12T23:18:39Z")

</div>

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