[ Ansible ] - Read a properties file (java/springboot) to use later

Hey all,

Would like to count with your experience/feedback with a very specific task…

I would like to collect the content of a properties file (java/springboot) and turn it to a form of data structure to later use it during playbook execution.

The properties file will havethis layout as an example:

`

sw01.local.protocol=${proto}
sw01.local.host=${host}
sw01.local.user=${user}

sw02.local.protocol=${proto}
sw02.local.host=${host}
sw02.local.user=${user}

`

In the end my goal will be to have like:

`

sw01:
local:
protocol: ${proto}
host: ${host}
user: ${user}

`

Or similar.

Why?

INI module only allows me to search for a key and collect its value, but imagine on a dynamic way, if I add in the future a sw03 key with values I need to add to my playbook in a static way…

Can I count with your thoughts/experience/suggestions with similar cases?

Try this

    - set_fact:
        prop: "{{ prop|default({})|
                  combine({my_key0:
                          {my_key1:
                          {my_key2: my_value}}}, recursive=True) }}"
      vars:
        my_keys: "{{ item.split('=').0 }}"
        my_value: "{{ item.split('=').1 }}"
        my_keys_split: "{{ my_keys.split('.') }}"
        my_key0: "{{ my_keys_split.0 }}"
        my_key1: "{{ my_keys_split.1 }}"
        my_key2: "{{ my_keys_split.2 }}"
      loop: "{{ lookup('file', 'java/springboot').splitlines()|
                list>select()|list }}"
    - debug:
        var: prop

HTH,

  -vlado

Vlado, your solution worked like a charm.

Thank you once again and keep up the good work of sharing your insights/knowledge with community.

See "String Methods". For example
https://docs.python.org/3/library/stdtypes.html#string-methods

  vars:
    capitalize: 'ansible'
    center: 'center'
    digit: '123'
    alnum: '123abc'
  tasks:
    - debug:
        msg: "{{ capitalize.capitalize() }}"
    - debug:
        msg: "{{ center.center(10, '.') }}"
    - debug:
        msg: "{{ center.count('en') }}"
    - debug:
        msg: "{{ digit.isdigit() }}"
    - debug:
        msg: "{{ alnum.isalnum() }}"

give

    "msg": "Ansible"
    "msg": "..center.."
    "msg": "1"
    "msg": true
    "msg": true

Feel free to test others. HTH

  -vlado