Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible selectattr和带有可选字段的映射_Ansible - Fatal编程技术网

Ansible selectattr和带有可选字段的映射

Ansible selectattr和带有可选字段的映射,ansible,Ansible,我正在尝试创建一个ansible playbook来设置多个节点。在我的变量中,我有以下结构: nodes: - nodename: foonode othervalue: foo booleanvalue: true - nodename: barnode othervalue: bar 布尔值是可选的,应该默认为false。以后可能会有更多的可选值。现在我只想使用可选的布尔值 - name: "Debug" debug: ms

我正在尝试创建一个ansible playbook来设置多个节点。在我的变量中,我有以下结构:

nodes:
  - nodename: foonode
    othervalue: foo
    booleanvalue: true
  - nodename: barnode
    othervalue: bar
布尔值是可选的,应该默认为false。以后可能会有更多的可选值。现在我只想使用可选的布尔值

- name: "Debug"
  debug:
    msg: "Do something useful here"
  when:  when: nodes | selectattr('nodename', 'equalto', ansible_hostname) | map(attribute='booleanvalue') | first | bool

只要booleanvalue存在,此代码段就可以工作。但它不适用于巴诺德。因为我使用的是2.9.6,所以我不能使用map(attribute='booleanvalue',default='whatever')。是否有其他或更好的方法使其与可选值一起工作?

将数据放入字典。比如说,

节点:
foonode:
其他值:foo
布尔值:true
巴诺德:
其他值:bar
那么,条件是微不足道的

-调试:
msg:“在这里做一些有用的事情”
时间:节点[inventory_hostname]['booleanvalue']|默认值(false)

未测试

非常感谢。这很有效,也很容易理解。