ansible 2.2.0.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
Ubuntu 14.04
Ubuntu 16.04
int variables change to strings during manipulations
- 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' }}"
3rd task should return int 2, while 4th task should most likely fail
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
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 }}"
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: