2

I have variable, its version numbering. How I catch last number in version

example

app_ver: 1.5.0.0.20

- debug:
    var: app_ver | regex_search('^(\d+).(\d+).(\d+).(\d+).(\d+)$') | list

gives me

"app_ver |regex_search('^(\\d+).(\\d+).(\\d+).(\\d+).(\\d+)$') | list": [
        "1",
        ".",
        "5",
        ".",
        "0",
        ".",
        "0",
        ".",
        "2",
        "0"
    ]

I need last number, may be 1-figure or more

Indrek Mäestu
  • 21
  • 1
  • 1
  • 2
  • try `regex_search('\.(\d+)$')` in place of `regex_search('^(\d+).(\d+).(\d+).(\d+).(\d+)$')`. This will give you ".20" but do not convert it to `list`. – kaza Sep 30 '17 at 16:44
  • Can you use (\d+)(?!.*\d) as shown here : [regular expression get last number](https://stackoverflow.com/questions/5320525/regular-expression-to-match-last-number-in-a-string) – QHarr Sep 30 '17 at 16:45
  • if ansible supports look-arounds then I would suggest `regex_search('(?<!\.)(\d+)$')` This should give just `20`. – kaza Sep 30 '17 at 16:49

1 Answers1

11

Split the line by . and take last element:

- debug:
    msg: "{{ app_ver.split('.')[-1] }}"
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • If I have a `var: mango-apple-banana-orange` and if I were to split and combine everything except banana, would that be possible via ansible? Desired output: `mango-apple-orange` I have been trying different ways using `var.split('-')[-2] | join('-')` , cannot seem to tell ansible to ingore the 3rd element. Thoughts pls? – Jninja Aug 05 '21 at 19:33