How to evaluate a when condition for Ansible task

29,785

Solution 1

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

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

It will definitely work.

Solution 2

Not sure filter search exists today?

solution with https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#searching-strings-with-regular-expressions :

when: name_prefix | regex_search("^stage-dbs(.*)$")
Share:
29,785
Nishant Singh
Author by

Nishant Singh

DevOps/SRE!! Reincarnation 1.0

Updated on July 29, 2022

Comments

  • Nishant Singh
    Nishant Singh almost 2 years

    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 ?