将结果加入多个主机的调试输出中

时间:2019-04-10 15:44:05

标签: ansible jinja2 ansible-2.x

我正在尝试输入主机列表,然后代码将检查哪些系统在/中具有大于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"

我希望将输出分组为,而不是空间较小的系统以外,将更多空间的系统分组并显示,例如:-(需要的输出)

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+$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

1 个答案:

答案 0 :(得分:0)

下面的任务创建一个空间小于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

(未经测试)