Essentially I'm looking for exactly this, but for ansible/jinja2: Convert a comma separated string of key values pairs to dictionary
I'm getting a semi-colon separated list of key=value pairs from a mariadb galera server wsrep_provider_options:
mariadb_wsrep_provider_options_output = 'base_dir = /var/lib/mysql/; base_host = 192.168.1.101; base_port = 4567;'
After storing that output with ansible register
, I am trying to parse it into a dictionary, like so:
mariadb_wsrep_provider_options_output:
base_dir: /var/lib/mysql/
base_host: 192.168.1.101
base_port: 4567
...
I've tried something like this, but I'm not sure how to convert it back to a dict
:
- debug:
msg: "{% for item in mariadb_wsrep_provider_options_output.split(';') %} {{ item.split('=') }} {% endfor %}"
EDIT: I have it working with the below tasks, but it seems clunky:
- set_fact:
new_var: "{{ new_var|d([]) + [{ 'key': item.split('=')[0]|trim, 'value': item.split('=')[1]|trim }] }}"
loop: "{{ mariadb_wsrep_provider_options_output.split(';') }}"
- debug:
var: new_var | items2dict
output:
"new_var|items2dict": {
"base_dir": "/var/lib/mysql/",
"base_host": "192.168.100.153",
"base_port": "4567"
}