I need to extract a specific value (release version in my case) from a string. I don't know how regex work in Ansible. Below is my Play -
---
- name: Getting the status of current deployment
stat:
path: /opt/tomcat/apps/myapp
register: p
- debug:
msg: "The value of symlink is {{ p.stat.lnk_target }}"
- debug:
msg: "{{ p.stat.lnk_target.split('/')[4]}}"
Output:
Getting the status of current deployment...
Monday 18 May 2020 11:18:43 +0100 (0:00:01.580) 0:00:01.812 ************
server1 ok
set_fact...
Monday 18 May 2020 11:18:43 +0100 (0:00:00.552) 0:00:02.364 ************
server1 ok
debug...
Monday 18 May 2020 11:18:43 +0100 (0:00:00.077) 0:00:02.442 ************
server1 ok: {
"changed": false,
"msg": "The value of symlink is /app/tomcat/releases/Release1.40.0-07/myapp/webapp"
}
debug...
Monday 18 May 2020 11:35:20 +0100 (0:00:00.080) 0:00:02.234 ************
myvm-kn-u1 ok: {
"changed": false,
"msg": "Release1.40.0-07"
}
I want to extract the release number from it and put it in a variable. For example in the above output it is 1.40.0-07
Let me know if someone can guide. Tried to look into google but couldn't find anything.
I think the best way is use regex_replace
In your case
- debug:
msg: "{{ p.stat.lnk_target.split('/')[4] | regex_replace('^Release(.*)$', '\\1') }}"
May be you need to adjust the Regex little bit.