【问题标题】:when statement with var1 contain in var2 ( can this be done?)当带有 var1 的语句包含在 var2 中时(可以这样做吗?)
【发布时间】:2020-06-20 09:42:03
【问题描述】:
# 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

这不起作用

播放 [127.0.0.1] ******************************************* ************************************************

TASK [在 var2 中测试 var1] *************************************** ***************************** 致命:[127.0.0.1]:失败! => {"msg": "条件检查 'var1 is in var2' failed. 错误是: Unexpected templating type error occurred on ({% if var1 is in var2 %} True {% else %} False {% endif % }): 'in ' 需要字符串作为左操作数,而不是 int\n\n错误似乎在 '/home/cngo/ansible/p79/define_var.yml': 第 14 行第 5 列,但可能\n在其他地方该文件取决于确切的语法问题。\n\n违规行似乎是:\n\n\n - name: test var1 inside of var2\n ^ here\n"}

【问题讨论】:

    标签: ansible


    【解决方案1】:

    错误信息很清楚: 'in' requires string as the left operand, not int。 Ansible 无法在字符串中搜索整数。 var1 应该是一个字符串

      vars:
        var1: "100"
    

    如果您无法更改 var1 的声明,另一种选择是 quote 变量

        when:  var1|quote is in var2
    

    ,或

        when:  var2 is search(var1|quote)
    

    【讨论】:

      猜你喜欢
      • 2011-08-18
      • 2015-06-17
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 2010-10-05
      相关资源
      最近更新 更多