Ansible: Variables type change from int to string during math manipulations

Created on 22 Dec 2016  ·  1Comment  ·  Source: ansible/ansible

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   
affects_2.2 bug

Most helpful comment

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 }}"

>All comments

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 }}"
Was this page helpful?
0 / 5 - 0 ratings