Search code examples
jinja2salt-project

How to get rid of special char '(u'


I am using Salt with jinja2 "regex_search" and I try to extract some digits (release version) from the archive file name. Then use the value to create a symlink, that contains it. I've tried different combinations using "list", "join" and other filters to get rid of this Unicode char, but without success.

Example: "release_info" variable gets value "release-name-0.2345.577_20190101_1030.tar.gz" and I need to get only digits between the dots.

Here is the corresponding part of the sls file:

symlink to current release {{ release_info }}:
  file.symlink:
    - name: /home/{{ component.software['component_name'] }}/latest
    - target: /home/{{ component.software['component_name'] }}/{{ release_info |regex_search('(\d+\.\d+\.\d+)') }}
    - user: support
    - group: support`enter code here`

The expected result is "/home/support/0.2345.577", but I have "/home/support/(u'0.2345.577',)"

If I try to pipe "yaml" or "json" filter like:

{{ release_info |regex_search('(\d+\.\d+\.\d+)') | yaml }}

I've got:

/home/support/[0.2345.577]

which is not what I am looking for.

PS I've got it, but seems to me as not a got approach. Just workaround.

{{ release_info |regex_search('(\d+\.\d+\.\d+)') |yaml |replace('[','') |replace(']','') }}

Solution

  • Hello Todor and Welcome to Stack Overflow!

    I have tried the example that you have posted and here is how to achieve what you want

    Note: I have changed the regex pattern a little in order to support any other possibilities that could have more digits e.g 0.1.2.3.4 and so on, but of course you can use your pattern as long as it works for you as expected.

    Solution 1:

    {{ release_info | regex_search("(\d(\.\d+){1,})") | first }}
    

    The result before using first:

    ('0.2345.577', '.577')
    

    The result after using first:

    0.2345.577
    

    Solution 2:

    {{ release_info | regex_search("(\d\.\d+\.\d+)") | first }}
    

    The result before using first:

    ('0.2345.577',)
    

    The result after using first:

    0.2345.577
    

    first is a built-in filter in jinja that can return the first item in a sequence. you can check List of built-in filters for more information about the other filters