我有以下事实
"ansible_facts": {
"facter_partitions": {
"sda1": {
"filesystem": "ext4",
"label": "BOOT",
"mount": "/boot",
"size": "1048576",
"uuid": "bfa6fa2e-2de5-48db-89ff-63df19e577bb"
},
"sda2": {
"filesystem": "LVM2_member",
"size": "82835456"
}
}
},
我想获得标有filesystem
BOOT
我试着这样做
---
- hosts: localhost
tasks:
- debug: msg={{ facter_partitions.values() | selectattr("label","search","BOOT")| map(attribute="filesystem") | list}}
但是我收到了这个错误:
fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ facter_partitions.values() | selectattr(\"label\",\"search\",\"BOOT\") | map(attribute=\"filesystem\") | list }}): expected string or buffer"}
获得该领域的正确方法是什么?我不希望过滤器返回多个值。
# ansible --version
ansible 2.4.2.0
# rpm -qa | grep jinja2
python-jinja2-2.7.2-2.el7.noarch
Red Hat 7.4
修改
这不起作用的原因是因为我试图过滤未在所有项目上定义的字段。我首先需要过滤所有已定义“label”的项目,然后应用我的过滤器。
{{ facter_partitions.values() | selectattr("label","defined") | selectattr("label","search","BOOT") | map(attribute="filesystem") | first }}