I have a machine where the variable contains the a number that I want to extract and then split to build an IP address using ansible.
variable = machine240dev
I want to be able to extract 240 then split it to fill in the two middle octets of an ip address
e.g. 10.2.40.170
I have tried various ways such as:
var: machine240dev
10.{{ var | regex_replace('[^0-9]','')[0] }}.{{ var | regex_replace('[^0-9]','')[-2:] }}.170
which fails as it doesn't like the [0] after the regex_replace filter.
I was just wondering if anyone has done this before or know a clean way to do it.
EDIT:
I have set a fact to first set the variable for the vlan before extracting the specific parts of the string.
- name: Setting vlan fact
set_fact: vlan="{{ var | regex_replace('[^0-9]','') }}"
Then in my template i set:
IPADDR=10.{{ vlan[0] }}.{{ vlan[-2:] }}.170
However I am still open to suggestions on how this could be improved