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

jinja2 templates can only yield strings #18722

Closed
pablobm opened this issue Dec 2, 2016 · 5 comments
Closed

jinja2 templates can only yield strings #18722

pablobm opened this issue Dec 2, 2016 · 5 comments
Labels
affects_2.1 This issue/PR affects Ansible v2.1 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@pablobm
Copy link

pablobm commented Dec 2, 2016

ISSUE TYPE
  • Bug Report
COMPONENT NAME
  • Evaluation of jinja2 templates
ANSIBLE VERSION
$ ansible --version
ansible 2.1.2.0
  config file =
  configured module search path = Default w/o overrides
OS / ENVIRONMENT

N/A (assumed. Only tested on macOS)

SUMMARY

You can't have non-string variables as the result of arithmetic/filters/etc in jinja2 templates

STEPS TO REPRODUCE

Consider a playbook and a vars file:

# test_vars.yml
seven: 7
seven_again: "{{ seven }}"
seven_once_more: "{{ seven | int }}"
fourteen: "{{ (seven * 2) | int }}"
# test.yml
- hosts: all
  gather_facts: False
  vars_files:
    - test_vars.yml
  tasks:
    - debug:
        msg: "{{ seven | to_json}} {{ seven_again | to_json }} {{ seven_once_more | to_json }} {{ fourteen | to_json }}"
EXPECTED RESULTS

I would have expected all four numbers to appear unquoted, as follows (abridged):

$ ansible-playbook test.yml -i localhost, -c local -vvv
(...)
ok: [localhost] => {
    "msg": "7 7 7 14"
}
(...)
ACTUAL RESULTS

Instead, the last two numbers are quoted, as follows (abridged):

$ ansible-playbook test.yml -i localhost, -c local -vvv
(...)
ok: [localhost] => {
    "msg": "7 7 \"7\" \"14\""
}
(...)
@ansibot ansibot added bug_report affects_2.1 This issue/PR affects Ansible v2.1 labels Dec 2, 2016
@ansibot ansibot added module This issue/PR relates to a module. plugin and removed module This issue/PR relates to a module. plugin labels Dec 13, 2016
@melankh
Copy link

melankh commented Dec 14, 2016

Hi,
This bug affect us too.


# ansible --version
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/usr/share/ansible']
# uname -a
Linux xxxxxxxx   2.6.32-642.6.2.el6.x86_64 #1 SMP Wed Oct 26 06:52:09 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

@alikins
Copy link
Contributor

alikins commented Jan 5, 2017

(Mentioning #17992 here to keep track of related issues)

@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 29, 2017
@ansibot
Copy link
Contributor

ansibot commented Sep 7, 2017

@dagwieers
Copy link
Member

dagwieers commented Sep 21, 2017

There is a light at the end of the tunnel. We made a change to Jinja2 so we don't see all variable types changed into strings. See: pallets/jinja#708

@ansibot ansibot added the module This issue/PR relates to a module. label Nov 17, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 2, 2018
@mkrizek
Copy link
Contributor

mkrizek commented Sep 3, 2018

This behavior can be altered with the following config option (turned off by default) https://docs.ansible.com/ansible/devel/reference_appendices/config.html#default-jinja2-native which will be included in the 2.7 release.

@mkrizek mkrizek closed this as completed Sep 3, 2018
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.1 This issue/PR affects Ansible v2.1 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

6 participants