# How to avoid matching string with the substring in group\_vars variable file

**URL:** <https://forum.ansible.com/t/how-to-avoid-matching-string-with-the-substring-in-group-vars-variable-file/35493>\
**Category:** Ansible Project\
**Created:** [July 12, 2021, 7:21pm UTC](https://forum.ansible.com/t/how-to-avoid-matching-string-with-the-substring-in-group-vars-variable-file/35493 "2021-07-12T19:21:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaurav\_Oberai](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gaurav\_Oberai](https://forum.ansible.com/u/Gaurav_Oberai)\
**Post date:** [July 12, 2021, 7:21pm UTC](https://forum.ansible.com/t/how-to-avoid-matching-string-with-the-substring-in-group-vars-variable-file/35493/1 "2021-07-12T19:21:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 13, 2021, 4:28am UTC](https://forum.ansible.com/t/how-to-avoid-matching-string-with-the-substring-in-group-vars-variable-file/35493/2 "2021-07-13T04:28:04Z")

</div>

Your problem is with the when statement, not with the with\_items  
Try regexp\_match

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 13, 2021, 9:28am UTC](https://forum.ansible.com/t/how-to-avoid-matching-string-with-the-substring-in-group-vars-variable-file/35493/3 "2021-07-13T09:28:33Z")

</div>

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:  
&nbsp;&nbsp;- abd-dash  
&nbsp;&nbsp;- 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

---

<div class="post-metadata">

**Author:** ![Gaurav\_Oberai](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@Gaurav\_Oberai](https://forum.ansible.com/u/Gaurav_Oberai)\
**Post date:** [July 13, 2021, 10:43am UTC](https://forum.ansible.com/t/how-to-avoid-matching-string-with-the-substring-in-group-vars-variable-file/35493/4 "2021-07-13T10:43:26Z")

</div>

Hi Dick,

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

when: item in variable|from\_yaml
