using regex in jinja 2 for ansible playbooks

后端 未结 7 2134
北海茫月
北海茫月 2020-12-31 06:37

HI i am new to jinja2 and trying to use regular expression as shown below

{% if ansible_hostname == \'uat\' %}
   {% set server = \'thinkingmonster.com\' %}
         


        
7条回答
  •  梦谈多话
    2020-12-31 06:59

    Jinja2 can quite easily do substr checks with a simple 'in' comparison, e.g.

    {% set server = 'www.thinkingmonster.com' %}
    {% if 'thinking' in server %}
       do something...
    {% endif %}
    

    So your substring regex filter isn't required. However if you want more advanced regex matching, then there are in fact filters available in ansible - see the regex filters in http://docs.ansible.com/playbooks_filters.html#other-useful-filters - funnily enough, your match syntax above is nearly exactly right.

    +1 for Bereal's answer though, it gives a nice alternative in the form of a map.

提交回复
热议问题