On Mon, 2 Mar 2020 18:14:41 -0800 (PST)
M F <[email protected]> wrote:

> I am having to resort to doing the following to get my answer of 4
> 
> set_fact: myvar = "{{ ((SOMENUMBER|int) / 2)|replace('.0','') }}"
> 
> Is there a better way to take a single integer stored as a string, and 
> perform math and output an INT more simply?

I'm afraid there isn't any. Jinja is a template engine, hence the default
output is a string. This was the intention of the developers, I think.
Otherwise one would have to explicitly convert the output of mathematics
expressions to strings.

Some notes to the examples:

* The output of the example isn't INT. It's a string. The consequent task
  confirms this

    - debug:
        msg: Variable myvar is a string
      when: myvar is string

* Also the following task will fail

    - debug:
        msg: "{{ myvar + 4 }}"

  with error message:

    "Unexpected templating type error occurred on ({{ myvar + 4 }}):
     coercing to Unicode: need string or buffer, int found"

* Instead the conversion to integer works as expected

    - debug:
        msg: "{{ myvar|int + 4 }}"

  gives:

    "msg": "8"


HTH
                -vlado

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200303070748.33c00854%40gmail.com.

Attachment: pgp40g6K51Uc6.pgp
Description: OpenPGP digital signature

Reply via email to