检查值是否是列表的一部分,列表的名称位于变量中

RAM*_*M88 2 ansible

我尝试查找变量是否具有存储在列表中的有效值。
列表的名称也存储在变量中。如果变量无效,测试就会失败。
如果我使用列表的名称,这将起作用:

- fail: msg="unsupported version requested"
  when: requestedversion not in windowsversionlist
Run Code Online (Sandbox Code Playgroud)

但名字versionlist也是一个变量,因为像linux和windows这样的列表比较多。

这不起作用:

- fail: msg="unsupported version requested"
  when: requesterversion not in versionlist 
Run Code Online (Sandbox Code Playgroud)

变量的versionlist值为windowsversionlist

我尝试{{ versionlist }}等等,但这不起作用。

β.ε*_*.βε 6

您应该使用vars查找

- fail: 
    msg: "unsupported version requested"
  when: "requestedversion not in lookup('vars', versionlist)"
Run Code Online (Sandbox Code Playgroud)

鉴于剧本:

- hosts: localhost
  gather_facts: no
      
  tasks:
    - fail: 
        msg: "unsupported version requested"
      when: "requestedversion not in lookup('vars', versionlist)"
      vars:
        requestedversion: a.a.a
        versionlist: windowsversionlist
        windowsversionlist: 
          - x.x.x
          - y.y.y
          - z.z.z
Run Code Online (Sandbox Code Playgroud)

这给出:

TASK [fail] *******************************************************
fatal: [localhost]: FAILED! => changed=false 
  msg: unsupported version requested
Run Code Online (Sandbox Code Playgroud)