我有大型变量列表(xml文件,4000多行),我通过xml-> yml转换器运行。输出yaml类似于下面的简化版本。
在模板中,我试图从另一个列表中的var文件列表中提取值。我需要来自' level4'的项目,当_type = listType1
来自var文件时:
Var文件:
---
# Site-specific vars
level1:
level2:
-
_type: listType1
_instance: 1
level3:
level4:
- "theImportanStuff1"
- "theImportanStuff2"
-
_type: notListType1
_instance: 1
level3:
level4:
- "notImportanStuff1"
- "notImportanStuff2"
模板文件(我尝试了许多变种,这种情况发生在最后):
# TEST FILE
# Insert Info Here6
{% for item in [level1.level2|selectattr('_type','match','listType1')|selectattr('level3.level4') | list ] %}
myInfo: {{ item }}
{% endfor %}
任务文件:
---
- name: set
set_fact:
testit: "{{level1.level2|selectattr('_type','match','listType1')| map(attribute='level3') | join (', ') }}"
- debug:
msg="{{ testit }}"
- name: set2
set_fact:
testit2: "{{level1.level2|selectattr('_type','match','listType1')| list }}"
- debug:
msg="{{ testit2 }}"
- name: set3
set_fact:
testit3: "{{level1.level2|selectattr('_type','match','listType1') }}"
- debug:
msg="{{ testit3 }}"
# tasks file for ansible-role snmp
- name: "Gather OS specific variables"
include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_distribution|lower }}-{{ ansible_distribution_version }}.yml"
- "{{ ansible_distribution|lower }}.yml"
- "{{ ansible_os_family|lower }}.yml"
- name: Copy TEST configuration file
template:
src: test.conf.j2
dest: /root/test.conf
mode: 0600
owner: root
group: root
运行输出:
[root@AnsibleServer2 ansible]# ansible-playbook -i inventory/staging/host_vars/hostname2 playbook/testit.yml
PLAY [Test-c7-1] ******************************************************************************************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [Test-c7-1]
TASK [john.test : set] ************************************************************************************************************************************************************
ok: [Test-c7-1]
TASK [john.test : debug] **********************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": {
"level4": [
"theImportanStuff1",
"theImportanStuff2"
]
}
}
TASK [john.test : set2] ***********************************************************************************************************************************************************
ok: [Test-c7-1]
TASK [john.test : debug] **********************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": [
{
"_instance": 1,
"_type": "listType1",
"level3": {
"level4": [
"theImportanStuff1",
"theImportanStuff2"
]
}
}
]
}
TASK [john.test : set3] ***********************************************************************************************************************************************************
ok: [Test-c7-1]
TASK [john.test : debug] **********************************************************************************************************************************************************
ok: [Test-c7-1] => {
"msg": "<generator object _select_or_reject at 0x1aa9730>"
}
TASK [john.test : Gather OS specific variables] ***********************************************************************************************************************************
ok: [Test-c7-1] => (item=/etc/ansible/roles/john.test/vars/redhat.yml)
TASK [john.test : Copy TEST configuration file] ***********************************************************************************************************************************
ok: [Test-c7-1]
PLAY RECAP ************************************************************************************************************************************************************************
Test-c7-1 : ok=9 changed=0 unreachable=0 failed=0
我尝试的所有内容都无法运行,或者将其中一个添加到test.conf中的值:
myInfo: <generator object _select_or_reject at 0x1b93cd0>
myInfo: [{u'_type': u'listType1', u'level3': {u'level4': [u'theImportanStuff1', u'theImportanStuff2']}, u'_instance': 1}]
我希望看到:
myInfo: theImportanStuff1
myInfo: theImportanStuff2
答案 0 :(得分:1)
如果您的NoSuchKey
条件仅返回单个元素:
_type = listType1
如果您的{% for item in (level1.level2|selectattr('_type','match','listType1') | first).level3.level4 %}
myInfo: {{ item }}
{% endfor %}
条件返回一个列表,则需要两个循环,但从您的问题中的剥离示例中不清楚在这种情况下您要打印的内容。无论如何,模板是:
_type = listType1