在来自多个主机的调试输出中加入结果 - ansible

Join results in debug output from multiple hosts- ansible

我正在尝试输入主机列表,然后代码将检查哪些系统 space 在 / 中有超过 1 GB 和少于 1GB 的空间并显示输出。 我得到的输出是主机明智的,就像这样:- 当前输出:-

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"

我想将输出分组为 space 更多的系统被分组并显示 space 更少的系统,示例:- (需要输出)

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"

我的代码:

    - name: Check the space in /
      shell: df -h /  | grep [0-9]%  | awk '{ print 0+ }'
      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

下面的任务创建一个 space 小于 1G 的主机列表。您可能希望根据需要对其进行格式化。

- 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

(未测试)