'How to grab a value from a war file without extracting it preferably, or extracting the minimum amount

I have a .war file called app.war, which contains a test.properties file which has a line called appName: Blackberry. The test.properties file could be anywhere in the WAR file, no specific directory.

What is the most efficient way for me to find the test.properties file, and then grab the value Blackberry from appName: Blackberry which is one of the lines in the file ?

The file looks like this

mainInfo: deployed 
app.Name: Blackberry 
testRun: success

I have heard about jar xf app.war, but not sure how to approach it. I am very new to Ansible, any help would be really appreciated :).



Solution 1:[1]

Regarding your question "How to grab a value from ...", I've created a simple test logic.

---
- hosts: test
  become: no
  gather_facts: no

  vars:

    WAR_FILE: "app.war"
    PROPERTY_FILE: "test.properties"

  tasks:

  - name: Gather full path of '{{ PROPERTY_FILE }}', if there is any
    shell:
      cmd: zipinfo -1 {{ WAR_FILE }} | grep {{ PROPERTY_FILE }}
    register: result
    check_mode: false
    changed_when: false
    failed_when: result.rc != 0

  - name: Gather content of '{{ PROPERTY_FILE }}'
    shell:
      cmd:  unzip -qq -c {{ WAR_FILE }} {{ result.stdout }}
      warn: false
    register: properties
    check_mode: false
    changed_when: false
    failed_when: properties.rc != 0

  - name: Show content
    debug:
      msg: "{{ properties.stdout }}"
    check_mode: false

... Consider using the unarchive module rather than running 'unzip'

Resulting into an output of

TASK [Show content] ****
ok: [test.example.com] =>
  msg: |-
    mainInfo: deployed
    app.Name: Blackberry
    testRun: success

You have to adapt this to your environment and requirements.

Thanks to

Please take note that the result properties.stdout is a list of strings which contains the key values pairs.

  - name: Debug how to get value of key
    debug:
      var: item | type_debug
    loop_control:
      label: "{{ ansible_loop.index }}"
      extended: yes
    loop: "{{ properties.stdout_lines }}"

It is recommended to read them into Ansible variables. You will find plenty examples for how to do that here on SO via a search.

  - name: Debug how to get value of key
    debug:
      var: item | from_yaml | type_debug
    loop: "{{ properties.stdout_lines }}"

Thanks to

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1