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

**URL:** <https://forum.ansible.com/t/ansible-read-a-properties-file-java-springboot-to-use-later/32291>\
**Category:** Ansible Project\
**Created:** [February 21, 2020, 9:57am UTC](https://forum.ansible.com/t/ansible-read-a-properties-file-java-springboot-to-use-later/32291 "2020-02-21T09:57:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_Santos](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Joao\_Santos](https://forum.ansible.com/u/Joao_Santos)\
**Post date:** [February 21, 2020, 9:57am UTC](https://forum.ansible.com/t/ansible-read-a-properties-file-java-springboot-to-use-later/32291/1 "2020-02-21T09:57:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 21, 2020, 10:51am UTC](https://forum.ansible.com/t/ansible-read-a-properties-file-java-springboot-to-use-later/32291/2 "2020-02-21T10:51:06Z")

</div>

Try this

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prop: "{{ prop|default({})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({my\_key0:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{my\_key1:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{my\_key2: my\_value}}}, recursive=True) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_keys: "{{ item.split('=').0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_value: "{{ item.split('=').1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_keys\_split: "{{ my\_keys.split('.') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_key0: "{{ my\_keys\_split.0 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_key1: "{{ my\_keys\_split.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_key2: "{{ my\_keys\_split.2 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ lookup('file', 'java/springboot').splitlines()|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list\>select()|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: prop

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Joao\_Santos](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Joao\_Santos](https://forum.ansible.com/u/Joao_Santos)\
**Post date:** [February 21, 2020, 1:08pm UTC](https://forum.ansible.com/t/ansible-read-a-properties-file-java-springboot-to-use-later/32291/3 "2020-02-21T13:08:48Z")

</div>

Vlado, your solution worked like a charm.

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

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 21, 2020, 2:11pm UTC](https://forum.ansible.com/t/ansible-read-a-properties-file-java-springboot-to-use-later/32291/4 "2020-02-21T14:11:43Z")

</div>

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

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

give

&nbsp;&nbsp;&nbsp;&nbsp;"msg": "Ansible"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "..center.."  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": "1"  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": true  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": true

Feel free to test others. HTH

&nbsp;&nbsp;-vlado
