Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String does not convert to integer in Ansible Playbook

Sorry if this already being asked before, but i can’t find any solution that match my problem..

I’m trying to convert the string that i have to integer:

- hosts: myhost
  vars:
    - variable1: 15
    - variable2: "15"
    - variable3: "{{ variable2 | int }}"

I tried to use variable2 (string) for calculation, so i convert it to int in variable3. But it failed with error message "Unexpected templating type error occurred on ({{ variable3 + 1 }}): coercing to Unicode: need string or buffer, int found"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So, i tried to use type_debug to find out each type of the 3 variable listed.

  • variable1 is int
  • variable2 is ansibleunicode
  • variable3 is unicode

I don’t understand the difference between ansibleunicode and unicode or why it is listed as unicode instead of integer when i already convert it from string to integer using | int. What should I do?

>Solution :

Try the following.

- hosts: myhost
  vars:
    - variable1: 15
    - variable2: "15"

  tasks:
  - name: test 
    debug: msg="{{ variable2 | int + 1}}"
    delegate_to: localhost

output:

TASK [test] ***********************************************************************************************************************
ok: [remote -> localhost] => {
    "msg": "16"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading