saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Install Salt from the Salt package repositories here:
https://docs.saltproject.io/salt/install-guide/en/latest/
Apache License 2.0
14.18k stars 5.48k forks source link

state minion regexp doesn't work - CentOS 6 #31123

Closed BalintSzigeti closed 6 years ago

BalintSzigeti commented 8 years ago

hello

according to this: https://docs.saltstack.com/en/latest/topics/targeting/globbing.html

this state should work:

/etc/ntp.conf:
  file.managed:
    - source:
      {% if grains['id'] == 'host(1|2)c' %}
      - salt://ntp/files/ntp.conf.server
      {% elif grains['id'] != 'host[1|2]c' %}
      - salt://ntp/files/ntp.conf.{{ grains['fqdn'] }}
      - salt://ntp/files/ntp.conf.default
      {% endif %}
    - user: root
    - group: root
    - mode: 644

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

tbaker57 commented 8 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

jfindlay commented 8 years ago

@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.

gladiatr72 commented 8 years ago

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
whiteinge commented 8 years ago

@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
BalintSzigeti commented 8 years ago

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({
.....
stale[bot] commented 6 years ago

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.

ist0ne commented 6 years ago

/etc/ntp.conf: file.managed: