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

时间:2019-12-16 09:08:40

标签: ansible

嵌套数据结构可以通过使用键名建立索引或使用键名-https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#accessing-complex-variable-data的点语法来访问

但是我无法从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语句中都有嵌套变量。以下是我已经尝试过但无法正常工作的一些事情:

  1. 使用点分符号来访问变量
  2. 在不带引号的情况下将when语句更改为“未定义项目”

3 个答案:

答案 0 :(得分:1)

以下代码对我有用。您必须在"{{}}"

中循环提供值
  -  name: "Check if variable is defined"
     fail:
      msg: "mandatory variable {{ item }} not passed as extra args when invoking playbook"
     loop:
      - "{{parent.child1}}"
      - "{{parent}}"
      - "{{parent.child2}}"
     when: item is not defined


答案 1 :(得分:1)

要获得相同的结果而无需更改警告

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

when: not vars|json_query(item)

答案 2 :(得分:0)

这是工作手册。感谢@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