regexansible

How to evaluate a when condition for Ansible task


I am having a variable with the following value:

   name_prefix: stage-dbs

I am having a task in my playbook which will have to check this variable and see if it contains *-dbs , if the condition is met then it should process. I wrote something like this :

- name: Ensure deployment directory is present
  file:
    path=/var/tmp/deploy/paTestTool
    state=directory
  when: name_prefix =="*-dbs" # what condition is required here to evaulate the rest part of variable?? 

What regex pattern should be used or how to use a regex here ?


Solution

  • No need to use regex for pattern searching. You can use search like this:

    when: name_prefix | search("stage-dbs")
    

    It will definitely work.