我正在运行Ansible剧本。在YAML文件“ jinja.yaml”中,我具有以下jinja2模板。
{% set cnt = 0 %}
{% for x in range(4, 20) %}
{% for y in range(1, 251) %}
- pool_name: pool_{{ cnt }}
{% set cnt = cnt + 1 %}
pool_member: 10.30.{{ x }}.{{ y }}
{% endfor %} {% endfor %}
以下是Ansilbe代码中的代码段:
tasks:
- name: XXX
set_fact:
members: "{{ lookup('template', 'jinja.yaml') | from_yaml}}"
运行Ansible剧本时,出现以下错误消息:
return loader.get_single_data()
File "/usr/lib/python2.7/dist-packages/yaml/constructor.py", line 37, in get_single_data
node = self.get_single_node()
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 36, in get_single_node
document = self.compose_document()
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 55, in compose_document
node = self.compose_node(None, None)
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 82, in compose_node
node = self.compose_sequence_node(anchor)
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 111, in compose_sequence_node
node.value.append(self.compose_node(node, index))
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 84, in compose_node
node = self.compose_mapping_node(anchor)
File "/usr/lib/python2.7/dist-packages/yaml/composer.py", line 127, in compose_mapping_node
while not self.check_event(MappingEndEvent):
File "/usr/lib/python2.7/dist-packages/yaml/parser.py", line 98, in check_event
self.current_event = self.state()
File "/usr/lib/python2.7/dist-packages/yaml/parser.py", line 428, in parse_block_mapping_key
if self.check_token(KeyToken):
File "/usr/lib/python2.7/dist-packages/yaml/scanner.py", line 116, in check_token
self.fetch_more_tokens()
File "/usr/lib/python2.7/dist-packages/yaml/scanner.py", line 220, in fetch_more_tokens
return self.fetch_value()
File "/usr/lib/python2.7/dist-packages/yaml/scanner.py", line 576, in fetch_value
self.get_mark())
ScannerError: mapping values are not allowed here
in "<unicode string>", line 2, column 32:
pool_member: 10.30.4.1
^
fatal: [10.6.177.160]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
我有99%的把握是jinja.yaml中的语法错误,但我只是没有解决。任何帮助都会 欣赏它。
答案 0 :(得分:1)
您正在解决错误的问题;如果您想要list[dict]
,则不要尝试使用jinja模板来构建yaml文本,而只是稍后将其转换为所需的实际数据结构:只需构建所需的list[dict]
,而无需中间序列化:
- name: XXX
set_fact:
members: >-
{%- set cnt_holder = {"cnt": 0} -%}
{%- set results = [] -%}
{%- for x in range(4, 20) -%}
{%- for y in range(1, 251) -%}
{%- set _ = results.append({
"pool_name": ("pool_%d" | format(cnt_holder.cnt)),
"pool_member": ("10.30.%d.%d"|format(x, y)),
}) -%}
{%- set _ = cnt_holder.update({"cnt": cnt_holder.cnt + 1}) -%}
{%- endfor -%}
{%- endfor -%}
{{ results }}
正如您将看到的那样,您的原始代码块有一个错误:无法重新分配变量(这种情况在for
循环之类的词法块中成倍增加); jinja2的“功能”实际上记录在the fine manual
但是,您可以更改现有的数据结构(实际上,这就是results.append
的重点)。因此,我们回避了jinja2的set
行为,并将可变的cnt
存储在全局字典中,以便我们 对其进行突变。这就是为什么我的{% set cnt_holder
与您的语法不同