I am trying to create a list of dictionaries in Ansible by parsing 2 lists and below is the code.
- name: Create list of data dictionary
vars:
_sinfo: |-
{% for data in data_list|default([]) %}
{%- if item in data['subject'] %}
[{ grade: {{data['grade']}}, id: {{data['id']}}, subject: {{data['subject']}} }]
{%- endif -%}
{% endfor %}
set_fact:
data_dict_list: "{{ data_dict_list|default([]) + _sinfo|list}}"
with_items:
- "{{ subject_list }}"
Above script gives me a list of dictionary with each string broken into letters and using
data_dict_list: "{{ data_dict_list|default([]) + _sinfo|list|join}}"
Gives me error
msg: 'Unexpected templating type error occurred on ({{ data_dict_list|default([]) + _sinfo | list | join}}): can only concatenate list (not "unicode") to list'
List values: [u''Physics'', u''Chemistry'', u''Biology'', u''Math'', u''Geology'']
Inventory file:
data_list:
- id: 31
grade: ['A+']
subject: Physics
- id: 40
grade: ['B']
subject: Math
- id: 30
grade: ['A']
subject: Biology
- id: 33
grade: ['A+']
subject: Physics
- id: 35
grade: ['A+', 'B+']
subject: Physics
Note:
Ansible version: 2.7.7
Python version: 2.7.18
Jinja2 version: 2.11.2
What I am trying to do is to group data by subject and create a list of subjects with each subject dictionary containing all the data with that subject.