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
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
load_avg: “{{ up_out.stdout_lines[0].split()[7:] }}”
Just use [7:] to get from position 7 on in your list.
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
An even more elegant solution would be to use ‘[-3:]’ to get the last three items regardless of how many there are.
% cat split.yml
If you want the commas removed you can do that too.
ok: [localhost] => {
“msg”: [
[
“0.02”,
“0.05”,
“0.00”
],
[
“1.08”,
“0.99”,
“1.05”
]
]
}
Walter
Thanks a lot for the valuable input…
Thanks Walter… the options helped me a lot.
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
Excellent find Vladimir! I just shared this with my team. I was unaware of ‘jc’.
Walter
Thanks Vladimir !!!