Ansible 检查when语句中是否定义了嵌套变量

Ansible 检查when语句中是否定义了嵌套变量,ansible,Ansible,嵌套数据结构可以通过使用键名编制索引或使用键名的点语法进行访问- 但我无法从when语句中的变量访问嵌套数据结构。这里有一个最小的示例来演示这个问题 # cat play.yml - hosts: localhost vars: parent: child1:

嵌套数据结构可以通过使用键名编制索引或使用键名的点语法进行访问-

但我无法从when语句中的变量访问嵌套数据结构。这里有一个最小的示例来演示这个问题

# cat play.yml                                                                                                         
- hosts: localhost   
  vars:
    parent:
      child1: true   tasks:
  - name: "Check if variable is defined"
    fail:
      msg: "mandatory variable {{ item }} not passed as extra args when invoking playbook"
    when: item not in vars
    loop:
    - parent
    - parent.child1
    - parent.child2
下面是示例输出

ansible-playbook play.yml                                                                                             (livingstone-dev/monitoring)
 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] *********************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [localhost]

TASK [Check if variable is defined] **************************************************************************************************************************
skipping: [localhost] => (item=parent) 
failed: [localhost] (item=parent.child1) => {"ansible_loop_var": "item", "changed": false, "item": "parent.child1", "msg": "mandatory variable parent.child1 not passed as extra args when invoking playbook"}
failed: [localhost] (item=parent.child2) => {"ansible_loop_var": "item", "changed": false, "item": "parent.child2", "msg": "mandatory variable parent.child2 not passed as extra args when invoking playbook"}

PLAY RECAP ***************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0 
知道我如何在ansible when语句中嵌套变量吗。以下是一些我已经尝试过但没有成功的东西:

  • 使用点符号访问变量
  • 将when语句更改为不带引号的“未定义项”

  • 下面的代码为我工作。您必须在
    “{{}}”


    这是工作手册。感谢@smily为我指明了正确的方向。因为循环变量项需要在计算when条件之前进行计算,所以我必须展开变量项。我通过在双引号中包含整个when条件并展开item循环变量来实现

    $ cat play.yml                                                                                                          (livingstone-dev/monitoring)
    - hosts: localhost
      vars:
        parent:
          child1: true
      tasks:
      - name: show variable
        debug:
          var: parent
      - name: "Check if variable is defined"
        fail:
          msg: "mandatory variable item not passed as extra args when invoking playbook"
        when: "{{item}} is not defined"
        loop:
        - parent
        - parent.child1
        - parent.child2
    
    这是剧本输出

    $ ansible-playbook play.yml                                                                                             (livingstone-dev/monitoring)
     [WARNING]: No inventory was parsed, only implicit localhost is available
    
     [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
    
    
    PLAY [localhost] *********************************************************************************************************************************************
    
    TASK [Gathering Facts] ***************************************************************************************************************************************
    ok: [localhost]
    
    TASK [show variable] *****************************************************************************************************************************************
    ok: [localhost] => {
        "parent": {
            "child1": true
        }
    }
    
    TASK [Check if variable is defined] **************************************************************************************************************************
     [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{item}} is not defined
    
    skipping: [localhost] => (item=parent) 
    skipping: [localhost] => (item=parent.child1) 
    failed: [localhost] (item=parent.child2) => {"ansible_loop_var": "item", "changed": false, "item": "parent.child2", "msg": "mandatory variable item not passed as extra args when invoking playbook"}
    
    PLAY RECAP ***************************************************************************************************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
    

    在没有警告更改的情况下获得相同的结果

    when: "{{item}} is not defined"
    


    我试过你的建议。它确实可以工作,但没有给出相关的错误消息。整个任务失败,甚至没有执行循环,并显示以下错误消息:{“msg”:“'dict object'没有属性'child2'”。但它确实帮助我找到了解决办法。谢谢我会把它作为答案贴出来。我试过你的建议了。获取所有循环项的此错误<代码>“msg”:“失效选项无效:何时”对您有效?我是不是漏掉了什么?我已经修好了。
    when: "{{item}} is not defined"
    
    when: not vars|json_query(item)