Q:

Ansible parse for a specific line in resulted registered var

Under my ansible playbook , i ve a task which execute a script:
- name: Execute Update on selected databases
  shell: "./script_update.sh server007 {{item}} {{bdd_config[item].updateFile }}"
  with_items: "{{selected_DBS}}"
  register: updateResult

the result of this script ca be something like this:
    INFO   - ================================================================================
    INFO   - BEGIN 'script_update' ON ini99db1 AT 2019/05/22 12:22:06
    INFO   - ================================================================================
    INFO   - THE MySQL SERVER server007 EXISTS
    INFO   - THE MySQL SERVER server007 IS ON
    INFO   - THE DATABASE myDB EXISTS
    INFO   - FILE /opt/myscode_In_progress.sql EXISTS.
    ERROR  - ERROR 1064 (42000) at line 4 in file: '/opt/myscode_In_progress.sql': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'azdazdazdazdazd' at line 1
    INFO   - SCRIPT OUTPUT : SEE LOG FILE 1
    INFO   - THE DB HAS BEEN CORRECTLY UPDATED
    INFO   - --------------------------------------------------------------------------------
    INFO   - THE PROCESS TERMINATED SUCCESSFULLY
    INFO   - SEE THE LOG FILE /opt/mysql/log/app_20190522_12H22.log

When Displaying the output of my register variable (updateResult) , it gives something like this :
"msg": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "_ansible_ignore_errors": null, 
                "_ansible_item_label": "nomadisdb", 
                "_ansible_item_result": true, 
                "_ansible_no_log": false, 
                "_ansible_parsed": true, 
                "changed": true, 
                "cmd": "./rcd_db_update.ksh myserver01 nomadisdb nomadisdb_In_progress.sql", 
                "delta": "0:00:12.786607", 
                "end": "2019-05-22 12:36:52.817077", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "./rcd_db_update.ksh myserver01 nomadisdb nomadisdb_In_progress.sql", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": "/opt/application/i99/current/sh", 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "warn": true
                    }
                }, 
                "item": "nomadisdb", 
                "rc": 0, 
                "start": "2019-05-22 12:36:40.030470",  
                "stdout_lines": [
                    "\tINFO   - ================================================================================", 
                    "\tINFO   - BEGIN 'rcd_db_update' ON ini99db1 AT 2019/05/22 12:36:50", 
                    "\tINFO   - ================================================================================", 
                    "\tINFO   - THE MySQL SERVER myserver01 EXISTS",  
                    "\tINFO   - THE MySQL SERVER myserver01 IS ON", 
                    "\tINFO   - THE DATABASE nomadisdb EXISTS", 
                    "\tINFO   - FILE /opt/application/i99/current/sql/nomadisdb_In_progress.sql EXISTS.", 
                    "\tERROR  - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'", 
                    "\tINFO   - SCRIPT OUTPUT : SEE LOG FILE 1",  
                    "\tINFO   - THE DB HAS BEEN CORRECTLY UPDATED", 
                    "\tINFO   - --------------------------------------------------------------------------------",  
                    "\tINFO   - THE PROCESS TERMINATED SUCCESSFULLY", 
                    "\tINFO   - SEE THE LOG FILE /opt/mysql/log/rcd_db_update_myserver01_nomadisdb_20190522_12H36.log", 
                    "\tINFO   - ================================================================================", 
                    "\tINFO   - END 'rcd_db_update.ksh' ON ini99db1 AT 2019/05/22 12:36:50",  
                    "\tINFO   - ================================================================================"
                ]
            }
        ]
    }
}

My purpose is to grep over this output msg and search fo any ERROR line , like this :
"\tERROR  - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'", 

i may search it with a regExp which looks for lines that start with "ERROR"  and includes  "at line" and "in file"
Finally i should get this whole line as a result
I ve tried to use a regexp like this: 
- name: set regex
  set_fact:
    regExpOfSqlError: '    '
  become_user: mysql

- set_fact:
    errorLine: "{{ updateResult.results | regex_search(regExpOfSqlError, '  ') }}" 
  become_user: mysql

But i still wonder what RegExp should i put , to search forthose lines , and ouput them
Suggestions ?

A:

A simple solution is to use the json_query filter. 
Notes:

json_query is dependent on jmespath. You will need to pip(3) install jmespath prior to running the example.
The to_json | from_json hack is needed to work arround a known bug when using jmespath contains function in ansible. This needs a modification on jmespath side still waiting for approval/development
You should read the jmespath documentation for further details, but here is a quick explanation of the query string in my example: create a list projection of all input elements (updateResults.result), then select all elements in stdout_lines entry containing text ERROR and finally project the result as a flat list.

---
- name: SO Test
  hosts: localhost

  vars:
    # This result var mimics your current captured result
    # I only kept the relevant part for the example.
    updateResult:
      results:
        - stdout_lines: [
            "\tINFO   - ================================================================================", 
            "\tINFO   - BEGIN 'rcd_db_update' ON ini99db1 AT 2019/05/22 12:36:50", 
            "\tINFO   - ================================================================================", 
            "\tINFO   - THE MySQL SERVER myserver01 EXISTS",  
            "\tINFO   - THE MySQL SERVER myserver01 IS ON", 
            "\tINFO   - THE DATABASE nomadisdb EXISTS", 
            "\tINFO   - FILE /opt/application/i99/current/sql/nomadisdb_In_progress.sql EXISTS.", 
            "\tERROR  - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'", 
            "\tINFO   - SCRIPT OUTPUT : SEE LOG FILE 1",  
            "\tINFO   - THE DB HAS BEEN CORRECTLY UPDATED", 
            "\tINFO   - --------------------------------------------------------------------------------",  
            "\tINFO   - THE PROCESS TERMINATED SUCCESSFULLY", 
            "\tINFO   - SEE THE LOG FILE /opt/mysql/log/rcd_db_update_myserver01_nomadisdb_20190522_12H36.log", 
            "\tINFO   - ================================================================================", 
            "\tINFO   - END 'rcd_db_update.ksh' ON ini99db1 AT 2019/05/22 12:36:50",  
            "\tINFO   - ================================================================================"
          ]
  tasks:
    - name: Capture a list of all lines containing an error in all results
      set_fact:
        error_lines: "{{ updateResult.results | to_json | from_json | json_query(\"[].stdout_lines[?contains(@, 'ERROR')][]\")  }}"

    - name: Show lines with errors
      debug:
        var: error_lines

Which gives:
PLAY [SO Test] *****************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [Capture a list of all lines containing an error in all results] **********
ok: [localhost]

TASK [Show lines with errors] **************************************************
ok: [localhost] => {
    "error_lines": [
        "\tERROR  - ERROR 1060 (42S21) at line 4 in file: '/opt/myDB_In_progress.sql': Duplicate column name 'con_habilitation_version'"
    ]
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0 

