Skip to content
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

Fix native type conversion in json_template #154

Merged
merged 4 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion action_plugins/command_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,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']"