using regex in jinja 2 for ansible playbooks

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

    There is a "regex_replace" filter available in Ansible>1.6

    Other Useful Filters Scroll down and you'll see this:

    New in version 1.6.

    To replace text in a string with regex, use the “regex_replace” filter:

    # convert "ansible" to "able"
    {{ 'ansible' | regex_replace('^a.*i(.*)$', 'a\\1') }}
    
    # convert "foobar" to "bar"
    {{ 'foobar' | regex_replace('^f.*o(.*)$', '\\1') }}
    
    # convert "localhost:80" to "localhost, 80" using named groups
    {{ 'localhost:80' | regex_replace('^(?P.+):(?P\\d+)$', '\\g, \\g') }}
    

    That being said, regex is overkill for finding a solution to this particular problem.

提交回复
热议问题