Ansible: El tipo de variables cambia de int a string durante las manipulaciones matemáticas

Creado en 22 dic. 2016  ·  1Comentario  ·  Fuente: ansible/ansible

TIPO DE PROBLEMA

  • Informe de error
NOMBRE DEL COMPONENTE
VERSION ANSIBLE
ansible 2.2.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
CONFIGURACIÓN
SO / MEDIO AMBIENTE


Ubuntu 14.04
Ubuntu 16.04

RESUMEN

las variables int cambian a cadenas durante las manipulaciones

PASOS PARA REPRODUCIR

- 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' }}"
RESULTADOS PREVISTOS


La tercera tarea debería devolver int 2, mientras que la cuarta tarea probablemente debería fallar

RESULTADOS ACTUALES


3ra tarea fallida

$ 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

Comentario más útil

Esto es realmente divertido de Jinja, ya que no siempre sabrá la fuente de la var (por ejemplo, la línea de comandos -e vars no se somete a inferencia de tipo YAML y, por lo tanto, siempre son cadenas), debe afirmar Obligar a eso antes de hacer operaciones matemáticas. La forma correcta de hacerlo entonces sería:

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

>Todos los comentarios

Esto es realmente divertido de Jinja, ya que no siempre sabrá la fuente de la var (por ejemplo, la línea de comandos -e vars no se somete a inferencia de tipo YAML y, por lo tanto, siempre son cadenas), debe afirmar Obligar a eso antes de hacer operaciones matemáticas. La forma correcta de hacerlo entonces sería:

- debug: msg="{{ c | int + 0 }}"
¿Fue útil esta página
0 / 5 - 0 calificaciones