Python – Saltstack iterate through second level pillar data

pythonsaltstack

I might be missing something or this might just be the wrong way to layout pillar data.

I want to iterate through the second level of pillar data.

Pillar example:

repo:
     Option1:
        version1:
            display_name: "abcde"
        version2:
            display_name: "fghij"
     Option2:
        version1:
            display_name: "klmn"`

Sls:

{% for version, versioninfo in salt['pillar.get']('repo', {}).iteritems() %}
{{ versioninfo[] }}{{ versioninfo[]['display_name'] }}
{% endfor %}`

I want to return each version and display name, effectively ignoring the first level of pillar data.
Obviously the above jinja does not work, but is there a way to do this?

Best Answer

You need two levels of iteration. .iteritems() returns the key and the values (items) for that key. Your first iteration will give you access to option names and versions. Your second iteration will give you the display_name.

{% for option, versions in salt['pillar.get']('repo', {}).iteritems() %}
  {% for version, info in versions.iteritems() %}
    {{ version }}:{{ info['display_name'] }} 
  {% endfor %}
{% endfor %}

OR you could do this:

{% for option, versions in salt['pillar.get']('repo', {}).iteritems() %}
  {% for version in versions %}
    {{ version }}:{{ versions[version]['display_name'] }} 
  {% endfor %}
{% endfor %}
Related Topic