set_facts with if-else clause, also check if the var contains digits?

8 views
Skip to first unread message

Ansar Sahit

unread,
May 11, 2019, 2:15:31 PM5/11/19
to Ansible Project
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


2)
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??


Reply all
Reply to author
Forward
0 new messages