问题描述
{% if node_count is defined %}
{% set cnt = node_count|int + 1 %}
{% for i in range(cnt) %}
localhost00{{ i + 1 }}.local
{% endfor %}
{% endif %}
我想像打印
localhost001.local
localhost002.local
.
.
localhost010.local
我知道这不是我的正确方法,当计数达到10时,其打印localhost0010.local
,我希望它打印localhost010.local
对此有所帮助。
解决方法
您要使用格式打印出前导零。您可以这样做:
{% set node_count = 10 %}
{% if node_count is defined %}
{% set cnt = node_count|int + 1 %}
{% for i in range(cnt) %}
localhost{{ '%03d' % (i + 1) }}<br>
{% endfor %}
{% endif %}
只需将3更改为所需的任何“数字”即可。
输出:
localhost001
localhost002
localhost003
localhost004
localhost005
localhost006
localhost007
localhost008
localhost009
localhost010
localhost011