本文介绍了模板中字母范围的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循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!