split with multiple columns in debug

22 views
Skip to first unread message

Veera

unread,
Feb 27, 2023, 11:46:54 AM2/27/23
to Ansible Project
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 


Rowe, Walter P. (Fed)

unread,
Feb 27, 2023, 12:27:56 PM2/27/23
to ansible...@googlegroups.com
        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=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=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-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.

Hearn, Stan J.

unread,
Feb 27, 2023, 1:00:52 PM2/27/23
to ansible...@googlegroups.com

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

Rowe, Walter P. (Fed)

unread,
Feb 27, 2023, 1:09:27 PM2/27/23
to ansible...@googlegroups.com
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=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Rowe, Walter P. (Fed)

unread,
Feb 27, 2023, 1:12:59 PM2/27/23
to ansible...@googlegroups.com
% 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=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Rowe, Walter P. (Fed)

unread,
Feb 27, 2023, 1:19:05 PM2/27/23
to ansible...@googlegroups.com
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"
        ]
    ]
}

Veera

unread,
Feb 28, 2023, 12:07:45 AM2/28/23
to Ansible Project
Thanks a lot for the valuable input..

Veera

unread,
Feb 28, 2023, 12:24:52 AM2/28/23
to Ansible Project
Thanks Walter.. the options helped me a lot. 

Vladimir Botka

unread,
Feb 28, 2023, 2:11:46 AM2/28/23
to Veera, ansible...@googlegroups.com
On Mon, 27 Feb 2023 21:07:45 -0800 (PST)
Veera <svee...@gmail.com> 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

Rowe, Walter P. (Fed)

unread,
Feb 28, 2023, 7:53:42 AM2/28/23
to ansible...@googlegroups.com
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
--
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.

Veera

unread,
Mar 1, 2023, 1:38:11 AM3/1/23
to Ansible Project
Thanks Vladimir  !!!
Reply all
Reply to author
Forward
0 new messages