Extracting items from list within another list

272 views
Skip to first unread message

John Kane

unread,
Jun 6, 2018, 1:47:59 PM6/6/18
to Ansible Project
Newbie when it comes to ansible and jinja.  Were porting our installation and maintenance python scripts to ansible.  We have large variable list (xml files, 4000+ lines) which I ran through an xml->yml converter.  The output yaml is similar to the simplified version below.

From within a template, I'm trying to extract a value from a var file list within another list.  I need the items from 'level4' in the var file:

Var file:
---
# Site-specific vars

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

Template file(I've tried many, many variations, this happens to the the last):
# TEST FILE
# Insert Info Here6
{% for item in [level1.level2|selectattr('_type','match','listType1')|selectattr('level3.level4') | list ] %}
myInfo: {{ item }}
{% endfor %}


Task file:
---
- 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

Output from run:
---
- 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


Everything I've tried either fails to run, or adds one of these to values in the 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}]


Where I'd like to see:

myInfo: theImportanStuff1
myInfo: theImportanStuff2

Thanks in advance,
John


John Kane

unread,
Jun 6, 2018, 1:51:13 PM6/6/18
to Ansible Project
Sorry, output from run:
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



John Kane

unread,
Jun 6, 2018, 5:54:10 PM6/6/18
to Ansible Project
Found my answer:

{% for item in level1.level2|selectattr('_type','match','listType1') %}
{% for item2 in item.level3.level4 %}
myInfo
: {{ item2 }}
{% endfor %}
{% endfor %}

I had too many [ ] in my testing.


On Wednesday, June 6, 2018 at 12:47:59 PM UTC-5, John Kane wrote:
Reply all
Reply to author
Forward
0 new messages