2
votes

I would like to do some basic pillar value settings for all boxes so that I can use them later in a unified way. Our minions are usually named in this format:

<project>-<env>.<role>-<sequence>.<domain>

Example pillar/base/top.sls:

base:
    '*':
        - basics
    'I@project:mycoolproject and I@role:nginx':
        - etc.

Example pillar/base/basics/init.sls:

{% if '-live.' in grains['id'] %}
env: production
{% elif '-qa.' in grains['id'] %}
env: qa
{% elif '-staging.' in grains['id'] %}
env: staging
{% else %}
env:
{% endif %}

{% set role = re.match("(?:live|qa|staging)\.([a-z_\-]+)\-', grains['id']).group(1) -%}
role: {{ role }}

The env part obviously works but I can't get the regex working. As far as I understood there is no way to import python module (i.e. import re) in jinja template. Any suggestions how to get regex functionality available in the pillar file if possible at all?

1

1 Answers

0
votes

The simple answer is, "no". There is not a way to inject regex functionality directly into the jinja environment (I'm sure there's a way to extend jinja, but anyway..)

The way I addressed this was with an external module function, id_info.explode() and an external pillar.

Enable external modules on the master:

external_modules: /srv/extmod

External modules do not require any sort of special infrastructure--they are just regular python modules (not packages, mind you--the loader doesn't currently know how to properly side-load a package yet)

Put your python+regex logic there. Return a dictionary, assembled to your your liking.

Your external module would go in /srv/extmod/modules. You can call call this function from your pillar.sls

{% id_info = __salt__[id_info.explode()] -%}
{% subcomponent = id_info['subcomponent'] -%}
{% project = id_info['project'] -%}

etc...

A couple things to know:

  • The salt-master has to be restarted when an external module is added or modified. There isn't a way that I know of to incite the equivalent of a saltutil.refresh_modules() call on the salt-master, so there ya go.
  • The external_modules directive is not just for execution modules. In this scenario, you would also create /srv/extmod/{pillar,runners,outputers,etc}.
  • These modules are only available on the master