Search code examples
regexansibleconfiguration-management

Ansible regex_replace does not work


"{{ archive_name | regex_replace('^(.*-)?.*-(.*)-.*-.*-.*-.*', '\\g<1>')}}"

Command above gives me an error in Ansible. archive_name is in the format of alpine-1.10-324bghz-i-2018-0503. I'm trying to grab the version number 1.10. It returns an error with \\g<1>\ instead of the version number. Am I doing something wrong? Any help or guidance is greatly appreciated here.

Thank you in advance!


Solution

  • You could simply split the string, avoiding regex altogether.

    {{ archive_name.split('-')[1:2] | join('-') }}
    

    In theory the result should return only 1.10 (unable to test).