# Render Jinja2 template into variable

**URL:** <https://forum.ansible.com/t/render-jinja2-template-into-variable/4866>\
**Category:** Get Help\
**Created:** [April 10, 2024, 2:25pm UTC](https://forum.ansible.com/t/render-jinja2-template-into-variable/4866 "2024-04-10T14:25:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erwyn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/erwyn/32/2854_2.png) [@Erwyn](https://forum.ansible.com/u/Erwyn)\
**Post date:** [April 10, 2024, 2:25pm UTC](https://forum.ansible.com/t/render-jinja2-template-into-variable/4866/1 "2024-04-10T14:25:34Z")

</div>

Hello there,

I’m have a kind of big variable to build that is containing a JSON payload. I was thinking of using a Jinja2 template file alongside to describe the content and render it into the variable. The only thing I could come up with is using something like:

```auto
"{{ lookup('template', './myTemplate.j2') }}"

```

as in:

```auto
tasks:
      - debug:
          var: rendered_template
        vars:
          rendered_template: "{{ lookup('template', './myTemplate.j2') }}"

```

Is this the way it should be achieved or am I doing something very wrong? Or is the idea of rendering into a variable even flawed to the core 😬 ?

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [April 10, 2024, 2:46pm UTC](https://forum.ansible.com/t/render-jinja2-template-into-variable/4866/2 "2024-04-10T14:46:11Z")

</div>

Json is very easy to manipulate as variables in Ansible. If your json payload is already saved to disk as a raw file, you can use `{{ lookup('file', 'payload.json') | from_json }}`.

```yaml
tasks:
  - debug:
      msg: "{{ payload.data[0].properties }}"
    vars:
      payload: "{{ lookup('file', 'payload.json') | from_json }}"

```

Templates are jinja2 files that contain variables that need to be replaced… although technically they don’t _have_ to have any variables and be a plaintext json file. It would still work, but would waste some overhead on loading the jinja2 libraries and processing the file.

On the other hand, if you’re querying the json payload from an API using the `uri:` module for e.g., you can just register the module’s output directly to a variable.

---

<div class="post-metadata">

**Author:** ![Erwyn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/erwyn/32/2854_2.png) [@Erwyn](https://forum.ansible.com/u/Erwyn)\
**Post date:** [April 10, 2024, 2:47pm UTC](https://forum.ansible.com/t/render-jinja2-template-into-variable/4866/3 "2024-04-10T14:47:52Z")

</div>

The idea in my case is that this payload has to be rendered first because there are indeed some variables that need to be replaced by Jinja2.

---

<div class="post-metadata">

**Author:** ![Denney-tech](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/denney-tech/32/1556_2.png) [@Denney-tech](https://forum.ansible.com/u/Denney-tech)\
**Post date:** [April 10, 2024, 2:49pm UTC](https://forum.ansible.com/t/render-jinja2-template-into-variable/4866/4 "2024-04-10T14:49:45Z")

</div>

Then yes, what you’re doing would work more or less. Ansible is pretty good about interpreting json, but piping to `from_json` to make sure wouldn’t hurt.

Edit: in fact, piping to `from_json` would make sure your rendered json is well-formatted. Your template could potentially render successfully and register to the variable as a malformed-json and you wouldn’t know until later in the play.

---

<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:** [May 10, 2024, 2:49pm UTC](https://forum.ansible.com/t/render-jinja2-template-into-variable/4866/5 "2024-05-10T14:49:49Z")

</div>

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