# set\_facts with if-else clause, also check if the var contains digits?

**URL:** <https://forum.ansible.com/t/set-facts-with-if-else-clause-also-check-if-the-var-contains-digits/30309>\
**Category:** Ansible Project\
**Created:** [May 11, 2019, 6:15pm UTC](https://forum.ansible.com/t/set-facts-with-if-else-clause-also-check-if-the-var-contains-digits/30309 "2019-05-11T18:15:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [May 11, 2019, 6:15pm UTC](https://forum.ansible.com/t/set-facts-with-if-else-clause-also-check-if-the-var-contains-digits/30309/1 "2019-05-11T18:15:31Z")

</div>

Im trying to set a variable based on below condition where templname is an external variable with below options

ansible-playbook -i hosts -e “templname=CTrl03\_kerbs\_09”

OR

ansible-playbook -i hosts -e “templname=CTrl05”

set\_fact:  
dbn: "{{ CTrl03\_kerbs\_09 | lower }}  
dbnum: “{ % ‘prefix-’ + templname.split(‘_')[-1] if '_’ in templname else ‘prefix’ %}” ##if templname contains “\_” then dbnum should append “prefix-” to the last digit of templname i.e I want “prefix-9”

I want vars set like below:

1. 

dbn = ctrl03\_kerbs\_09  
dbnum = prefix-9

1. 

dbn = ctrl05

dbnum = prefix

But this doesngt seem to work? Any problem in syntax? I get syntax error…

Also is there a ansible way to check if the string(templname) contains digits??
