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

342 views
Skip to first unread message

João Santos

unread,
Feb 21, 2020, 4:57:32 AM2/21/20
to Ansible Project
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?





Vladimir Botka

unread,
Feb 21, 2020, 5:51:26 AM2/21/20
to João Santos, ansible...@googlegroups.com
On Fri, 21 Feb 2020 01:57:32 -0800 (PST)
João Santos <morgado...@gmail.com> wrote:

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

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

João Santos

unread,
Feb 21, 2020, 8:09:40 AM2/21/20
to Vladimir Botka, ansible...@googlegroups.com
Vlado, your solution worked like a charm.

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


Vladimir Botka

unread,
Feb 21, 2020, 9:12:04 AM2/21/20
to João Santos, ansible...@googlegroups.com
On Fri, 21 Feb 2020 13:15:59 +0000
João Santos <morgado...@gmail.com> wrote:

> Regarding ".splitlines()" and other functions that might not be
> directly documented at Ansible webpages, for a newbie, how can I be able to
> know which one are available to be used?

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
Reply all
Reply to author
Forward
0 new messages