# \#jinja2

**URL:** https://forum.ansible.com/tag/jinja2/173.md

[Latest](https://forum.ansible.com/latest.md) · [Categories](https://forum.ansible.com/categories.md) · [Tags](https://forum.ansible.com/tags.md)

---

## [Run a remote command from a template \*file\*](https://forum.ansible.com/t/run-a-remote-command-from-a-template-file/46319)

<div class="topic-metadata">

**Author:** [@mirabilos](https://forum.ansible.com/u/mirabilos)\
**Replies:** 12\
**Last updated:** [October 14, 2026, 10:35pm UTC](https://forum.ansible.com/t/run-a-remote-command-from-a-template-file/46319 "2026-10-14T22:35:06Z")

</div>

I need to run a few lines of shell on a remote system, and I find YAML more than awkward to use for this and would prefer to put this into a foo.sh.j2 file instead. Now I could do the dance: stat to test if a remote d…

---

## [Add b32encode and b32decode](https://forum.ansible.com/t/add-b32encode-and-b32decode/46425)

<div class="topic-metadata">

**Author:** [@agowa](https://forum.ansible.com/u/agowa)\
**Replies:** 8\
**Last updated:** [October 7, 2026, 8:26pm UTC](https://forum.ansible.com/t/add-b32encode-and-b32decode/46425 "2026-10-07T20:26:02Z")

</div>

See Add b32encode and b32decode by agowa · Pull Request #87627 · ansible/ansible · GitHub but apparently we’re doing discussions here now instead of in the PR…

---

## [Lazy templating and global variables in Ansible 2.19+](https://forum.ansible.com/t/lazy-templating-and-global-variables-in-ansible-2-19/45487)

<div class="topic-metadata">

**Author:** [@bozho](https://forum.ansible.com/u/bozho)\
**Replies:** 10\
**Last updated:** [March 9, 2026, 5:05pm UTC](https://forum.ansible.com/t/lazy-templating-and-global-variables-in-ansible-2-19/45487 "2026-03-09T17:05:24Z")

</div>

Hi all, We are in the process of upgrading from Ansible 2.18 to 2.20 and are aware that there were some significant changes to templating in 2.19. What would be the best practice for handling “undefined” variables in t…

---

## [Include jinja template files in module utils](https://forum.ansible.com/t/include-jinja-template-files-in-module-utils/45335)

<div class="topic-metadata">

**Author:** [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Replies:** 4\
**Last updated:** [February 13, 2026, 11:39pm UTC](https://forum.ansible.com/t/include-jinja-template-files-in-module-utils/45335 "2026-02-13T23:39:10Z")

</div>

I have a collection that relies on a graphql API. The queries are pretty unpleasant, and to make them a bit more manageable I store them as jinja templates. However, I struggled getting ansible to include/find the jinja …

---

## [Deprecation of ANSIBLE\_JINJA2\_EXTENSIONS - what to 'do'?](https://forum.ansible.com/t/deprecation-of-ansible-jinja2-extensions-what-to-do/45256)

<div class="topic-metadata">

**Author:** [@JuliaMono](https://forum.ansible.com/u/JuliaMono)\
**Replies:** 4\
**Last updated:** [February 7, 2026, 11:18am UTC](https://forum.ansible.com/t/deprecation-of-ansible-jinja2-extensions-what-to-do/45256 "2026-02-07T11:18:37Z")

</div>

I am running a sizable deployment of ansible with a bit of tooling around it to make things work for me. Some templates for example for network and network services gather extensive amount of data from the environment, …

---

## [Omit and the dict lookup](https://forum.ansible.com/t/omit-and-the-dict-lookup/45197)

<div class="topic-metadata">

**Author:** [@utoddl](https://forum.ansible.com/u/utoddl)\
**Replies:** 3\
**Last updated:** [January 26, 2026, 7:55pm UTC](https://forum.ansible.com/t/omit-and-the-dict-lookup/45197 "2026-01-26T19:55:55Z")

</div>

I’m seeing unexpected effects when using the omit value with the ansible.builtin.dict lookup. I think I understand what it’s doing, but I’m coming up short in the why department. Here’s a sample playbook that demonstrat…

---

## [Jinja2 template with nested loops](https://forum.ansible.com/t/jinja2-template-with-nested-loops/45047)

<div class="topic-metadata">

**Author:** [@Django](https://forum.ansible.com/u/Django)\
**Replies:** 2\
**Last updated:** [January 3, 2026, 8:44pm UTC](https://forum.ansible.com/t/jinja2-template-with-nested-loops/45047 "2026-01-03T20:44:28Z")

</div>

OK: what’s this about? I have defined various hosts in my inventory. Among other things, the hosts in the IDMZ zone each have an array with a different number of alias definitions. The host vml010110 has the following …

---

## [Best way to build a configurable when statement ?](https://forum.ansible.com/t/best-way-to-build-a-configurable-when-statement/44969)

<div class="topic-metadata">

**Author:** [@frederic.giquel](https://forum.ansible.com/u/frederic.giquel)\
**Replies:** 6\
**Last updated:** [December 10, 2025, 10:25pm UTC](https://forum.ansible.com/t/best-way-to-build-a-configurable-when-statement/44969 "2025-12-10T22:25:44Z")

</div>

In order to get instances matching some criteria, I wrote the following tasks file: --- # Required variables : # - deployment\_type: "vm" or "container" # - cmp\_operator: '\<', '\>', '\<=', '\>=' or '==' # - cmp\_days: number…

---

## [Unclear templating error started from 2.19.0](https://forum.ansible.com/t/unclear-templating-error-started-from-2-19-0/44874)

<div class="topic-metadata">

**Author:** [@yurnov](https://forum.ansible.com/u/yurnov)\
**Replies:** 4\
**Last updated:** [November 21, 2025, 4:35pm UTC](https://forum.ansible.com/t/unclear-templating-error-started-from-2-19-0/44874 "2025-11-21T16:35:08Z")

</div>

Going to migrate from ansible-core 2.18 to 2.20 for 3pp lifecycle management, and it was actually delayed due to changes in 2.19, and I am still stuck with that. So, simple playbook (written for test): --- - name: Test…

---

## [Non-aphabetic ordering of templated dict items](https://forum.ansible.com/t/non-aphabetic-ordering-of-templated-dict-items/44783)

<div class="topic-metadata">

**Author:** [@utoddl](https://forum.ansible.com/u/utoddl)\
**Replies:** 2\
**Last updated:** [November 4, 2025, 8:54pm UTC](https://forum.ansible.com/t/non-aphabetic-ordering-of-templated-dict-items/44783 "2025-11-04T20:54:15Z")

</div>

I’m pulling a bunch of dicts from an API, each of which looks something like the following after piping through to\_nice\_yaml: - createdAt: '2022-01-04T16:31:35Z' executionOrder: Parallel frequency: Weekly …

---

## [Type 'GroupTuple' is unsupported in variable storage](https://forum.ansible.com/t/type-grouptuple-is-unsupported-in-variable-storage/44513)

<div class="topic-metadata">

**Author:** [@vbotka](https://forum.ansible.com/u/vbotka)\
**Replies:** 9\
**Last updated:** [September 24, 2025, 4:07pm UTC](https://forum.ansible.com/t/type-grouptuple-is-unsupported-in-variable-storage/44513 "2025-09-24T16:07:16Z")

</div>

Given the list data: - {group: devel, team: dev\_1, users: \[alice, bob\]} - {group: devel, team: dev\_2, users: \[charlie, david\]} - {group: qa, team: qa\_1, users: \[mallory, ted\]} The Jinja filter groupby works as ex…

---

## [Filter ansible\_devices to get the first disk without partitions](https://forum.ansible.com/t/filter-ansible-devices-to-get-the-first-disk-without-partitions/44399)

<div class="topic-metadata">

**Author:** [@stemid](https://forum.ansible.com/u/stemid)\
**Replies:** 3\
**Last updated:** [September 1, 2025, 8:08pm UTC](https://forum.ansible.com/t/filter-ansible-devices-to-get-the-first-disk-without-partitions/44399 "2025-09-01T20:08:46Z")

</div>

My goal is to get the first disk in ansible\_devices without any partitions, to format it. Here is the output of ansible -m setup -a filter=ansible\_devices all, so in this case it’s sdb but it’s not always sdb. "ans…

---

## [Ansible output jinja2 loop to a different files](https://forum.ansible.com/t/ansible-output-jinja2-loop-to-a-different-files/42264)

<div class="topic-metadata">

**Author:** [@Shiraz\_Kamal](https://forum.ansible.com/u/Shiraz_Kamal)\
**Replies:** 2\
**Last updated:** [April 30, 2025, 11:28am UTC](https://forum.ansible.com/t/ansible-output-jinja2-loop-to-a-different-files/42264 "2025-04-30T11:28:10Z")

</div>

I have the following in my vars file. aux: "SHR" cluster\_nodes: - { node\_name: sudora123a, node\_num: 1, inst\_num: "123A" } - { node\_name: sudora123b, node\_num: 2, inst\_num: "123B" } I am trying to loop t…

---

## [Need basic help with jinja and ansible inventory](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239)

<div class="topic-metadata">

**Author:** [@wes](https://forum.ansible.com/u/wes)\
**Replies:** 6\
**Last updated:** [March 26, 2025, 4:36pm UTC](https://forum.ansible.com/t/need-basic-help-with-jinja-and-ansible-inventory/41239 "2025-03-26T16:36:29Z")

</div>

Hi, I’m trying to create a configuration for haproxy, that renders a haproxy.cfg.j2 jinja template, and populates it based on the ansible inventory. I’m having issues with jinja and iterating/loops. My Inventory looks l…

---

## [Why do variables for a template need top be defined 'above' the template task?](https://forum.ansible.com/t/why-do-variables-for-a-template-need-top-be-defined-above-the-template-task/41007)

<div class="topic-metadata">

**Author:** [@halloleo](https://forum.ansible.com/u/halloleo)\
**Replies:** 4\
**Last updated:** [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 "2025-03-13T23:18:00Z")

</div>

I want to use ansible.builtin.template. So I tried as a task: - name: Create cfg file ansible.builtin.template: src: path/to/my/template.j2 dest: /path/to/output/file.conf vars: my\_port: 1234 with t…

---

## [Inconsistent expression evaluation](https://forum.ansible.com/t/inconsistent-expression-evaluation/40816)

<div class="topic-metadata">

**Author:** [@vbotka](https://forum.ansible.com/u/vbotka)\
**Replies:** 6\
**Last updated:** [March 10, 2025, 5:20pm UTC](https://forum.ansible.com/t/inconsistent-expression-evaluation/40816 "2025-03-10T17:20:10Z")

</div>

The below expressions are valid YAML dictionaries {"socket": "test"} {socket: "test"} They become valid YAML strings if you quote them '{"socket": "test"}' '{socket: "test"}' When you put a substitution into the e…

---

## [How to generate a list of formatted strings](https://forum.ansible.com/t/how-to-generate-a-list-of-formatted-strings/40198)

<div class="topic-metadata">

**Author:** [@ianhinder](https://forum.ansible.com/u/ianhinder)\
**Replies:** 8\
**Last updated:** [February 12, 2025, 10:58pm UTC](https://forum.ansible.com/t/how-to-generate-a-list-of-formatted-strings/40198 "2025-02-12T22:58:09Z")

</div>

I would like to define a variable called test\_users with a list of test usernames: test\_users: - testuser\_001 - testuser\_002 ... - testuser\_100 I would prefer not to type them in by hand, or generate the list a…

---

## [Do I have too many Jinja magic going on in my inventory?](https://forum.ansible.com/t/do-i-have-too-many-jinja-magic-going-on-in-my-inventory/38931)

<div class="topic-metadata">

**Author:** [@Thulium-Drake](https://forum.ansible.com/u/Thulium-Drake)\
**Replies:** 2\
**Last updated:** [December 5, 2024, 10:14pm UTC](https://forum.ansible.com/t/do-i-have-too-many-jinja-magic-going-on-in-my-inventory/38931 "2024-12-05T22:14:29Z")

</div>

Hi folks, So here I was, coding away and minding my own business, when I noticed that one of my tasks (rendering apache config files from a list, but that’s not important) wasn’t running as expected… And it’s completel…

---

## [\`undef()\` cannot be used on dictionary members](https://forum.ansible.com/t/undef-cannot-be-used-on-dictionary-members/10879)

<div class="topic-metadata">

**Author:** [@pieterjanv](https://forum.ansible.com/u/pieterjanv)\
**Replies:** 10\
**Last updated:** [November 19, 2024, 9:32pm UTC](https://forum.ansible.com/t/undef-cannot-be-used-on-dictionary-members/10879 "2024-11-19T21:32:15Z")

</div>

undef() behaves unexpectedly when set to a dictionary member. I expected the following code to ensure that more or less foo == {}. - hosts: localhost gather\_facts: no vars: foo: bar: baz tasks: - var…

---

## [Unable to use Parentheses for Jinja2 functions in Conditionals](https://forum.ansible.com/t/unable-to-use-parentheses-for-jinja2-functions-in-conditionals/10843)

<div class="topic-metadata">

**Author:** [@AnthonyAnsible](https://forum.ansible.com/u/AnthonyAnsible)\
**Replies:** 3\
**Last updated:** [November 12, 2024, 5:41am UTC](https://forum.ansible.com/t/unable-to-use-parentheses-for-jinja2-functions-in-conditionals/10843 "2024-11-12T05:41:07Z")

</div>

Hello, I’m having problems using a Jinja2 function in an Ansible conditional and was hoping someone could please help me find a way around it. As far as I can tell I must use both parentheses and single quotes in the J…

---

## [Collect together the results of all hosts](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608)

<div class="topic-metadata">

**Author:** [@hieronymus](https://forum.ansible.com/u/hieronymus)\
**Replies:** 4\
**Last updated:** [October 31, 2024, 3:14pm UTC](https://forum.ansible.com/t/collect-together-the-results-of-all-hosts/10608 "2024-10-31T15:14:23Z")

</div>

Hi, How can I with ansible collect all results not only from one host but from all and put together in a file (template)? Here’s below my playbook and jinja2 config’s files : --- - name: Get all interfaces hosts: fe…

---

## [Using Jinja2 loops inside inventory variables files](https://forum.ansible.com/t/using-jinja2-loops-inside-inventory-variables-files/10275)

<div class="topic-metadata">

**Author:** [@N-Mi](https://forum.ansible.com/u/N-Mi)\
**Replies:** 3\
**Last updated:** [October 14, 2024, 6:05pm UTC](https://forum.ansible.com/t/using-jinja2-loops-inside-inventory-variables-files/10275 "2024-10-14T18:05:12Z")

</div>

Hi, I’m trying to organize my inventory variables, and I’m struggling with the syntax. I have an inventory structure similar to one suggested in documentation : admin@ansible:~/ansible/environments$ find . . ./000\_cro…

---

## [Jinja+facts :: how to get active interface](https://forum.ansible.com/t/jinja-facts-how-to-get-active-interface/8746)

<div class="topic-metadata">

**Author:** [@adriansev](https://forum.ansible.com/u/adriansev)\
**Replies:** 8\
**Last updated:** [October 10, 2024, 11:55am UTC](https://forum.ansible.com/t/jinja-facts-how-to-get-active-interface/8746 "2024-10-10T11:55:31Z")

</div>

I’m trying to get in a configuration file the active interface from facts … but i’m still wrong somewhere in the jinja code or in facts accessing … My tryout looks like this: # Network interface to monitor (default eth…

---

## [Using registered variable in a loop](https://forum.ansible.com/t/using-registered-variable-in-a-loop/8806)

<div class="topic-metadata">

**Author:** [@TheSpy0](https://forum.ansible.com/u/TheSpy0)\
**Replies:** 2\
**Last updated:** [September 27, 2024, 3:43am UTC](https://forum.ansible.com/t/using-registered-variable-in-a-loop/8806 "2024-09-27T03:43:06Z")

</div>

I’m trying to configure Molecule to use Azure’s Devtest Lab virtual machines for testing playbooks using the delegated|default driver. The current issue that I’m running into is getting the necessary data from a register…

---

## [Ansible-lint name\[template\] with multiple vars](https://forum.ansible.com/t/ansible-lint-name-template-with-multiple-vars/8530)

<div class="topic-metadata">

**Author:** [@falon](https://forum.ansible.com/u/falon)\
**Replies:** 5\
**Last updated:** [September 16, 2024, 6:35pm UTC](https://forum.ansible.com/t/ansible-lint-name-template-with-multiple-vars/8530 "2024-09-16T18:35:17Z")

</div>

Hello, I ask an help to avoid this error: name\[template\]: Jinja templates should only be at the end of 'name' roles/role\_name/tasks/main.yml:39 Task/Handler: Copy file in {{ role\_name\_conf.dest }} of {{ role\_name\_serve…

---

## [Nested Jinja2 templates and template\_module](https://forum.ansible.com/t/nested-jinja2-templates-and-template-module/7421)

<div class="topic-metadata">

**Author:** [@yokodzun](https://forum.ansible.com/u/yokodzun)\
**Replies:** 9\
**Last updated:** [August 6, 2024, 7:38am UTC](https://forum.ansible.com/t/nested-jinja2-templates-and-template-module/7421 "2024-08-06T07:38:19Z")

</div>

I am using Ansible as part of my GitLab CI/CD pipeline, and I have separate playbooks for rendering configs for my app and Nginx. The folder structure looks like this ├── XXX/YYY/playbook.yml ├── templates │…

---

## [How to use an inventory variable in a lookup of aws secret?](https://forum.ansible.com/t/how-to-use-an-inventory-variable-in-a-lookup-of-aws-secret/7255)

<div class="topic-metadata">

**Author:** [@PB70](https://forum.ansible.com/u/PB70)\
**Replies:** 7\
**Last updated:** [July 16, 2024, 6:19pm UTC](https://forum.ansible.com/t/how-to-use-an-inventory-variable-in-a-lookup-of-aws-secret/7255 "2024-07-16T18:19:21Z")

</div>

I am trying to lookup an aws\_secret using the lookup functionality, but I want the path to the secret I lookup to come from an inventory variable. That way I can have a different secret for each environment (dev, stage,…

---

## [Get key from a list of dict](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799)

<div class="topic-metadata">

**Author:** [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Replies:** 4\
**Last updated:** [June 27, 2024, 8:11am UTC](https://forum.ansible.com/t/get-key-from-a-list-of-dict/6799 "2024-06-27T08:11:55Z")

</div>

Hello, from the xml module I get a list of dict of interfaces: TASK \[Show result\] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ok: \[opnsense\] =\> { "iface\_cur…

---

## [Filter a list of dict](https://forum.ansible.com/t/filter-a-list-of-dict/6793)

<div class="topic-metadata">

**Author:** [@DengelFred](https://forum.ansible.com/u/DengelFred)\
**Replies:** 1\
**Last updated:** [June 26, 2024, 7:04pm UTC](https://forum.ansible.com/t/filter-a-list-of-dict/6793 "2024-06-26T19:04:45Z")

</div>

Hello! I have a list of dicts with network definitions for an opnsense: networks\_test: - descr: UpLink iface: wan vlan\_id: '7' - descr: MgmtNet iface: lan vlan\_id: '40' - descr: HomeNet iface…

---

## [Variable considered undefined if it contains undefined values](https://forum.ansible.com/t/variable-considered-undefined-if-it-contains-undefined-values/5908)

<div class="topic-metadata">

**Author:** [@Rens\_Sikma](https://forum.ansible.com/u/Rens_Sikma)\
**Replies:** 10\
**Last updated:** [May 22, 2024, 11:02am UTC](https://forum.ansible.com/t/variable-considered-undefined-if-it-contains-undefined-values/5908 "2024-05-22T11:02:02Z")

</div>

If a variable exist, but contains undefined value. Its considered undefined. In the past this would create a error. Is this a bug? and if it works as intended is there a way to get the old behavior? problem For examp…

[Next page](https://forum.ansible.com/tag/jinja2/173.md?match_all_tags=true&page=1&tags%5B%5D=jinja2)
