The original use-case for using a lookup table was to get conditional
logic out of Jinja. As you pointed out, it tends to be very illegible.
It also is fairly limited in functionality (a design choice in Jinja)
and has restrictive variable scoping rules. The ``{% set %}`` in your
for-loop is defining a _new_ var that is scoped to inside the loop
which is why it's not available outside the loop. (A common hack
around this is to modify an outside dictionary/list var from inside a
loop.) Also there is no matching/globbing support in Jinja.
I would suggest creating the lookup table in the most ideal format for
the data you're trying to match, then writing a custom ``filter_by``
function in Python to filter the information down to what you need.
That keeps the Jinja clean, keeps logic out of Jinja, and lets you
customize how you match. It's easy to do:
1. mkdir -p /srv/salt/_modules
2. Edit /srv/salt/_modules/myutil.py and add a ``filter_by`` function.
3. Sync the module to all minions with ``salt '*' saltutil.sync_modules``.
4. Use it from Jinja with: ``salt['myutil.filter_by'](arg1, arg2)``.
If you want to use globbing you can use the fnmatch module in Python.
Something like:
# This is /srv/salt/_modules/mytil.py
import fnmatch
def filter_by(lookup_table, match):
[...]
for key in lookup_table.keys():
if fnmatch.fnmatch(key, match):
return lookup_table[key]
The above doesn't take ordering or duplicate matches into account. It
might be used like this:
{% set file_path = salt['myutil.filter_by'](version_paths, '11.0*') %}
There are two 'filter_by' functions that ship with Salt if you need
more inspiration:
https://github.com/saltstack/salt/blob/23fbd41/salt/modules/grains.py#L349
https://github.com/saltstack/salt/blob/23fbd41/salt/modules/match.py#L293
Good luck!