Skip to content

Commit

Permalink
Fix native type conversion in json_template (#154)
Browse files Browse the repository at this point in the history
* Fix native type conversion in json_template

Fixes #151

*  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

* Fix CI failure

* Update doc

* Add example usage in doc
  • Loading branch information
ganeshrn authored and trishnaguha committed Oct 9, 2018
1 parent 9fa2e2b commit a83d5c2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion action_plugins/command_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def do_pattern_group(self, block):
def _process_directive(self, task):
for directive, args in iteritems(task):
if directive == 'block':
display.deprecated('`block` is not longer supported, use `pattern_group` instead')
display.deprecated('`block` is not longer supported, use `pattern_group` instead', version=2.6)
directive = 'pattern_group'

if directive not in self.VALID_DIRECTIVES:
Expand Down
30 changes: 30 additions & 0 deletions docs/directives/parser_directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,36 @@ The following arguments are supported for this directive:

* `template`


**Note**
Native jinja2 datatype (eg. 'int', 'float' etc.) rendering is supported with Ansible version >= 2.7
and jinja2 library version >= 2.10. To enable native jinja2 config add below configuration in active
ansible configuration file.
```
[defaults]
jinja2_native= True
```
Usage example:
```yaml
- set_fact:
count: "1"
- name: print count
debug:
msg: "{{ count|int }}"
```

With jinja2_native configuration enabled the output of above example task will have
```
"msg": 1
```

and with jinja2_native configuration disabled (default) output of above example task will have
```
"msg": "1"
```

### `set_vars`

Use the `set_vars` directive to set variables to the values like key / value pairs
Expand Down
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 a83d5c2

Please sign in to comment.