2
votes
# This playbook to test define var
- hosts: 127.0.0.1
  become: yes
  gather_facts: no

  vars:
    var1: 100
    var2: "contain 100"

  tasks:

  - name: test var1 inside of var2
    debug:
      msg: "var1 is inside var2"
    when:  var1 is in var2

This does not work

PLAY [127.0.0.1] ***********************************************************************************

TASK [test var1 inside of var2] ******************************************************************** fatal: [127.0.0.1]: FAILED! => {"msg": "The conditional check 'var1 is in var2' failed. The error was: Unexpected templating type error occurred on ({% if var1 is in var2 %} True {% else %} False {% endif %}): 'in ' requires string as left operand, not int\n\nThe error appears to be in '/home/cngo/ansible/p79/define_var.yml': line 14, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: test var1 inside of var2\n ^ here\n"}

1

1 Answers

1
votes

The error message is clear: 'in' requires string as the left operand, not int. Ansible is not able to search an integer in a string. var1 shall be a string

  vars:
    var1: "100"

Another option is to quote the variable if you're not able to change the declaration of var1

    when:  var1|quote is in var2

, or

    when:  var2 is search(var1|quote)