I have a version variable, which I need to split and insert a . in between
I tried this
ansible localhost -e version=300 -m debug -a "msg={{ version | regex_replace('\d{1,2}', '.\g<0>' ) }}"
But the o/p is
TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [localhost] =>
msg: .30.0
There is a . getting .30.0 added in the first place. I can use regex_repace to remove the first. after that. But is there any other better way? Why is pattern putting the decimal point in the first place?
Q: "Why is pattern putting the decimal point in the first place?"
A: Regex \d{1,2}
matches one or two digits. Given the string 300
this regex matches the first two digits30
. It will be replaced with .\g<0>
which gives
.30
Next match is 0
because only one digit has left. The replacement gives
.0
Put together the result is
.30.0
Q: "Is there any way I can directly insert "." (dot) after the second place? ie 30.0?"
A: For example the play
- hosts: localhost
vars:
my_string: '300'
tasks:
- debug:
msg: "{{ my_string[0:2] ~ '.' ~ my_string[2:] }}"
- debug:
msg: "{{ my_string[:-1] ~ '.' ~ my_string[-1] }}"
gives
"msg": "30.0"
"msg": "30.0"