Ansible 在模板中的字母范围内循环 [英] Ansible loop over range of letters in template
本文介绍了Ansible 在模板中的字母范围内循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试生成一个 Ansible 模板,该模板按字母顺序而不是数字递增.有没有类似于 range(x)
的函数可以帮助我?
I'm trying to generate an Ansible template that increments on letters alphabetically rather than numbers. Is there a function similar to range(x)
that could help me?
伪代码示例
{% for letter in range(a, d) %}
{{ letter }}
{% endfor %}
预期输出
a
b
c
d
或者,有没有办法在 Ansible 中将数字转换为对应的字母顺序?
Alternatively is there a way to convert a number into it's alphabetical equivalent in Ansible?
{% for i in range(6) %}
{{ convert(i) }}
{% endfor %}
更新
对于那些好奇的人,以下是我最终应用@zigam 解决方案的方式.目标是为主机组中的每个主机创建 xml 标记.
UPDATE
For those who are curious, here's how I ended up applying @zigam's solution. The goal was to create xml tags with every host from a hostgroup.
在我的角色中默认:
ids: "ABCDEFGHIGJKLMNPQRSTUVWXYZ"
在我的模板中:
{% for host in groups['some_group'] %}
<host-id="{{ ids[loop.index] }}" hostName="{{ host }}" port="8888" />
{% endfor %}
推荐答案
您可以遍历字符串:
{% for letter in 'abcd' %}
{{ letter }}
{% endfor %}
如果你想遍历一个范围的字母表:
If you want to iterate over a range of the alphabet:
{% set letters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' %}
{% for letter in letters[:6] %} {# first 6 chars #}
{{ letter }}
{% endfor %}
这篇关于Ansible 在模板中的字母范围内循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文