Jinja convert string to integer
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
“`
{% macro get_host_number() -%}
{{ item | regex_replace('^D*[0]?(d*)$', '\1') }}
{%- endmacro %}
{% macro get_host_name() -%}
{{ item | regex_replace('^(D*)d*$', '\1') }}
{%- endmacro %}
{% macro get_host_range(name, number) -%}
{% if name=='aaa' %}
{{ ((number*5)+100) | int | abs }}
{% elif name=='bbb' %}
{{ ((number*5)+200) | int | abs }}
{% else %}
{{ ((number*5)+300) | int | abs }}
{% endif %}
{%- endmacro %}
{% set number = get_host_number() %}
{% set name = get_host_name() %}
{% set value = get_host_range(name, number) %}
Name: {{ name }}
Number: {{ number }}
Type: {{ value }}
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.
Answers:
You need to cast string to int after regex’ing number:
{% set number = get_host_number() | int %}
And then there is no need in | int
inside get_host_range
macro.
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
“`
{% macro get_host_number() -%}
{{ item | regex_replace('^D*[0]?(d*)$', '\1') }}
{%- endmacro %}
{% macro get_host_name() -%}
{{ item | regex_replace('^(D*)d*$', '\1') }}
{%- endmacro %}
{% macro get_host_range(name, number) -%}
{% if name=='aaa' %}
{{ ((number*5)+100) | int | abs }}
{% elif name=='bbb' %}
{{ ((number*5)+200) | int | abs }}
{% else %}
{{ ((number*5)+300) | int | abs }}
{% endif %}
{%- endmacro %}
{% set number = get_host_number() %}
{% set name = get_host_name() %}
{% set value = get_host_range(name, number) %}
Name: {{ name }}
Number: {{ number }}
Type: {{ value }}
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.
You need to cast string to int after regex’ing number:
{% set number = get_host_number() | int %}
And then there is no need in | int
inside get_host_range
macro.