How to use variable in "when" statement in ansible?

Any idea how can I fix the below error?

tasks:

  • name: check the state (primary/secondary/standalone)
    asa_command:
    commands:

  • ‘show version | i “{{ OldImage }}”’
    register: state

  • include: reboot.yml
    when: ‘“{{ OldImage }}” in state.stdout’

Error I’m receiving:

TASK [include] *********************************************************************************************************************
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: “{{ OldImage }}” in
state.stdout
skipping: [Fw4Script-01.tor.bellnhs.int]
[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: “{{ OldImage }}” in
state.stdout
skipping: [Fw4Script-02.tor.bellnhs.int]

As the error message say, you can't use {{ }} in when so just do

  when: OldImage in state.stdout

Any alternative to make this work?

I don’t get the error anymore, but it doesn’t work as expected either. I will skip my both devices which is not expected:

TASK [include] *********************************************************************************************************************
skipping: [Fw4Script-01.tor.int]
skipping: [Fw4Script-02.tor.int]

Try dumping the contents of state.stdout ... Use a debug and you'll need braces

when: "'OldImage' in state.stdout"

since i assume OldImage is a string and not a variable.