Question:
I am trying to convert a string that has been parsed using a regex into a number so I can multiply it, using Jinja2. This file is a template to be used within an ansible script.
I have a series of items
which all take the form of <word><number>
such as aaa01
, aaa141
, bbb05
.
The idea was to parse the word and number(ignoring leading zeros) and use them later in the template.
I wanted to manipulate the number by multiplication and use it. Below is what I have done so far
`
1 2 3 4 |
{% macro get_host_number() -%} {{ item | regex_replace('^\D*[0]?(\d*) With the above template I am getting an error coercing to Unicode: need string or buffer, int found which i think is telling me it cannot convert the string to integer, however i do not understand why. I have seen examples doing this and working. |
Answer:
You need to cast string to int after regex’ing number:
1 2 |
{% set number = get_host_number() | int %} |
And then there is no need in | int
inside get_host_range
macro.