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

33 views
Skip to first unread message

Sahar

unread,
Jan 21, 2019, 12:48:51 PM1/21/19
to Ansible Project
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
 [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: "{{ OldImage }}" in
state.stdout

Kai Stian Olstad

unread,
Jan 21, 2019, 12:57:06 PM1/21/19
to ansible...@googlegroups.com
As the error message say, you can't use {{ }} in when so just do

when: OldImage in state.stdout

--
Kai Stian Olstad


Sahar

unread,
Jan 21, 2019, 1:42:41 PM1/21/19
to Ansible Project
Any alternative to make this work?


On Monday, January 21, 2019 at 12:48:51 PM UTC-5, Sahar wrote:
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
 [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: "{{ OldImage }}" in
state.stdout

Sahar Dianat

unread,
Jan 21, 2019, 6:01:28 PM1/21/19
to ansible...@googlegroups.com
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] *********************************************************************************************************************

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/11732955.liqkLmbh1j%40x1.
For more options, visit https://groups.google.com/d/optout.

goforawalktwice

unread,
Jan 22, 2019, 12:49:44 AM1/22/19
to Ansible Project
Try dumping the contents of state.stdout ... Use a debug and you'll need braces

Brian Coca

unread,
Jan 30, 2019, 7:35:02 PM1/30/19
to Ansible Project
when: "'OldImage' in state.stdout"

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


--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages