Closed BalintSzigeti closed 6 years ago
I think the jinja rendering in your issue is separate from targeting. I don't think jinja2 supports regex matching natively. You'd need to write a custom jinja filter for that (or use another templating engine such as Mako).
cheers, Tim
@BalintSzigeti, also your jinja logic tests whether the id grain is literally host(1|2)c
or host[1|2]c
. In order to get pattern matching you will need to look into the options @tbaker57 has mentioned.
As my salt mentor (hi, @whiteinge! :beer: :) told me on several occasions, beyond very basic logic, you'll be miles ahead to do the heavy lifting in python.
I have a couple custom execution modules that hold nothing but utility functions for use within state files. So, something along the lines of:
/srv/salt/_modules/tplutils.py
import re
def __virtual__():
return __name__
def rematch(pattern, target):
"""
(because Jinja makes me want to cut myself)
args:
regex pattern (str)
target text (str)
"""
return True if re.match(r'{}'.format(pattern), target) else False
could then be called like
/etc/ntp.conf:
file.managed:
- source:
{% if salt.tplutils.rematch(grains['id'], 'host(1|2)c') %}
- salt://ntp/files/ntp.conf.server
{% else %}
- salt://ntp/files/ntp.conf.{{ grains['fqdn'] }}
- salt://ntp/files/ntp.conf.default
{% endif %}
- user: root
- group: root
- mode: 644
@gladiatr72 (hi!) is right that util functions are often preferable to Jinja logic, particularly inline Jinja. (Speaking of which, your general-use rematch
function would be a very nice addition to this recent module addition, wink, wink, :beer: nudge, nudge.)
@BalintSzigeti Since you're doing a regex match against a minion ID there is an existing util function in Salt's targeting module that will work for this use-case. This should work:
{% default_sources = [
'salt://ntp/files/ntp.conf.'~ grains['fqdn'],
'salt://ntp/files/ntp.conf.default',
] %}
{% set sources = salt.match.filter_by({
'host(1|2)c': ['salt://ntp/files/ntp.conf.server']
}, expr_form='pcre') or default_sources %}
/etc/ntp.conf:
file.managed:
- source: {{ sources | json() }}
- user: root
- group: root
- mode: 644
hello
unfortunately it doesn't work:
host2c:
Data failed to compile:
----------
Rendering SLS 'live:ntp' failed: Jinja syntax error: unknown tag 'default_sources'; line 1
---
{% default_sources = [ <======================
'salt://ntp/files/ntp.conf.'~ grains['fqdn'],
'salt://ntp/files/ntp.conf.default',
] %}
{% set sources = salt.match.filter_by({
.....
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.
/etc/ntp.conf: file.managed:
hello
according to this: https://docs.saltstack.com/en/latest/topics/targeting/globbing.html
this state should work:
but it return with ntp.conf.default regardless the state was executed on host1c or host2c.
salt version: salt-2015.8.5-1.el6.noarch