How to use an OrderedDict Jinja2 variable in my Salt state?

430 views
Skip to first unread message

Nicholas Wylie

unread,
Apr 10, 2018, 8:34:20 AM4/10/18
to Salt-users

I'm trying to write a Jinja macro that will allow me to template a file configuration and I'm hoping someone kind provide me with some advice.


The configuration file itself requires certain settings to appear in order. I would like to be able to pass a dict as an optional variable to my macro so that I can override the default settings in the configuration file as well as pass non-default settings.


For example, my default settings might look like this:

{% set default = { 'default_setting': 'default_value', 'another_default_setting': 'another_default_value', } %}

And my new settings:

{% set new = { 'default_setting': 'new_value', 'new_setting': 'another_new_value', } %}

Then in my template:

{% set settings = salt['slsutil.merge'](default, new, 'overwrite') %}
{% for setting, value in settings.items() %}
{{ setting }} = {{ value }};
{% endfor %}

Which should output:

default_setting = new_value;
another_default_setting
= another_default_value;
new_setting
= another_new_value;

Is this possible to do or am I heading in the wrong direction?

Dafydd Jones (techneg.it)

unread,
Apr 10, 2018, 9:46:20 AM4/10/18
to salt-...@googlegroups.com
Have you looked at the salt-formulas github repository. Many examples there of merging default values and custom values based on OS or pillar.

HTH

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/3cd747ce-083f-41eb-8fd9-e6078231884f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nicholas Wylie

unread,
Apr 10, 2018, 11:33:53 PM4/10/18
to Salt-users
Merging the default and custom values isn't my problem. As you said this is fairly well documented. It is preserving the order of the data that I am struggling with.


On Tuesday, April 10, 2018 at 11:46:20 PM UTC+10, Dafydd Jones wrote:
Have you looked at the salt-formulas github repository. Many examples there of merging default values and custom values based on OS or pillar.

HTH
On 10 April 2018 at 04:05, Nicholas Wylie <now...@gmail.com> wrote:

I'm trying to write a Jinja macro that will allow me to template a file configuration and I'm hoping someone kind provide me with some advice.


The configuration file itself requires certain settings to appear in order. I would like to be able to pass a dict as an optional variable to my macro so that I can override the default settings in the configuration file as well as pass non-default settings.


For example, my default settings might look like this:

{% set default = { 'default_setting': 'default_value', 'another_default_setting': 'another_default_value', } %}

And my new settings:

{% set new = { 'default_setting': 'new_value', 'new_setting': 'another_new_value', } %}

Then in my template:

{% set settings = salt['slsutil.merge'](default, new, 'overwrite') %}
{% for setting, value in settings.items() %}
{{ setting }} = {{ value }};
{% endfor %}

Which should output:

default_setting = new_value;
another_default_setting
= another_default_value;
new_setting
= another_new_value;

Is this possible to do or am I heading in the wrong direction?

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.

Matthew Phillips

unread,
Apr 12, 2018, 6:57:05 PM4/12/18
to salt-...@googlegroups.com
Hi,
OrderedDict is actually exposed as a global in the jinja renderer here:
https://github.com/saltstack/salt/blob/develop/salt/utils/templates.py#L365
so I think you would be able to use odict() inside a jinja context to create ordered dicts that act as you expect. Haven't tested this though, so this might not work as I'm expecting :)

Cheers

Reply all
Reply to author
Forward
0 new messages