繁体   English   中英

出现错误 - 使用 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 %}"}

我知道我做错了,但不确定调试任务中的错误是什么/在哪里?

预期 output:

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 }}"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM