Re: [ansible-project] split with multiple columns in debug

2023-02-28 Thread Veera
Thanks Vladimir  !!!

On Tuesday, February 28, 2023 at 12:41:46 PM UTC+5:30 Vladimir Botka wrote:

> On Mon, 27 Feb 2023 21:07:45 -0800 (PST)
> Veera  wrote:
>
> > load_avg: "{{ up_out.stdout_lines[0].split()[7:] }}"
>
> Given the registered variable *up_out*
>
> - command: uptime
> register: up_out
>
> Use the filter community.general.jc to parse the stdout. The utility
> *jc* "converts the output of many commands, file-types, and strings
> to JSON or YAML". For example,
>
> uptime: "{{ up_out.stdout|community.general.jc('uptime') }}"
>
> gives
>
> uptime:
> load_15m: 1.21
> load_1m: 1.84
> load_5m: 1.41
> time: 07:57:41
> time_hour: 7
> time_minute: 57
> time_second: 41
> uptime: 11 days, 18:12
> uptime_days: 11
> uptime_hours: 18
> uptime_minutes: 12
> uptime_total_seconds: 1015920
> users: 2
>
> The first three attributes come from "load average". Quoting from man
> uptime: "The averages are taken over the three time intervals." Now,
> the usage is trivial. For example,
>
> load_average: >
> {{ uptime.load_1m }},
> {{ uptime.load_5m}},
> {{ uptime.load_15m }}
>
> gives
>
> load_average: |-
> 1.84, 1.41, 1.21
>
> -- 
> 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/5e9ee466-5e48-4e6a-87f9-0e67834443a8n%40googlegroups.com.


Re: [ansible-project] split with multiple columns in debug

2023-02-28 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
Excellent find Vladimir! I just shared this with my team. I was unaware of 'jc'.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Feb 28, 2023, at 2:11 AM, Vladimir Botka  wrote:

On Mon, 27 Feb 2023 21:07:45 -0800 (PST)
Veera  wrote:

load_avg: "{{ up_out.stdout_lines[0].split()[7:] }}"

Given the registered variable *up_out*

 - command: uptime
   register: up_out

Use the filter community.general.jc to parse the stdout. The utility
*jc* "converts the output of many commands, file-types, and strings
to JSON or YAML". For example,

 uptime: "{{ up_out.stdout|community.general.jc('uptime') }}"

gives

 uptime:
   load_15m: 1.21
   load_1m: 1.84
   load_5m: 1.41
   time: 07:57:41
   time_hour: 7
   time_minute: 57
   time_second: 41
   uptime: 11 days, 18:12
   uptime_days: 11
   uptime_hours: 18
   uptime_minutes: 12
   uptime_total_seconds: 1015920
   users: 2

The first three attributes come from "load average". Quoting from man
uptime: "The averages are taken over the three time intervals." Now,
the usage is trivial. For example,

 load_average: >
   {{ uptime.load_1m }},
   {{ uptime.load_5m}},
   {{ uptime.load_15m }}

gives

 load_average: |-
   1.84, 1.41, 1.21

--
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/20230228081123.140af349%40gmail.com.

-- 
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/D209028D-24B1-4996-86A2-83EA001FBDB3%40nist.gov.


Re: [ansible-project] split with multiple columns in debug

2023-02-27 Thread Vladimir Botka
On Mon, 27 Feb 2023 21:07:45 -0800 (PST)
Veera  wrote:

> load_avg: "{{ up_out.stdout_lines[0].split()[7:] }}"

Given the registered variable *up_out*

  - command: uptime
register: up_out

Use the filter community.general.jc to parse the stdout. The utility
*jc* "converts the output of many commands, file-types, and strings
to JSON or YAML". For example,

  uptime: "{{ up_out.stdout|community.general.jc('uptime') }}"

gives

  uptime:
load_15m: 1.21
load_1m: 1.84
load_5m: 1.41
time: 07:57:41
time_hour: 7
time_minute: 57
time_second: 41
uptime: 11 days, 18:12
uptime_days: 11
uptime_hours: 18
uptime_minutes: 12
uptime_total_seconds: 1015920
users: 2

The first three attributes come from "load average". Quoting from man
uptime: "The averages are taken over the three time intervals." Now,
the usage is trivial. For example,

  load_average: >
{{ uptime.load_1m }},
{{ uptime.load_5m}},
{{ uptime.load_15m }}

gives

  load_average: |-
1.84, 1.41, 1.21

-- 
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/20230228081123.140af349%40gmail.com.


pgpPtlL5zDkW9.pgp
Description: OpenPGP digital signature


Re: [ansible-project] split with multiple columns in debug

2023-02-27 Thread Veera
Thanks Walter.. the options helped me a lot. 

On Monday, February 27, 2023 at 11:49:05 PM UTC+5:30 Rowe, Walter P. (Fed) 
wrote:

> If you want the commas removed you can do that too.
>
>   - "{{ text_to_split1.replace(',','').split()[-3:] }}"
>   - "{{ text_to_split2.replace(',','').split()[-3:] }}"
>
> ok: [localhost] => {
> "msg": [
> [
> "0.02",
> "0.05",
> "0.00"
> ],
> [
> "1.08",
> "0.99",
> "1.05"
> ]
> ]
> }
>
>
> Walter
> --
> Walter Rowe, Division Chief
> Infrastructure Services, OISM
>

-- 
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/42fc8d28-fbd3-44a1-8e56-54dd9e4783dan%40googlegroups.com.


Re: [ansible-project] split with multiple columns in debug

2023-02-27 Thread Veera
Thanks a lot for the valuable input..

On Monday, February 27, 2023 at 11:30:52 PM UTC+5:30 Hearn, Stan J. wrote:

> Veera,
>
>  
>
> Be careful splitting uptime on white space because when a system is up 
> less than 24 hours, it will show 
>
>  
>
> 12:58:10 up  6:51,  0 users,  load average: 1.08, 0.99, 1.05
>
>  
>
> which will have one less white space and your desired output will be 
> position 6.
>
>  
>
> I would recommend splitting the output on comma.   The fourth value will 
> always be load average.
>
>  
>
> Regards,
>
> Stan
>
>  
>
> *From:* 'Rowe, Walter P. (Fed)' via Ansible Project <
> ansible...@googlegroups.com> 
> *Sent:* Monday, February 27, 2023 12:28 PM
> *To:* ansible...@googlegroups.com
> *Subject:* [EXTERNAL] Re: [ansible-project] split with multiple columns 
> in debug
>
>  
>
> load_avg: "{{ up_out.stdout_lines[0].split()[7:] }}"
>
>  
>
> Just use [7:] to get from position 7 on in your list.
>
>  
>
>
>
> ---
>
> - name: test splitting text
>
>   hosts: localhost
>
>   become: false
>
>   gather_facts: false
>
>   vars:
>
> text_to_split: "18:37:01 up 5 days,  4:37,  2 users,  load average: 
> 0.02, 0.05, 0.00"
>
>   tasks:
>
> - name: get the load average text
>
>   debug:
>
> msg: "{{ text_to_split.split()[7:] }}"
>
>
>
>
>
>
>
>
>
>
>
>
>
> *% ansible-playbook -i localhost, split.yml*
>
>  
>
> PLAY [test splitting text] 
> *
>
>  
>
> TASK [get the load average text] 
> ***
>
> ok: [localhost] => {
>
> "msg": [
>
> "load",
>
> "average:",
>
> "0.02,",
>
> "0.05,",
>
> "0.00"
>
> ]
>
> }
>
>  
>
> PLAY RECAP 
> *
>
> localhost  : ok=1changed=0unreachable=0
> failed=0skipped=0rescued=0ignored=0   
>
>  
>
>
> Walter
> --
> Walter Rowe, Division Chief
> Infrastructure Services, OISM
> Mobile: 202.355.4123 <(202)%20355-4123>
>
>
>
> On Feb 27, 2023, at 11:46 AM, Veera  wrote:
>
>  
>
> Hi, 
>
>  
>
> When I try to split the output of uptime to capture the "load average from 
> the below line
>
>  
>
> $ uptime
>  18:37:01 up 5 days,  4:37,  2 users,  load average: 0.02, 0.05, 0.00
>
>  
>
> using the below playbook 
>
>  
>
> ---
> - name: uptime_collect22
>   hosts: localhost
>   gather_facts: no
>   tasks:
> - name:  register the uptime
>   shell: "uptime"
>   register: up_out
>
> - set_fact:
> #load_avg: "{{ up_out.stdout_lines[0].split()[[7],[8],[9],[10]] }}"
> load_avg: "{{ up_out.stdout_lines[0].split()[7] }}"
>
> - name: print the load average
>   debug:
> var: load_avg
>
>  
>
> when i execute i got the output debug as 
>
> TASK [print the   load average  ] 
> ***
> ok: [my-client-2] => {
> "load_avg": "load"
> }
>
>  
>
> I can split the stdout lines . How can I split/print with  multiple 
> columns similar to awk '{print $7,$8,$9,$10}'  .?
>
> Expected output:  "load average: 0.02, 0.05, 0.00"  
>
>  
>
> I can achieve it with shell module with uptime |awk '{print $8,$9,$10, 
> $11, $12}'
>
> However I want to check the  ansible filtering and split 
>
>  
>
>  
>
>  
>
> -- 
> 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-proje...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/31258954-4fc0-4ef7-91b1-2fb7f92b5066n%40googlegroups.com
>  
> <https://urldefense.com/v3/__https:/gcc02.safelinks.protection.outlook.com/?url=https*3A*2F*2Fgroups.google.com*2Fd*2Fmsgid*2Fansible-project*2F31258954-4fc0-4ef7-91b1-2fb7f92b5066n*2540googlegroups.

Re: [ansible-project] split with multiple columns in debug

2023-02-27 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
If you want the commas removed you can do that too.

  - "{{ text_to_split1.replace(',','').split()[-3:] }}"
  - "{{ text_to_split2.replace(',','').split()[-3:] }}"

ok: [localhost] => {
"msg": [
[
"0.02",
"0.05",
"0.00"
],
[
"1.08",
"0.99",
"1.05"
]
]
}


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM

-- 
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/B3FDACA1-CDB0-4C1C-B1D7-0AD7564C6186%40nist.gov.


Re: [ansible-project] split with multiple columns in debug

2023-02-27 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
% cat split.yml
---
- name: test splitting text
  hosts: localhost
  become: false
  gather_facts: false
  vars:
text_to_split1: "18:37:01 up 5 days,  4:37,  2 users,  load average: 0.02, 
0.05, 0.00"
text_to_split2: "12:58:10 up  6:51,  0 users,  load average: 1.08, 0.99, 
1.05"
  tasks:
- name: get the load average text
  debug:
msg:
  - "{{ text_to_split1.split()[-3:] }}"
  - "{{ text_to_split2.split()[-3:] }}"


% ansible-playbook -i localhost, split.yml

PLAY [test splitting text] 
*

TASK [get the load average text] 
***
ok: [localhost] => {
"msg": [
[
"0.02,",
"0.05,",
"0.00"
],
[
"1.08,",
"0.99,",
"1.05"
]
]
}

PLAY RECAP 
*
localhost  : ok=1changed=0unreachable=0failed=0
skipped=0rescued=0ignored=0

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM

-- 
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/6A84D45C-C18C-455C-9AB8-CE30D18B1D00%40nist.gov.


Re: [ansible-project] split with multiple columns in debug

2023-02-27 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
An even more elegant solution would be to use '[-3:]' to get the last three 
items regardless of how many there are.

---
- name: test splitting text
  hosts: localhost
  become: false
  gather_facts: false
  vars:
text_to_split: "18:37:01 up 5 days,  4:37,  2 users,  load average: 0.02, 
0.05, 0.00"
  tasks:
- name: get the load average text
  debug:
msg: "{{ text_to_split.split()[-3:] }}"



% ansible-playbook -i localhost, split.yml

PLAY [test splitting text] 
*

TASK [get the load average text] 
***
ok: [localhost] => {
"msg": [
"0.02,",
"0.05,",
"0.00"
]
}

PLAY RECAP 
*
localhost  : ok=1changed=0unreachable=0failed=0
skipped=0rescued=0ignored=0

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

-- 
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/F928F5C5-E013-4092-A47B-FCC5480E82C3%40nist.gov.


RE: [ansible-project] split with multiple columns in debug

2023-02-27 Thread 'Hearn, Stan J.' via Ansible Project
Veera,

Be careful splitting uptime on white space because when a system is up less 
than 24 hours, it will show

12:58:10 up  6:51,  0 users,  load average: 1.08, 0.99, 1.05

which will have one less white space and your desired output will be position 6.

I would recommend splitting the output on comma.   The fourth value will always 
be load average.

Regards,
Stan

From: 'Rowe, Walter P. (Fed)' via Ansible Project 

Sent: Monday, February 27, 2023 12:28 PM
To: ansible-project@googlegroups.com
Subject: [EXTERNAL] Re: [ansible-project] split with multiple columns in debug

load_avg: "{{ up_out.stdout_lines[0].split()[7:] }}"

Just use [7:] to get from position 7 on in your list.




---

- name: test splitting text

  hosts: localhost

  become: false

  gather_facts: false

  vars:

text_to_split: "18:37:01 up 5 days,  4:37,  2 users,  load average: 0.02, 
0.05, 0.00"

  tasks:

- name: get the load average text

  debug:

msg: "{{ text_to_split.split()[7:] }}"













% ansible-playbook -i localhost, split.yml



PLAY [test splitting text] 
*



TASK [get the load average text] 
***

ok: [localhost] => {

"msg": [

"load",

"average:",

"0.02,",

"0.05,",

"0.00"

]

}



PLAY RECAP 
*

localhost  : ok=1changed=0unreachable=0failed=0
skipped=0rescued=0ignored=0



Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123


On Feb 27, 2023, at 11:46 AM, Veera  wrote:

Hi,

When I try to split the output of uptime to capture the "load average from the 
below line

$ uptime
 18:37:01 up 5 days,  4:37,  2 users,  load average: 0.02, 0.05, 0.00

using the below playbook

---
- name: uptime_collect22
  hosts: localhost
  gather_facts: no
  tasks:
- name:  register the uptime
  shell: "uptime"
  register: up_out

- set_fact:
#load_avg: "{{ up_out.stdout_lines[0].split()[[7],[8],[9],[10]] }}"
load_avg: "{{ up_out.stdout_lines[0].split()[7] }}"

- name: print the load average
  debug:
var: load_avg

when i execute i got the output debug as
TASK [print the   load average  ] 
***
ok: [my-client-2] => {
"load_avg": "load"
}

I can split the stdout lines . How can I split/print with  multiple columns 
similar to awk '{print $7,$8,$9,$10}'  .?
Expected output:  "load average: 0.02, 0.05, 0.00"

I can achieve it with shell module with uptime |awk '{print $8,$9,$10, $11, 
$12}'
However I want to check the  ansible filtering and split



--
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<mailto:ansible-project+unsubscr...@googlegroups.com>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/31258954-4fc0-4ef7-91b1-2fb7f92b5066n%40googlegroups.com<https://urldefense.com/v3/__https:/gcc02.safelinks.protection.outlook.com/?url=https*3A*2F*2Fgroups.google.com*2Fd*2Fmsgid*2Fansible-project*2F31258954-4fc0-4ef7-91b1-2fb7f92b5066n*2540googlegroups.com*3Futm_medium*3Demail*26utm_source*3Dfooter&data=05*7C01*7Cwalter.rowe*40nist.gov*7C54ba8576ec0b40e63dfd08db18e23fba*7C2ab5d82fd8fa4797a93e054655c61dec*7C1*7C0*7C638131132250539784*7CUnknown*7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0*3D*7C3000*7C*7C*7C&sdata=OTm3zMPEJmid3Y*2B38zRQiCYoN*2FOuSzGjEN*2B2p94yT3U*3D&reserved=0__;JSUlJSUlJSUlJSUlJSUlJSUlJSUlJSUlJSUlJSUlJQ!!Fto3Xw!ustaILJ31tnDLrQ49tlAS1IVT343dBG4LnrxDkuQ4fv0P1glM5u_2lyoFFfQ3dzZocd7wHgKMKIdGaX4XW-eZX2dTfmV4Rw$>.

--
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<mailto:ansible-project+unsubscr...@googlegroups.com>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/A3889D94-A02D-4C35-891F-693D087D8459%40nist.gov<https://urldefense.com/v3/__https:/groups.google.com/d/msgid/ansible-project/A3889D94-A02D-4C35-891F-693D087D8459*40nist.gov?utm_medium=email&utm_source=footer__;JQ!!Fto3Xw!ustaI

Re: [ansible-project] split with multiple columns in debug

2023-02-27 Thread 'Rowe, Walter P. (Fed)' via Ansible Project
load_avg: "{{ up_out.stdout_lines[0].split()[7:] }}"

Just use [7:] to get from position 7 on in your list.



---

- name: test splitting text

  hosts: localhost

  become: false

  gather_facts: false

  vars:

text_to_split: "18:37:01 up 5 days,  4:37,  2 users,  load average: 0.02, 
0.05, 0.00"

  tasks:

- name: get the load average text

  debug:

msg: "{{ text_to_split.split()[7:] }}"







% ansible-playbook -i localhost, split.yml


PLAY [test splitting text] 
*


TASK [get the load average text] 
***

ok: [localhost] => {

"msg": [

"load",

"average:",

"0.02,",

"0.05,",

"0.00"

]

}


PLAY RECAP 
*

localhost  : ok=1changed=0unreachable=0failed=0
skipped=0rescued=0ignored=0


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM
Mobile: 202.355.4123

On Feb 27, 2023, at 11:46 AM, Veera  wrote:

Hi,

When I try to split the output of uptime to capture the "load average from the 
below line

$ uptime
 18:37:01 up 5 days,  4:37,  2 users,  load average: 0.02, 0.05, 0.00

using the below playbook

---
- name: uptime_collect22
  hosts: localhost
  gather_facts: no
  tasks:
- name:  register the uptime
  shell: "uptime"
  register: up_out

- set_fact:
#load_avg: "{{ up_out.stdout_lines[0].split()[[7],[8],[9],[10]] }}"
load_avg: "{{ up_out.stdout_lines[0].split()[7] }}"

- name: print the load average
  debug:
var: load_avg

when i execute i got the output debug as
TASK [print the   load average  ] 
***
ok: [my-client-2] => {
"load_avg": "load"
}

I can split the stdout lines . How can I split/print with  multiple columns 
similar to awk '{print $7,$8,$9,$10}'  .?
Expected output:  "load average: 0.02, 0.05, 0.00"

I can achieve it with shell module with uptime |awk '{print $8,$9,$10, $11, 
$12}'
However I want to check the  ansible filtering and split



--
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/31258954-4fc0-4ef7-91b1-2fb7f92b5066n%40googlegroups.com.

-- 
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/A3889D94-A02D-4C35-891F-693D087D8459%40nist.gov.