how to split value in Ansible with delimiter

Question:

I am setting a fact in Ansible and that variable has a value with hyphens, like this “dos-e1-south-209334567829102380“. i want to split , so i only get “dos-e1-south”

Here is the play

Answer:

another option is ansibles regular expression filter, you find here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#regular-expression-filters

has the same result:

Explanation for the regex:

keep everything from the start of the string in the first backreference

find the last “-” followed by non-“-” characters till the end of string.

replaces the string with the first backreference.

Leave a Reply