String being submitted when it should be an integer

First thank you for your help. :grinning:

A little background on what I am trying to accomplish. I am pulling a query via JSON and submitting to another API(Servicedesk). Every time I submit to service desk, the item variable is to be processed as a string. I need the quotes removed for submission. I have tried converting to (INT) and the data continues to be processed as a string. If I manually set the variable to the number it will submit correctly. here is what my output currently looks like.

(Grabbed from -vvv ansible output)
“customfield_1705”: “421266”
and here is what i need it to look like
“customfield_1705”: 421266 (Requirement of servicedesk)

this is an example of how I submit the data.

  • community.general.jira:
    args:
    fields:
    customfield_1705: “{{json.[0].jmesquery}}”

What does the following return?

- name: Debug customfield_1705
  ansible.builtin.debug:
    var: customfield_1705 | ansible.builtin.type_debug
  vars:
    customfield_1705: "{{ json.[0].jmesquery | int }}"

Before I convert to int it is a string. here is the output of type debug.

ok: [localhost] => {
“msg”: [
“str”
]

If I set to INT and submit here is the output of that field

“{{json.[0].jmesquery | int }}”

“customfield_1705”: “421266”

It looks like it gets converted back to a string?

Sorry I don’t have an answer to this :frowning:

@chris ,

no problem, I have no clue either. :smile:

I found a solution. I needed to set the jinja2 Native to true. Feel free to look at this solution. Thank you.

2 Likes

In the normal course of things the result of Jinja templating is always a string (even for cases where it looks like it isn’t, that’s because Ansible converted it from a string into something else after templating.)

This isn’t a great user experience for Ansible, where a large number of non-string types are used, so there is a (currently optional) feature to enable preservation of types during templating. This will be the default in the future, but for now you can enable it if necessary. Ansible Configuration Settings — Ansible Community Documentation

Unfortunately this has the potential to break existing content that relied on the previous behaviour, so it requires careful testing to make nothing’s subtly wrong with this configuration enabled.

As an alternative, you can template more of the structure; since Ansible will parse a string that represents a dict back into a data structure, this can contain more types.

- community.general.jira:
    fields: "{{ { 'customfield_1705': json.[0].jmesquery } }}"
2 Likes

oh Thank you very much. this works much better. I prefer not to make a global change for on variable. appreciate the help.

don’t use var to check types, it does ‘forced jsonization’, use msg:

- debug: msg="{{ json.[0].jmesquery | int| type_debug }}"

The trick is to always cast at consumption, if you do this in intermediate steps you might still get ‘stringified’, using the ‘native’ engine minimizes this but does not remove it. An upcoming change to the core way Ansible deals with types (Data Tagging) will hopefully fix this going forward.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.