# Inline template into variable

**URL:** <https://forum.ansible.com/t/inline-template-into-variable/40818>\
**Category:** Get Help\
**Tags:** template, variables, facts\
**Created:** [March 6, 2025, 11:03am UTC](https://forum.ansible.com/t/inline-template-into-variable/40818 "2025-03-06T11:03:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vegivamp](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@vegivamp](https://forum.ansible.com/u/vegivamp)\
**Post date:** [March 6, 2025, 11:03am UTC](https://forum.ansible.com/t/inline-template-into-variable/40818/1 "2025-03-06T11:03:45Z")

</div>

Hi there,

I’ve been trying to do something I _think_ I already saw done, but I can’t get it to work: I want to parse an inline template into a variable.

Everything I find is either to file (`ansible.builtin.template` task), template lookup from a template file, or just mentions regular `{{ var }}` templates, which are obvious, but I need loops and the fancy bits of jinja :-p

In essence, I have a nested dict:

```auto
config:
  default:
    instance: prodbox
  auth:
    admin_user: francis
    admin_password: heloveshismomandcatandpancakes
  database:
    type: mysql
    schema: pancake
    user: mom
    password: kitty99

```

And I need to build that into a bunch of individual lines in a variable that basically flatten the structure.

I’ve tried a million variations of this task:

```auto
    - ansible.builtin.set_fact:
        config_blob: |-
          {% for section, conf in config %}
            {% for key, value in conf %}
              {{ 'GF_' + section | upper + '_' + key | upper + '=' + value }}
            {% endfor %}
          {% endfor %}

```

but depending on the variation, I get errors about unexpected % signs, something not being able to convert to dict or list, etc.

Any help would be appreciated. Being told that my memory is broken and it totally isn’t possible is less fun but would at least put me at rest to find a less elegant solution with tempfiles 🙂

---

<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 6, 2025, 11:09am UTC](https://forum.ansible.com/t/inline-template-into-variable/40818/2 "2025-03-06T11:09:50Z")

</div>

If you need key and value in the loop you’d need to use `.items()` method on the dictionary to convert it to key-value tuples.

Also, note that whitespace is preserved in the loops, so you might not want the nesting.

Try this:

```plaintext
{% for section, conf in config.items() %}
{% for key, value in conf.items() %}
{{ 'GF_' + section | upper + '_' + key | upper + '=' + value }}
{% endfor %}
{% endfor %},'

```

---

<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 6, 2025, 12:30pm UTC](https://forum.ansible.com/t/inline-template-into-variable/40818/3 "2025-03-06T12:30:01Z")

</div>

There is also the [ansible.builtin.dict2items filter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/dict2items_filter.html) and since it looks like you are writing conf/ini format you might also consider the [community.general.ini\_file module](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html) and the [community.general.to\_ini filter](https://docs.ansible.com/ansible/latest/collections/community/general/to_ini_filter.html).

---

<div class="post-metadata">

**Author:** ![vegivamp](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@vegivamp](https://forum.ansible.com/u/vegivamp)\
**Post date:** [March 6, 2025, 12:53pm UTC](https://forum.ansible.com/t/inline-template-into-variable/40818/4 "2025-03-06T12:53:15Z")

</div>

@chris It’s not ini or toml, it just looks a little like it - and I’m also not writing it, as I said, I need it in a var.

@kristianheljas has pointed out exactly what I was missing - and I actually used that before, but apparently I’m getting old and forgetful 🙂 . Marking as solved, thank you !

---

<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 5, 2025, 12:54pm UTC](https://forum.ansible.com/t/inline-template-into-variable/40818/5 "2025-04-05T12:54:03Z")

</div>

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