0

I am trying to input a list of hosts, then the code will check which systems have space more than 1 GB in /, and less than 1GB and display the output. The output I am getting is host wise like this:- Current Output:-

ok: [hostname1.com] => {
    "msg": "hostname1.com : Space is more than 1GB"
}
ok: [hostname2.com] => {
    "msg": "hostname2.com : Space is less than 1GB"
}
ok: [hostname3.com] => {
    "msg": "hostname3.com : Space is more than 1GB"

I want to group the output as, systems whose space is more are grouped and displayed other than systems whose space is less , example:- (Needed Output)

ok: [hosts] => {
    "msg": "hostname1.com : Space is more than 1GB"
           "hostname2.com : Space is more than 1GB"
}
ok: [hosts] => {
    "msg": "hostname3.com : Space is less than 1GB"
           "hostname4.com : Space is less than 1GB"

My code:

    - name: Check the space in /
      shell: df -h /  | grep [0-9]%  | awk '{ print 0+$4 }'
      register: space

    - debug:
       msg: "{{ inventory_hostname }} : Space is more than 1GB"
      when: (space.stdout| int) > 1
    - debug:
       msg: "{{ inventory_hostname }} : Space is less than 1GB"
      when: (space.stdout| int) < 1
rocknrollaki
  • 75
  • 1
  • 4
  • 11

1 Answers1

0

The task below creates a list of hosts with space less than 1G. You might want to format it to your needs.

- name: Create a list of hosts with space less than 1GB
  when: hostvars[item].space.stdout|int < 1
  set_fact:
    hosts_less_1gb: "{{ hosts_less_1gb|default([]) + [ item ] }}"
  loop: "{{ play_hosts }}"
  run_once: true

(not tested)

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63