How to avoid matching string with the substring in group_vars variable file

Hello,

I have items defined with with_items in ansible task and that item is not existing in the group_vars/all file so it should be skipped. But it is matching to the substring of a string in the group_vars file.
e.g. I have the below task:
task:
when: “item in variable”
with_items:

  • dash
  • lg

group_vars/all file has below list:
variable: |
[
“abc-dash”,
‘def-dash’
]

I don’t want the item (dash) should match to abc_dash and def_dash. currently, it is matching to both and it is not being skipped. I already tried quoting the item(dash) with single and double quotes but did not work. How can I implement this so that item dash can be skipped?

Your problem is with the when statement, not with the with_items
Try regexp_match

Actually, your problem is with the way you layed out variable
'variable', it is now a string.
You can either lay it out as a proper yaml list (with correct spacing etc)

variable:
  - abd-dash
  - def-dash

Or you can supply the from_yaml filter wherever you use it, for
example in your conditional:

when: item in variable|from_yaml

Dick

Hi Dick,

Thanks a lot. I used your below fix and it worked like a charm.

when: item in variable|from_yaml