我正在尝试在Jinja2中使用python执行for循环,该循环将创建并附加到一个列表中——但是我认为我可能会有语法问题,因为在原生python中工作正常的代码在Jinja2模板中失败。我想得到一些帮助,看看我可能做错了什么
在较高级别上,代码将使用aws收集的子网列表收集事实,使用单独yaml文件中提供的数字来确定附加到单独列表中的子网的数量,以便for循环运行并填充单独的yaml文件。你知道吗
{% set subnets_to_use = [] %}
{% set number_of_subnets = {{ cluster.master }} %} <-- #this value is set in another yaml file
{% set list = usable_kops_subnets %} <-- this has been set by gather facts seperately
{% set list_len = list | length %}
{% for i in range(number_of_subnets) %}
{{ subnets_to_use.append(list[(i)%list_len]) }} <-- #appears to fail here
{% endfor %}
{% for etcd_host_id in subnets_to_use[:cluster.master] %}
- instanceGroup: master-{{ etcd_host_id.availability_zone }}-{{ loop.index }}
name: {{ etcd_host_id.availability_zone }}-{{ loop.index }}
{% endfor %}
"msg": "AnsibleError: template error while templating string: expected token ':', got '}'
当我在Python编译器中运行类似的Python代码时,代码似乎可以工作
例如:
x = 400
list = ["string1","str2","str3","str4"]
subnets_to_use = []
list_len = len(list)
for i in range(x):
subnets_to_use.append(list[(i)%list_len])
print (subnets_to_use)
看起来你把注意力放错了方向。更改此行
为了这个
然后就可以了。你知道吗
我最终在ansible中使用了以下内容,而不是使用python—似乎很管用。在这种情况下clusters.node节点定义为3,我有一个子网。我想要创建3个实例组,所有实例组都具有相同的子网。你知道吗
然后对“slist\u nodes”列表运行循环,例如
这将创建3个子网实例。你知道吗
相关问题 更多 >
编程相关推荐