我正在循环使用dict“ aws_ec2_volums_setting ”并尝试将循环变量item.id
传递给“ selectattr ”语句以获取列表符合标准。
- name: Set Filters
set_fact:
snapshot_list: "{{ ec2_snapshot_facts.snapshots | selectattr('volume_id', 'equalto', vars[item.id] ) | list }}"
with_items: "{{ aws_ec2_volums_setting }}"
如果我对该值进行硬编码,则可以正常工作:
- name: Set Filters
set_fact:
snapshot_list: "{{ ec2_snapshot_facts.snapshots | selectattr('volume_id', 'equalto', 'vol-xxxxxxxxx' ) | list }}"
with_items: "{{ aws_ec2_volums_setting }}"
如果我运行调试命令来检查var的值,那么看起来很好
- name: Check Val
debug:
msg: "{{ item.id }}"
with_items: "{{ aws_ec2_volums_setting }}"
输出:
TASK [aws_delete_snapshoot : Set Filters2] ****************************************************************************************
ok: [localhost] => (item={u'vol': u'Vol-01', u'id': u'vol-XXXXXX', u'server': u'us-nv-sat-01'}) => {
"changed": false,
"item": {
"id": "vol-vol-XXXXXX",
"server": "us-nv-sat-01",
"vol": "Vol-01"
},
"msg": "vol-XXXXXX"
}
答案 0 :(得分:3)
在我看来,你只需要放弃vars [],它应该可以正常工作。将vars [item.id]更改为item.id。
尝试:
- name: Set Filters
set_fact:
snapshot_list: "{{ ec2_snapshot_facts.snapshots | selectattr('volume_id', 'equalto', item.id ) | list }}"
with_items: "{{ aws_ec2_volums_setting }}"