netv
netv

Reputation: 185

Getting error - Ansible debug message using jinja2 - template error while templating string: Expected an expression, got end of statement block

I register below task results to "output" var

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

        }
    ]
}

my debus task has this:

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

Error:

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

I know i am doing wrong but not sure what/where is the mistake in my debug task ?

expected output:

1.1.1.1 BGP IS UP
2.2.2.2 BGP IS DOWN
3.3.3.3 BGP IS UP

Upvotes: 0

Views: 1553

Answers (1)

larsks
larsks

Reputation: 311711

You've written:

{% elif %}{{output.msg[0].neighbor}}BGP IS DOWN{%- endif %}

But I think you mean else. elif requires a conditional expression just like if. Also, you need to quote string values in an if expression.

Fixing both of those issues, we get:

   - 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 %}'

Given you sample input, this produces:

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

If I were writing this, I would probably reformat things for better readability and stick the task in a loop:

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

Upvotes: 2

Related Questions