regex – Extracting part of the string using Ansible regex_search and save the output as a variable


I’m having a content like below inside a file.

dataDir=/var/lib/zookeeper
4lw.commands.whitelist=mntr,conf,ruok,stat
syncLimit=2

I wanted to read the value for dataDir using Ansible and set it to a variable. I have written following code but regular expression and storing the variable both have some issues.

  - name: Read zoo.cfg content
    shell:
      cmd: cat zoo.cfg
    register: zoo_config_content

  - set_fact:
      my_var: "{{ zoo_config_content.stdout | regex_search('dataDir.*')}}"

  - name: Print
    debug:
      var: my_var

Q1.) How can I improve the regular expression to get only /var/lib/zookeeper ?

Q2.) How can I store that extracted value to use in another task?