For example
- set_fact:
x: "{{ _dict|from_yaml }}"
vars:
_regex: '^(.*)\[(.*)\](.*)$'
_key: "{{ stdout|regex_replace(_regex, '\\1') }}"
_val: "{{ stdout|regex_replace(_regex, '\\2') }}"
_dict: |
{{ _key }}:
{% for i in _val.split() %}
{{ i|regex_replace(':', ': ') }}
{% endfor %}
gives
x:
map:
key1: value1
key2: value2
key3: value3
If there were more lines, e.g.
stdout_lines:
- "map1[key1:value1 key2:value2 key3:value3]"
- "map2[key1:value1 key2:value2 key3:value3]"
- "map3[key1:value1 key2:value2 key3:value3]"
it would be possible to combine the dictionary, e.g.
- set_fact:
x: "{{ x|d({})|combine(_dict|from_yaml) }}"
loop: "{{ stdout_lines }}"
vars:
_regex: '^(.*)\[(.*)\](.*)$'
_key: "{{ item|regex_replace(_regex, '\\1') }}"
_val: "{{ item|regex_replace(_regex, '\\2') }}"
_dict: |
{{ _key }}:
{% for i in _val.split() %}
{{ i|regex_replace(':', ': ') }}
{% endfor %}
gives
x:
map1:
key1: value1
key2: value2
key3: value3
map2:
key1: value1
key2: value2
key3: value3
map3:
key1: value1
key2: value2
key3: value3
If the keys were repeating, e.g.
stdout_lines:
- "map[key1:value1 key2:value2 key3:value3]"
- "map[key1:value1 key2:value2 key3:value3]"
- "map[key1:value1 key2:value2 key3:value3]"
it would be possible to concatenate a list, e.g.
- set_fact:
x: "{{ x|d([]) + [_dict|from_yaml] }}"
loop: "{{ stdout_lines }}"
vars:
_regex: '^(.*)\[(.*)\](.*)$'
_key: "{{ item|regex_replace(_regex, '\\1') }}"
_val: "{{ item|regex_replace(_regex, '\\2') }}"
_dict: |
{{ _key }}:
{% for i in _val.split() %}
{{ i|regex_replace(':', ': ') }}
{% endfor %}
gives
x:
- map:
key1: value1
key2: value2
key3: value3
- map:
key1: value1
key2: value2
key3: value3
- map:
key1: value1
key2: value2
key3: value3