Nishant Singh
Nishant Singh

Reputation: 3209

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 ?

Upvotes: 13

Views: 55711

Answers (3)

Kalyan Raparthi
Kalyan Raparthi

Reputation: 435

Using "|" is deprecated in ansible 2.9.* .Instead of using name_prefix |search use name_prefix is search. This feature will be removed in version 2.9

Upvotes: 3

Thomas Decaux
Thomas Decaux

Reputation: 22701

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(.*)$")

Upvotes: 20

kohi
kohi

Reputation: 984

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

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

It will definitely work.

Upvotes: 21

Related Questions