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

2020-07-02 Thread T. JENISHA
Thankyou Vladimir. Its working now


Best Regards
Jenisha T
Whatsapp: +91 07598087866





On Thu, 2 Jul 2020 at 14:54, Vladimir Botka  wrote:

> On Thu, 2 Jul 2020 13:59:13 +0530
> "T. JENISHA"  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
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAAGhZYgms5X_vChFry3eLbNZnnodrhVcyawB5GdLutqHbLrgKQ%40mail.gmail.com.


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

2020-07-02 Thread Vladimir Botka
On Thu, 2 Jul 2020 13:59:13 +0530
"T. JENISHA"  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

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200702112417.0974f64d%40gmail.com.


pgpAo2m1blNnq.pgp
Description: OpenPGP digital signature


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

2020-07-01 Thread Vladimir Botka
Hi!

On Thu, 2 Jul 2020 10:24:29 +0530
"T. JENISHA"  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.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200702073841.4aaa0021%40gmail.com.


pgpyvF3aXCeFo.pgp
Description: OpenPGP digital signature


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

2020-07-01 Thread T. JENISHA
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





On Thu, 2 Jul 2020 at 08:35, Vladimir Botka  wrote:

> On Wed, 1 Jul 2020 02:43:24 -0700 (PDT)
> Jenisha T  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
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAAGhZYjkMvFNfLrX1jz9OYphsKt4xw_c9PX3XqJEQDEfEKct6A%40mail.gmail.com.


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

2020-07-01 Thread Vladimir Botka
On Wed, 1 Jul 2020 02:43:24 -0700 (PDT)
Jenisha T  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

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200702050513.1e7886ed%40gmail.com.


pgpgX5BHzNUue.pgp
Description: OpenPGP digital signature