It seems Jinja2 (as supported in Saltstack) doesn't support regex matching, unless I'm missing something? A number of frameworks that leverage Jinja2, such as Ansible have custom support for regex filters. Elsewhere people are directed to write custom filters.
Some questions for those in the know, would probably help understand this state of affairs:
Even if its old this is still the top post when googling for salt match regex. In 2018.3.3 it is possible to write following code:
{% if "abc" is match("*b*") %}
...
{% endif %}
The relevant documentation link: https://docs.saltstack.com/en/latest/topics/jinja/index.html#match
This is an oldish post, but I came across it and to help other people that do the same..
SaltStack does now have regex_search and regex_match as of 2017.7.0:
https://docs.saltstack.com/en/latest/topics/jinja/index.html#regex-search
You can match both State and Pillar files like this,
for example, Top.sls
base:
'*':
- servers.{{ grains.id }}
{% if grains.id | regex_match('nyc(.*)', ignorecase=True) %}
- data.region.nyc
{% endif %}
- ignore_missing: True
any node matching nyc* will get these pillar values. This also works inside State files (tested on 2018.3.4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With