-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: int not of type integer if jinja2 expressions are used in custom attributes #985
Comments
Did you try casting it to
|
I have encountered the same issue and I can confirm that casting a variable to
I also tried casting it to
|
OK. I think at the very least, we should add the note about using We probably need to query the |
Ansible jinja filter always return strings, unless you set jinja2_native=true in your ansible.cfg or do I ran into this when using id's against netbox, it wants them to be ints not strings. ie, |
Hi @ThomasADavis that's just a workaround, the netbox module itself knows which datatype the netbox API expects (or could query the schema for custom objects Probably would also prevent injection attacks in some cases. But at a bare minimum, it would improve UX and also provide better error messages when users provide incorrect data. (Btw, I just noticed the API calls I just mentioned also return any defined validation min/max/regex, so technically we could do the validation before even calling the actual API) |
That is true, I ran into this when doing bulk updates via URL to netbox in ansible - it much faster to do bulk update of a large number of interfaces than do a loop with the netbox module. There is also other places in the modules that look/ask for a id, and I'm not sure if it's doing right conversion there either; ie, if it's a object_id, it's always going to be an int.. To get the bulk updates to work correctly, I had to turn on the jinja native type, which promptly broke ansible_async (another collection used this feature). So the modules need to be able to work no matter what the jinja_native setting is. |
This comment was marked as off-topic.
This comment was marked as off-topic.
even when running python filter, which converts the data from string to int (confirmed with type_debug) - the module doesn't recognize it as a integer. The only workaround is inja2_native=true in ansible.cfg. But the problem comes - how to do it if i'm using AWX ? |
Add ansible.cfg with the option to your project root. |
The workaround I used when encountering this issue is setting my custom field to text, and then setting a regex validation field of ^\d+$, which essentially forces it to be an integer. |
Ansible NetBox Collection version
v3.12.0
Ansible version
NetBox version
v3.4.5
Python version
3.10
Steps to Reproduce
with:
some_ansible_variable: 3
Expected Behavior
correct serialization of custom fields using their correct datatype.
Observed Behavior
API Post request is formed incorrectly with "3" as string instead of integer. Doing a "type_debug" in the playbook outputs that it is of type int. But when I try to use it in the
netbox.netbox.netbox_vm_interface
module for example it gets serialized as string and an error message of"msg": "{\"__all__\":[\"Invalid value for custom field 'some_integer_field': Value must be an integer.\"]}"
is printedIf I add an explicit int cast in _normalize_data, then the custom fields are also sent correctly. I don't know how to write a generic version that would query the custom field type and serialize it correctly though. Also I don't know if there is a possibility to use the same datatype ansible outputs for ansible.builtin.type_debug filter.
Edit: I found a workaround. To set
[defaults] jinja2_native=True
within theansible.cfg
, but as it is not required for other modules, there may be an attribute to set within the module.The text was updated successfully, but these errors were encountered: