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

Variables type change from int to string during math manipulations #19643

Closed
yvlasenko opened this issue Dec 22, 2016 · 1 comment
Closed

Variables type change from int to string during math manipulations #19643

yvlasenko opened this issue Dec 22, 2016 · 1 comment
Labels
affects_2.2 This issue/PR affects Ansible v2.2 bug This issue/PR relates to a bug.

Comments

@yvlasenko
Copy link
Contributor

ISSUE TYPE
  • Bug Report
COMPONENT NAME
ANSIBLE VERSION
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
CONFIGURATION
OS / ENVIRONMENT

Ubuntu 14.04
Ubuntu 16.04

SUMMARY

int variables change to strings during manipulations

STEPS TO REPRODUCE
- hosts: localhost
  vars:
    a: 1
    b: "{{ a }}"
    c: "{{ a+1 }}"
  tasks:
    - debug: msg="{{ a + 0 }}"
    - debug: msg="{{ b + 0 }}"
    - debug: msg="{{ c + 0 }}"
      ignore_errors: yes
    - debug: msg="{{ c + '0' }}"
EXPECTED RESULTS

3rd task should return int 2, while 4th task should most likely fail

ACTUAL RESULTS

3rd task failed

$ ansible-playbook a.yml -vvvvv
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: provided hosts list is empty, only localhost is available

Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/dist-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: a.yml ****************************************************************
1 plays in a.yml

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/core/system/setup.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: yurii
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
ok: [localhost]

TASK [debug] *******************************************************************
task path: /home/yurii/sandbox/ans/a.yml:7
ok: [localhost] => {
    "msg": "1"
}

TASK [debug] *******************************************************************
task path: /home/yurii/sandbox/ans/a.yml:8
ok: [localhost] => {
    "msg": "1"
}

TASK [debug] *******************************************************************
task path: /home/yurii/sandbox/ans/a.yml:9
fatal: [localhost]: FAILED! => {
    "failed": true, 
    "msg": "Unexpected templating type error occurred on ({{ c + 0 }}): coercing to Unicode: need string or buffer, int found"
}
...ignoring

TASK [debug] *******************************************************************
task path: /home/yurii/sandbox/ans/a.yml:11
ok: [localhost] => {
    "msg": "20"
}

PLAY RECAP *********************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0   
@ansibot ansibot added affects_2.2 This issue/PR affects Ansible v2.2 bug_report needs_triage Needs a first human triage before being processed. labels Dec 22, 2016
@nitzmahone nitzmahone removed the needs_triage Needs a first human triage before being processed. label Dec 22, 2016
@nitzmahone
Copy link
Member

This is really just Jinja fun- since you won't always know the source of the var (eg, command-line -e vars don't undergo YAML type inference and are thus always strings), you need to assert/coerce that before doing mathematical ops. The right way to do it then, would be:

- debug: msg="{{ c | int + 0 }}"

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.2 This issue/PR affects Ansible v2.2 bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

3 participants