Search String in Ansible

match matches from the start of the string, so your examples would only match if the string started with are or you. So effectively it is the equivalent of ^are or ^you in regex terms.

You likely want search instead of match, which will search anywhere in the string.

You shouldn’t have jinja templating in a when clause. try something more simpler like the following, the search filter already returns a boolean, don’t need to test twice.

  • debug:
    msg: “hi how are ?”
    when: my_list | search(‘are’)

  • debug:
    msg: "hi how you ? "
    when: my_list | regex_search(‘yolo’)

Also check your inventory contains localhost.

Perfect it’s working.

But here it just giving true or false.

In case if I want to fetch the value then how can I do?

When needs either a true or false response.

Have a look at regex_replace if you want to return values.

Jon