Skip to content

Commit

Permalink
Fix native type comnversion in json_template
Browse files Browse the repository at this point in the history
*  The current implementation tries to convert the value
   to int irrespective to the actual desired type of data.
*  Ansible version 2.7 onwards supports native jinja2 types
   eg: value: "{{ item.name.acl_name|float }}"
   To convert to native jinja2 type by default need to set
   `jinja2_native` config varaible in `defaults` setion within
   configuration file.
   Note: this required jinja2 version to be >= 2.10
  • Loading branch information
ganeshrn committed Oct 6, 2018
1 parent b4e90c1 commit ab2b443
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 12 deletions.
11 changes: 0 additions & 11 deletions lib/network_engine/plugins/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,13 @@ def template(self, data, variables, convert_bare=False):
self._templar.set_available_variables(variables)
try:
resp = self._templar.template(data, convert_bare=convert_bare)
resp = self._coerce_to_native(resp)
except AnsibleUndefinedVariable:
resp = None
pass
finally:
self._templar.set_available_variables(tmp_avail_vars)
return resp

def _coerce_to_native(self, value):
if not isinstance(value, bool):
try:
value = int(value)
except Exception:
if value is None or len(value) == 0:
return None
pass
return value

def _update(self, d, u):
for k, v in iteritems(u):
if isinstance(v, collections.Mapping):
Expand Down
2 changes: 1 addition & 1 deletion tests/json_template/json_template/tasks/json_lookup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
- "'GigabitEthernet0/0' in result.msg"
- "'config' in result['msg']['GigabitEthernet0/0']"
- "'Configured by Ansible' in result['msg']['GigabitEthernet0/0']['config']['description']"
- "result['msg']['GigabitEthernet0/0']['config']['mtu'] == 1500"
- "result['msg']['GigabitEthernet0/0']['config']['mtu'] == '1500'"
- "'iGbE' in result['msg']['GigabitEthernet0/0']['config']['type']"
- "'GigabitEthernet0/0' in result['msg']['GigabitEthernet0/0']['config']['name']"

0 comments on commit ab2b443

Please sign in to comment.