0

I would like to have less conditions in my saltstack sls files.

Up to now it looks like this:

foo_package:
  pkg.installed:
    - pkgs:  
  {% if grains.os_family == 'Debian' %}
      - foo_BAR
  {% else %}
      - foo-bar
  {% endif %}

I would like to have it like this:

foo_package:
  pkg.installed:
    - pkgs:  
      - {{ foo_BAR | normalize_package_name }}

How to implement normalize_package_name?

As soon as I can use Python I can help myself and implement this very easily: Underscores should get replaced with - and upper case characters should get lower case.

I know that I could do it in Jinja like this:

- {{ foo_BAR | replace... | replace .. }}

But this gets used several times, that's why I would like to have a custom filter.

.... some days later: Maybe it is better to write a custom state (mypkg.installed) and call pkg.installed after normalizing the package names. See salt docs: https://docs.saltstack.com/en/latest/ref/states/writing.html#cross-calling-state-modules

guettli
  • 3,113
  • 14
  • 59
  • 110

1 Answers1

1

In your case I think the best is to use the possibilities offered by 'formulas'. You can transform your standalone states in a formula and use a map.jinja to specify the package name depending of the OS or other grains values.

As an example you can look on https://github.com/saltstack-formulas/template-formula/ which implements it. Look at template/map.jinja and template/defaults.yaml and template/os*.yaml files

daks
  • 673
  • 6
  • 23