# How to extract  information from string

**URL:** <https://forum.ansible.com/t/how-to-extract-information-from-string/33301>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [July 2, 2020, 3:05am UTC](https://forum.ansible.com/t/how-to-extract-information-from-string/33301 "2020-07-02T03:05:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 2, 2020, 3:05am UTC](https://forum.ansible.com/t/how-to-extract-information-from-string/33301/1 "2020-07-02T03:05:13Z")

</div>

Try this (given the string is in variable "my\_data")

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_agent: "{{ my\_agent|default({})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({my\_key: my\_val}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_data.splitlines()|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;select('match', my\_regex)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_regex: '^(.\*?):(.\*)$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_key: "{{ item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regex\_replace(my\_regex, '\\1')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trim\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regex\_replace(' ', '\_') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_val: "{{ item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regex\_replace(my\_regex, '\\2')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trim }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_agent  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_agent.Logical\_Agent\_Name

---

<div class="post-metadata">

**Author:** ![Jenisha\_T](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@Jenisha\_T](https://forum.ansible.com/u/Jenisha_T)\
**Post date:** [July 2, 2020, 4:54am UTC](https://forum.ansible.com/t/how-to-extract-information-from-string/33301/2 "2020-07-02T04:54:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 2, 2020, 5:38am UTC](https://forum.ansible.com/t/how-to-extract-information-from-string/33301/3 "2020-07-02T05:38:41Z")

</div>

Hi!

> my\_data is input data stored in the register

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

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;my\_data: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b: 2

> whats my\_key?

It's variable. See [https://docs.python.org/3/library/re.html](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.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 2, 2020, 9:24am UTC](https://forum.ansible.com/t/how-to-extract-information-from-string/33301/4 "2020-07-02T09:24:17Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ ag\_diag.stdout\_lines|

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

---

<div class="post-metadata">

**Author:** ![Jenisha\_T](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@Jenisha\_T](https://forum.ansible.com/u/Jenisha_T)\
**Post date:** [July 2, 2020, 11:56am UTC](https://forum.ansible.com/t/how-to-extract-information-from-string/33301/5 "2020-07-02T11:56:06Z")

</div>

Thankyou Vladimir. Its working now
