using regex in jinja 2 for ansible playbooks

后端 未结 7 2105
北海茫月
北海茫月 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:44

    To my knowledge, there is no builtin filter for that in Jinja2 neither among Ansible's extra filters, but it's not a big deal to make your own:

    certs = {'.*thinking.*': 'akash', '.*sleeping.*': 'akashthakur'}
    def map_regex(value, mapping=certs):
        for k, v in mapping.items():
            if re.match(k, value):
                return v 
    

    Then you'll need to add a filter plugin to Ansible, so that it will use the function above in templates (like {{server|ssl_cert}} if you name the filter ssl_cert).

    That said, a plain old function or a plain old dictionary that is passed to the templates and used there explicitly might fit this job better.

提交回复
热议问题