Re: [ansible-project] How to extract information from string

Skip to first unread message

Vladimir Botka

unread,
Jul 1, 2020, 11:05:31 PM7/1/20
to Jenisha T, ansible...@googlegroups.com
On Wed, 1 Jul 2020 02:43:24 -0700 (PDT)
Jenisha T <joy...@gmail.com> wrote:

> Agent Communication Diagnostic Report
> -----------------------------------------------
>
> Agent User Name : username
> Agent Directory : /path/to/dir
> Agent Platform Architecture : Linux
> Agent Version : x.0.00.x
> Agent Host Name : abcdef1234
> Logical Agent Name : abcdef1234
> Listen to Network Interface : *ANY
>
> Agent processes status
> ======================
> Agent Listener : Running as root

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


--
Vladimir Botka

T. JENISHA

unread,
Jul 2, 2020, 12:55:02 AM7/2/20
to Vladimir Botka, ansible...@googlegroups.com
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"}



Best Regards
Jenisha T
Whatsapp: +91 07598087866




Vladimir Botka

unread,
Jul 2, 2020, 1:38:56 AM7/2/20
to T. JENISHA, ansible...@googlegroups.com
Hi!

On Thu, 2 Jul 2020 10:24:29 +0530
"T. JENISHA" <joy...@gmail.com> wrote:

> 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: |
a: 1
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.

Vladimir Botka

unread,
Jul 2, 2020, 5:24:32 AM7/2/20
to Ansible Project, T. JENISHA
On Thu, 2 Jul 2020 13:59:13 +0530
"T. JENISHA" <joy...@gmail.com> wrote:

> - name: Ex grep
> gather_facts: yes
> hosts: localhost
> connection: local
> tasks:
>
> - name: Run ag_diag_comm
> command: cat "ag_diag.txt"
> register: ag_diag
>
> - 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
>
> fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an

> > Fix it. The code below works as expected. Mind the indentation.
> >
> > - 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

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

--
Vladimir Botka

T. JENISHA

unread,
Jul 2, 2020, 7:56:38 AM7/2/20
to Vladimir Botka, Ansible Project
Thankyou Vladimir. Its working now


Best Regards
Jenisha T
Whatsapp: +91 07598087866




Reply all
Reply to author
Forward
0 new messages