首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从嵌套在另一个列表中的列表中提取项

从嵌套在另一个列表中的列表中提取项
EN

Stack Overflow用户
提问于 2018-06-07 02:36:03
回答 1查看 1.2K关注 0票数 2

我们有很大的变量列表(xml文件、4000+行),我通过一个xml->yml转换器运行了它们。输出的yaml类似于下面的简化版本。

在一个模板中,我尝试从另一个列表中的var文件列表中提取一个值。我需要var文件中的'level4‘中的项,当是_type = listType1时:

Var文件:

代码语言:javascript
复制
---
# Site-specific vars

level1:
  level2:
    -
      _type: listType1
      _instance: 1
      level3:
        level4:
         - "theImportanStuff1"
         - "theImportanStuff2"
    -
      _type: notListType1
      _instance: 1
      level3:
        level4:
         - "notImportanStuff1"
         - "notImportanStuff2"

模板文件(我已经尝试了很多很多变体,这发生在最后一个):

代码语言:javascript
复制
# TEST FILE
# Insert Info Here6
{% for item in [level1.level2|selectattr('_type','match','listType1')|selectattr('level3.level4') | list ] %}
myInfo: {{ item }}
{% endfor %}

任务文件:

代码语言:javascript
复制
---
- 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

运行的输出:

代码语言:javascript
复制
[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的值中:

代码语言:javascript
复制
myInfo: <generator object _select_or_reject at 0x1b93cd0>

myInfo: [{u'_type': u'listType1', u'level3': {u'level4': [u'theImportanStuff1', u'theImportanStuff2']}, u'_instance': 1}]

我希望看到的是:

代码语言:javascript
复制
myInfo: theImportanStuff1
myInfo: theImportanStuff2
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-07 04:51:44

如果您的_type = listType1条件仅返回单个元素:

代码语言:javascript
复制
{% for item in  (level1.level2|selectattr('_type','match','listType1') | first).level3.level4 %}
myInfo: {{ item }}
{% endfor %}

如果您的_type = listType1条件返回一个列表,那么您需要两个循环,但是从您问题中的已剥离示例中并不清楚在这种情况下您希望打印什么。无论如何,模板是:

代码语言:javascript
复制
{% for outer in level1.level2|selectattr('_type','match','listType1') %}
{% for inner in outer.level3.level4 %}
myInfo: {{ inner }}
{% endfor %}
{% endfor %}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50727334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档