我正在查询一个表,只对"name";柱该表以字典列表的形式返回,因此我提取";name";并将其添加到列表中。问题是,如果名称具有某个值,例如";N/A";我不希望我正在填充的新列表拉入值"0";N/A";而是另一个值;跳过";。下面是一个我正在做的不起作用的例子,列表保持不变:
- name: Extract "name" column from table and append it to new_list
set_fact:
new_list: "{{ new_list + [item.name] }}"
loop: "{{queried_table.record}}"
- name: Iterate through new_list and change "N/A" elements to "skip"
set_fact:
current_name: "skip"
when: current_name == "N/A"
loop: "{{new_list}}"
loop_control:
loop_var: current_name
我想做什么,但在伪代码:
- name: Extract "name" column from table and append it to new_list
set_fact:
new_list: "{{ if(item.name == "N/A"){
append the word "skip" to new_list instead of "N/A"
}else{append item.name onto new_list}
}}"
loop: "{{queried_table.record}}"
Q:"从每个条目中提取"name"。。。将"N/A"替换为"skip">
A: 例如,给定下表用于测试
queried_table:
record:
- {name: r1, rec: data}
- {name: r2, rec: data}
- {name: N/A, rec: none}
映射属性名称和regex_replace项
new_list: "{{ queried_table.record|
map(attribute='name')|
map('regex_replace', 'N/A', 'skip')|
list }}"
给出
new_list: [r1, r2, skip]
将new_list的声明放入vars中(视情况而定(。