pythonansiblemariadbjinja2galera

How to convert a string of "key = value;" pairs to a dictionary in ansible?


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"
    }

Solution

  • Try this

        - set_fact:
            new_var: "{{ dict(_keys|zip(_vals)) }}"
          vars:
            _arr: "{{ mdb_out.split(';')|map('trim')|select()|list }}"
            _keys: "{{ _arr|map('regex_replace', '^(.*?)=(.*)$', '\\1')|map('trim')|list }}"
            _vals: "{{ _arr|map('regex_replace', '^(.*?)=(.*)$', '\\2')|map('trim')|list }}"
    

    gives

      new_var:
        base_dir: /var/lib/mysql/
        base_host: 192.168.1.101
        base_port: '4567'
    

    Your code with a few changes works fine too

        - set_fact:
            new_var: "{{ new_var|d({})|combine({_key: _val}) }}"
          loop: "{{ mdb_out.split(';') }}"
          when: item|length > 0
          vars:
            _key: "{{ item.split('=')[0]|trim }}"
            _val: "{{ item.split('=')[1]|trim }}"
    

    split can be used as a filter since Ansible 2.11. You can use it instead of regex_replace here. The task below gives the same result

        - set_fact:
            new_var: "{{ dict(_keys|zip(_vals)) }}"
          vars:
            _arr: "{{ mdb_out.split(';')|map('trim')|select()|list }}"
            _keys: "{{ _arr|map('split', '=')|map('first')|map('trim')|list }}"
            _vals: "{{ _arr|map('split', '=')|map('last')|map('trim')|list }}"