获取错误 - 使用 jinja2 的 Ansible 调试消息 - 模板化字符串时出现模板错误:需要一个表达式,语句块结束
Getting error - Ansible debug message using jinja2 - template error while templating string: Expected an expression, got end of statement block
我将下面的任务结果注册到“输出”变量
TASK [bgp status] ****************************************************************************************************************************************************************************
ok: [4.4.4.4] => {
"msg": [
{
"bgp_state": "Established",
"neighbor": "1.1.1.1",
},
{
"bgp_state": "Down",
"neighbor": "2.2.2.2",
},
{
"bgp_state": "Established",
"neighbor": "3.3.3.3",
}
]
}
我的 debus 任务是这样的:
- name: bgp status
debug:
msg:
- "{% if output.msg[0].bgp_state == Established %}{{output.msg[0].neighbor}} BGP IS UP{% elif %}{{output.msg[0].neighbor}}BGP IS DOWN{%- endif %}"
- "{% if output.msg[1].bgp_state == Established %}{{output.msg[1].neighbor}}BGP IS UP{% elif %}{{output.msg[1].neighbor}}BGP IS DOWN{%- endif %}"
- "{% if output.msg[2].bgp_state == Established %}{{output.msg[2].neighbor}}BGP IS UP{% elif %}{{output.msg[2].neighbor}}BGP IS DOWN{%- endif %}"
错误:
fatal: [4.4.4.4]: FAILED! => {"msg": "template error while templating string: Expected an expression, got 'end of statement block'. String: {% if output.msg[0].bgp_state == Established %}{{output.msg[0].neighbor}}BGP IS UP{% elif %}{{output.msg[0].neighbor}}BGP IS DOWN{%- endif %}"}
我知道我做错了但不确定 what/where 是我的调试任务中的错误吗?
预期输出:
1.1.1.1 BGP IS UP
2.2.2.2 BGP IS DOWN
3.3.3.3 BGP IS UP
您写了:
{% elif %}{{output.msg[0].neighbor}}BGP IS DOWN{%- endif %}
但我认为你的意思是 else
。 elif
需要条件表达式,就像 if
一样。此外,您需要在 if
表达式中引用字符串值。
解决这两个问题后,我们得到:
- name: bgp status
debug:
msg:
- '{% if output.msg[0].bgp_state == "Established" %}{{output.msg[0].neighbor}} BGP IS UP{% else %}{{ output.msg[0].neighbor }} BGP IS DOWN{%- endif %}'
- '{% if output.msg[1].bgp_state == "Established" %}{{output.msg[1].neighbor}} BGP IS UP{% else %}{{ output.msg[1].neighbor }} BGP IS DOWN{%- endif %}'
- '{% if output.msg[2].bgp_state == "Established" %}{{output.msg[2].neighbor}} BGP IS UP{% else %}{{ output.msg[2].neighbor }} BGP IS DOWN{%- endif %}'
给定样本输入,这将产生:
TASK [bgp status] ****************************************************************************
ok: [localhost] => {
"msg": [
"1.1.1.1 BGP IS UP",
"2.2.2.2 BGP IS DOWN",
"3.3.3.3 BGP IS UP"
]
}
如果我正在写这篇文章,我可能会重新格式化内容以提高可读性并将任务置于循环中:
- name: bgp status
debug:
msg:
- >-
{% if item.bgp_state == "Established" %}
{{item.neighbor}} BGP IS UP
{% else %}
{{ item.neighbor }} BGP IS DOWN
{%- endif %}
loop: "{{ output.msg }}"
我将下面的任务结果注册到“输出”变量
TASK [bgp status] ****************************************************************************************************************************************************************************
ok: [4.4.4.4] => {
"msg": [
{
"bgp_state": "Established",
"neighbor": "1.1.1.1",
},
{
"bgp_state": "Down",
"neighbor": "2.2.2.2",
},
{
"bgp_state": "Established",
"neighbor": "3.3.3.3",
}
]
}
我的 debus 任务是这样的:
- name: bgp status
debug:
msg:
- "{% if output.msg[0].bgp_state == Established %}{{output.msg[0].neighbor}} BGP IS UP{% elif %}{{output.msg[0].neighbor}}BGP IS DOWN{%- endif %}"
- "{% if output.msg[1].bgp_state == Established %}{{output.msg[1].neighbor}}BGP IS UP{% elif %}{{output.msg[1].neighbor}}BGP IS DOWN{%- endif %}"
- "{% if output.msg[2].bgp_state == Established %}{{output.msg[2].neighbor}}BGP IS UP{% elif %}{{output.msg[2].neighbor}}BGP IS DOWN{%- endif %}"
错误:
fatal: [4.4.4.4]: FAILED! => {"msg": "template error while templating string: Expected an expression, got 'end of statement block'. String: {% if output.msg[0].bgp_state == Established %}{{output.msg[0].neighbor}}BGP IS UP{% elif %}{{output.msg[0].neighbor}}BGP IS DOWN{%- endif %}"}
我知道我做错了但不确定 what/where 是我的调试任务中的错误吗?
预期输出:
1.1.1.1 BGP IS UP
2.2.2.2 BGP IS DOWN
3.3.3.3 BGP IS UP
您写了:
{% elif %}{{output.msg[0].neighbor}}BGP IS DOWN{%- endif %}
但我认为你的意思是 else
。 elif
需要条件表达式,就像 if
一样。此外,您需要在 if
表达式中引用字符串值。
解决这两个问题后,我们得到:
- name: bgp status
debug:
msg:
- '{% if output.msg[0].bgp_state == "Established" %}{{output.msg[0].neighbor}} BGP IS UP{% else %}{{ output.msg[0].neighbor }} BGP IS DOWN{%- endif %}'
- '{% if output.msg[1].bgp_state == "Established" %}{{output.msg[1].neighbor}} BGP IS UP{% else %}{{ output.msg[1].neighbor }} BGP IS DOWN{%- endif %}'
- '{% if output.msg[2].bgp_state == "Established" %}{{output.msg[2].neighbor}} BGP IS UP{% else %}{{ output.msg[2].neighbor }} BGP IS DOWN{%- endif %}'
给定样本输入,这将产生:
TASK [bgp status] ****************************************************************************
ok: [localhost] => {
"msg": [
"1.1.1.1 BGP IS UP",
"2.2.2.2 BGP IS DOWN",
"3.3.3.3 BGP IS UP"
]
}
如果我正在写这篇文章,我可能会重新格式化内容以提高可读性并将任务置于循环中:
- name: bgp status
debug:
msg:
- >-
{% if item.bgp_state == "Established" %}
{{item.neighbor}} BGP IS UP
{% else %}
{{ item.neighbor }} BGP IS DOWN
{%- endif %}
loop: "{{ output.msg }}"