我们有很大的变量列表(xml文件、4000+行),我通过一个xml->yml转换器运行了它们。输出的yaml类似于下面的简化版本。
在一个模板中,我尝试从另一个列表中的var文件列表中提取一个值。我需要var文件中的'level4‘中的项,当是_type = listType1时:
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发布于 2018-06-07 04:51:44
如果您的_type = listType1条件仅返回单个元素:
{% for item in (level1.level2|selectattr('_type','match','listType1') | first).level3.level4 %}
myInfo: {{ item }}
{% endfor %}如果您的_type = listType1条件返回一个列表,那么您需要两个循环,但是从您问题中的已剥离示例中并不清楚在这种情况下您希望打印什么。无论如何,模板是:
{% for outer in level1.level2|selectattr('_type','match','listType1') %}
{% for inner in outer.level3.level4 %}
myInfo: {{ inner }}
{% endfor %}
{% endfor %}https://stackoverflow.com/questions/50727334
复制相似问题