我在Jinja写了一个Ansible模板,它有以下模式:
# Inventory
[Group1]
vm1 cluster=clusterName
[Group2]
vm2
Run Code Online (Sandbox Code Playgroud)
请注意,第二个组没有cluster定义属性.
# Task Definition
vars:
potential_seeds: "{{groups.all | map('extract', hostvars) | groupby('cluster') | list}}"
Run Code Online (Sandbox Code Playgroud)
当然,这会导致明显的错误:
"the field 'vars' has an invalid value, which appears to include a variable that is undefined. The error was: 'dict object' has no attribute 'cluster'
我需要以一种只对已cluster定义的字典进行分组的方式过滤列表.
你可以使用selectattr之前groupby:
groups.all |
map('extract', hostvars) |
selectattr('cluster','defined') |
groupby('cluster') |
list
Run Code Online (Sandbox Code Playgroud)
这将仅选择cluster在分组之前定义属性的元素.
| 归档时间: |
|
| 查看次数: |
1404 次 |
| 最近记录: |