将结果连接到多个主机的调试输出 - ansible

问题描述 投票:0回答:1

我正在尝试输入主机列表,然后代码将检查哪些系统的空间大于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
ansible jinja2 ansible-2.x
1个回答
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

(未测试)

© www.soinside.com 2019 - 2024. All rights reserved.