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

Problem with type conversion #17665

Closed
damaca opened this issue Sep 20, 2016 · 4 comments
Closed

Problem with type conversion #17665

damaca opened this issue Sep 20, 2016 · 4 comments
Labels
affects_2.1 This issue/PR affects Ansible v2.1 bug This issue/PR relates to a bug. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@damaca
Copy link

damaca commented Sep 20, 2016

ISSUE TYPE
  • Bug Report
COMPONENT NAME

Type conversion

ANSIBLE VERSION

Tried with the versions below:
ansible 2.1.0.0
config file =
configured module search path = Default w/o overrides
ansible 2.1.1.0
config file =
configured module search path = Default w/o overrides


OS / ENVIRONMENT

N/A

SUMMARY

I found this when I tried to override a variable. This variable have a "int" value and I tried to override with other "int" from a config yml file. I have an {% if variable == 27017 %} block which is evaluated as false because of the variable type.

STEPS TO REPRODUCE

I created a simple role to reproduce de problem. The role have a template named test_template.j2 with the following content:

{% if port is string %}
{{port}} is string
{% else %}
{{port}} is not string
{% endif %}{% if port is string %}
{{port}} is string
{% else %}
{{port}} is not string
{% endif %}

Just a var inside the defaults/main.yml file:

port: 27017

The main.yml from tasks, which create a file good when is evaluated to true and wrong when is false :


---
# tasks file for test
- name: Check the type
  template: src=test_template.j2 dest=./{% if port == 27017 %}good{% else %}wrong{% endif %}{{iterate}}

A config.yml file with the value to override:

provision:
  port: 27017

And a test.yml file:

- name: TEST
  hosts: localhost
  vars_files:
    - config.yml
  roles:
    - role: test
      port: 27017
      iterate: 1
    - role: test
      port: "27017"
      iterate: 2
    - role: test
      port: "{{ provision.port }}"
      iterate: 3
    - role: test
      port: "{{ provision.port | int }}"
      iterate: 4
EXPECTED RESULTS

The expected result will be al least two "good" files in the case 1 and 4, maybe in the iterate 3 too and a wrong file in the case of iterate 2.

ACTUAL RESULTS

We found just one "good" file good1 with the "27017 is not a string" content and three "wrong" files with the content "27017 is a string"

○ → ap test_pl.yml
 [WARNING]: Host file not found: /usr/local/etc/ansible/hosts

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [TEST] ********************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [test : Check the type] ***************************************************
ok: [localhost]

TASK [test : Check the type] ***************************************************
ok: [localhost]

TASK [test : Check the type] ***************************************************
ok: [localhost]

TASK [test : Check the type] ***************************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0
@ansibot ansibot added bug_report affects_2.1 This issue/PR affects Ansible v2.1 labels Sep 20, 2016
@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
@dagwieers
Copy link
Member

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 bug This issue/PR relates to a bug. and removed bug_report labels Mar 1, 2018
@damaca
Copy link
Author

damaca commented Oct 18, 2018

Not included yet in 2.7, ¿right?

@mkrizek
Copy link
Contributor

mkrizek commented Mar 4, 2019

@damaca It is. Please see https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-jinja2-native

If you have further questions please stop by IRC or the mailing list:

@mkrizek mkrizek closed this as completed Mar 4, 2019
@ansible ansible locked and limited conversation to collaborators Jul 25, 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. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

5 participants