Home > Back-end >  Extract mulitline regexp and parse newlines when needed
Extract mulitline regexp and parse newlines when needed

Time:11-03

I am trying to extract 2 lines (multiple times) from a specific multilines log including accented caracters (I cannot change the log).
(the raw log file is available for consultation, but I want to show a summary of the pb from ansible playbook logs).

The logs I am trying to parse look like :

_
02/11/2021 17:14:20;...
02/11/2021 17:14:20;==================================
02/11/2021 17:14:20;Starting test for : OUTBOUND RabbitMQ
02/11/2021 17:14:51;Error validating properties for broker : OUTBOUND RabbitMQ
java.lang.Exception: ERREUR::Exception lors de l'ouverture du channel[Connection timed out: connect]

at com.mom.utils.ClientUtils.openChannel(ClientUtils.java:80) ~[classes/:na]
at com.mom.properties.RabbitMqOutboundProperties.testConfig(RabbitMqOutboundProperties.java:61) ~[classes/:na]
at com.mom.BrokerConfigurationTester.main(BrokerConfigurationTester.java:73) ~[classes/:na]
02/11/2021 17:14:51;==========================================
02/11/2021 17:14:51;...
_

For that, I have an ansible task that looks like that :

 - name: showing Errors and Exception
   debug:
     msg: "{{ item }}"
   with_items:
    - "{{ test_result.stdout_lines| string | regex_findall ('(Error validating properties for broker :)[a-zA-Z0-9 '']*$.^((?!Exception).)*Exception:[^\n]*',multiline=True) }}"
# one test among many

Unfortunately, what works from regexp tester or here, does not from Ansible regexp_findall filter.

My main pb is that I (think I) need "multiline=true" in order to catch the 2 lines in a row, but then I cannot stop the catch at the end of the line (because basically ".*$" will not stop at then end of the line but at the end of the text).
And as far as I know, "all but new line" ([^\n]*) is not going to work from ansible regexp (or not the way I tried)

I have tried all the solutons from this post, but they do not work from ansible filter.
Accented caracters cause encoding error (not "UTF8"), and caracters class \u00C0 are treated like range ("from \u to 0")
So basically, I am trying to find how I can perform this with ansible (and maybe underlying python?)

Any tip?

CodePudding user response:

i suggest you to use stdout instead stdout_lines:

- name: test regex
  hosts: localhost

  tasks:
    - name: trap the log in register
      command: cat logs/testfile.log 
      register: output

    - name: Print output
      debug: 
        var: output 

    - name: build log
      set_fact: 
        log: "{{ output.stdout | regex_findall('([0-9 :/;] Error validating properties for broker :. )\n(. )') }}"

    - name: Print log
      debug: 
        msg: "{{ item }}" 
      loop: "{{ log }}"

result:

ok: [localhost] => (item=['02/11/2021 17:14:51;Error validating properties for broker : OUTBOUND RabbitMQ', "java.lang.Exception: ERREUR::Exception lors de l'ouverture du channel[Connection timed out: connect]"]) => 
  msg:
  - '02/11/2021 17:14:51;Error validating properties for broker : OUTBOUND RabbitMQ'
  - 'java.lang.Exception: ERREUR::Exception lors de l''ouverture du channel[Connection timed out: connect]'
ok: [localhost] => (item=['02/11/2021 17:14:53;Error validating properties for broker : INBOUND MqSeries', "com.ibm.msg.client.jms.DetailedIllegalStateException: JMSWMQ0018: Echec de la connexion au gestionnaire de files d'attente 'QMFINOUT' à l'aide du mode de connexion 'Client' et du nom d'hôte 'Client'."]) => 
  msg:
  - '02/11/2021 17:14:53;Error validating properties for broker : INBOUND MqSeries'
  - 'com.ibm.msg.client.jms.DetailedIllegalStateException: JMSWMQ0018: Echec de la connexion au gestionnaire de files d''attente ''QMFINOUT'' à l''aide du mode de connexion ''Client'' et du nom d''hôte ''Client''.'
  • Related