在使用ansible进行部署时,有一种特定的情况,我需要剥离尾随-p
子字符串的字符串。
仅当somemachine-prod-p
在末尾时,字符串somemachine-prod
才应成为-p
。
我看到可以与Jinja一起使用的子字符串函数无法满足我的需求,因为我需要剥离字符串的末尾而不是开始的末尾。
有想法吗?
最佳答案
找到了。
如果有人想知道:
{% if name.endswith('-p') %}
{{ name[:-2] }}
{% else %}
{{ name }}
{% endif %}
关于substring - Jinja2模板中的条件子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41791055/