How to extract information from string

Try this (given the string is in variable "my_data")

    - set_fact:
        my_agent: "{{ my_agent|default({})|
                      combine({my_key: my_val}) }}"
      loop: "{{ my_data.splitlines()|
                select('match', my_regex)|
                list }}"
      vars:
        my_regex: '^(.*?):(.*)$'
        my_key: "{{ item|
                    regex_replace(my_regex, '\\1')|
                    trim>
                    regex_replace(' ', '_') }}"
        my_val: "{{ item|
                    regex_replace(my_regex, '\\2')|
                    trim }}"
    - debug:
        var: my_agent
    - debug:
        var: my_agent.Logical_Agent_Name

Hi Vladimir

I get the below error. my_data is input data stored in the register. whats my_key?

fatal: [localhost]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘my_key’ is undefined\n\nThe error appears to be in ‘/home/jenisha/PycharmProjects/ansible_ex/tasks/grep_ex.yml’: line 31, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - set_fact:\n ^ here\n”}

Hi!

my_data is input data stored in the register

If you have problem test the code with simple data first. For example

  vars:
    my_data: |
      b: 2

whats my_key?

It's variable. See https://docs.python.org/3/library/re.html

fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an
undefined variable. The error was: 'my_key' is undefined\n\nThe error
appears to be in
'/home/jenisha/PycharmProjects/ansible_ex/tasks/grep_ex.yml': line 31,

Fix it. The code below works as expected. Mind the indentation.

Fix the indentation. I've put the dots '.' into the code to make the
comparison easier. In addition to this, use your registered data in the loop

       loop: "{{ ag_diag.stdout_lines|

For your convenience the code is also here
https://gist.github.com/vbotka/1e68ef07627dc37b252553b6d838ed20

Thankyou Vladimir. Its working now