How to fix warning message conditional statements should not include jinja2 templating delimiters

How do I make changes to the below complex when condition without breaking the code in order to address the warning.

- debug: msg: "Hi"
when: entrycurrdb.stdout.find("{{ hostvars['localhost']['BASEPATH_FINAL'] }}/{{ vars[(item | splitext)[1].split('.')[1] | default('exe')] }}/{{ item | basename }}") == -1 and actualfile.stat.exists == True

Getting warning

Output:

TASK [debug] *******************************************************************
[WARNING]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}. Found: entrycurrdb.stdout.find(“{{
hostvars[‘localhost’][‘BASEPATH_FINAL’] }}/{{ vars[(item |
splitext)[1].split(‘.’)[1] | default(‘exe’)] }}/{{ item | basename }}”) == -1
and actualfile.stat.exists == True
changed: [10.0.0.16]

Kindly suggests.

`

- debug:
msg: Hi
when:
- entrycurrdb.stdout.find(hostvars['localhost']['BASEPATH_FINAL'] ~ '/' ~ vars[(item | splitext)[1].split('.')[1] | default('exe')] ~ '/' ~ item | basename) == -1
- actualfile.stat.exists | bool

`

A few comments though

  • This is an extremely complex and weird conditional that will be hard for anything to read and understand what is happening. Consider simplifying it as much as you can or using set_fact beforehand to build the path to find components so you can better document it.
  • Split every and condition only a new entry to reduce the line length, each entry is just and’d together
  • The ‘~’ is a jinja2 string concatenation operator that works similar to ‘+’ but it converts both sides to a string before adding it together making it better for what you want
  • The reason why the warning still showed is because you still have jinja2 blocks, technically they are required in your example because you have it in a string but what you should be doing is not enclse the var in a string and concat it together
    I haven’t tested this but it “should” work.