Regex Matching within State (Jinja)?

974 views
Skip to first unread message

Jay Ambi

unread,
Apr 18, 2017, 8:21:22 PM4/18/17
to Salt-users

trying to create a state to configure filebeat for 20+ servers based on their minion id. for the sake of simplicity ive only included 3 of the types of server names. the following works, but its not ideal because it requires the exact minion id.


/etc/filebeat/filebeat.yml:

  file.managed:

    {% if grains[‘id]’ == “prod-varnish-01” %}

    - source: salt://files/filebeat/varnish.yml

    {% elif grains['id'] == “prod-nginx-01” %}

    - source: salt://files/filebeat/nginx.yml

    {% elif grains['id'] == “prod-api-01” %}

    - source: salt://files/filebeat/api.yml

    {% else %}

    - source: salt://files/filebeat/default.yml

    {% endif %}

    - user: root

    - group: root

    - mode: '644'

    - template: jinja


i’d like it to work in more of a regex kinda way such as:


/etc/filebeat/filebeat.yml:

  file.managed:

    {% if varnish in grains['id'] %}

    - source: salt://files/filebeat/varnish.yml

    {% elif nginx in grains['id'] %}

    - source: salt://files/filebeat/nginx.yml

    {% elif api in grains['id'] %}

    - source: salt://files/filebeat/api.yml

    {% else %}

    - source: salt://files/filebeat/default.yml

    {% endif %}

    - user: root

    - group: root

    - mode: '644'

    - template: jinja


with python string matching, but that doesn’t work and for the life of me i havent had any luck.


how would i achieve what i need?

tim.h...@gmail.com

unread,
Apr 19, 2017, 4:45:35 AM4/19/17
to Salt-users

    {% if varnish in grains['id'] %}

    - source: salt://files/filebeat/varnish.yml

    {% elif nginx in grains['id'] %}

    - source: salt://files/filebeat/nginx.yml


I think jinja will try to interpret those parameters as jinja variables rather than strings.

Does it help if you do:

 
  {% if 'varnish' in grains['id'] %}

instead ?

cheers
Tim
Reply all
Reply to author
Forward
0 new messages