-3

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 :).

U880D
  • 8,601
  • 6
  • 24
  • 40
testcodezz
  • 69
  • 6
  • 2
    I'd suggest first trying to get this done manually (with `jar` command). Lookup the help for the command to know how to - list the contents; extract only the necessary file, etc. Ansible can come into picture once we have this clarity. – seshadri_c Feb 01 '22 at 06:19
  • As I understood your question; The module `find` can be used to locate your file which could be located "anywhere", and with the help of `lookup`-plugin or `slurp` module filtered to `regex` you can your get desired content, which is `appName: blackberry` in this case. – user1098490 Feb 01 '22 at 06:23
  • I understand your question that you were looking for something like `unzip -qq -c app.war $(zipinfo -1 app.war | grep test.properties)` in Ansible. – U880D Feb 01 '22 at 17:22

1 Answers1

0

Regarding your question

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

I understand that you are looking for something like unzip -qq -c app.war $(zipinfo -1 app.war | grep test.properties) in Ansible and I've created a simple minimal example for.

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

U880D
  • 8,601
  • 6
  • 24
  • 40