linux Ansible -在CPU负载低于2.0时启动任务

l5tcr1uw  于 2023-06-05  发布在  Linux
关注(0)|答案(2)|浏览(404)

我正在尝试做一个剧本与一些任务。我想perfor一个简单的调试任务后,cpu负载低于2.0。
我在cpu-load.yml中有这样一个例子:

---
- name: Check CPU load and wait
  hosts: localhost
  gather_facts: yes
  
  tasks:
    - name: Check cpu load
      shell: uptime | awk -F 'load average:' '{print $2}' | awk -F ', ' '{print $1}'
      register: cpu_load
      
    - name: Wait until cpu load is bellow 2.0
      wait_for:
        timeout: 300
        delay: 10
        shell: Do something here
        msg: "cpu load is bellow 2.0"
      
    - name: Continue jobs
      debug:
        msg: "CPU load is bellow 2.0. Continue!!!"

现在我不知道如何使任务等待cpu负载去below 2. 0。你们能帮忙吗

6fe3ivhb

6fe3ivhb1#

你需要在你的“check cpu load”任务周围放置一个until循环:

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Check cpu load
      shell: uptime | awk -F 'load average:' '{print $2}' | awk -F ', ' '{print $1}'
      register: cpu_load
      until: cpu_load.stdout|float < 2.0
      retries: 300
      delay: 1

    - name: Some other task
      debug:
        msg: hello world

这将等待最多五分钟(300次重试,延迟1秒),以使负载平均值降至2.0以下。
可能有更好的方法来获得当前1分钟的CPU负载;从/proc/loadavg阅读可能是最简单的:

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Check cpu load
      command: cat /proc/loadavg
      register: cpu_load
      until: cpu_load.stdout.split()|first|float < 2.0
      retries: 300
      delay: 1

    - name: Some other task
      debug:
        msg: hello world
mhd8tkvw

mhd8tkvw2#

建立在@larsks的答案上,并进一步讨论句子
可能有更好的方法来获得当前1分钟的CPU负载
实际上有一个事实包含了这些信息,至少在linux上是这样的,例如。

$ ansible localhost -m setup -a gather_subset='!all,!min,loadavg'
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_loadavg": {
            "15m": 0.669921875,
            "1m": 0.48974609375,
            "5m": 0.4501953125
        },
        "gather_subset": [
            "!all",
            "!min",
            "loadavg"
        ],
        "module_setup": true
    },
    "changed": false
}

注意事项:

  1. gather_subset='!all,!min,loadavg'确保严格地只收集所需的事实(并在下面的剧本中刷新)。有关详细信息,请参阅选项文档
    1.在我写这些行的时候,loadavg子集没有被记录,但是在我的ansible 2.14.6 / 2.15.0版本中,它被列在模块错误消息的允许选项中(见下文)
    知道这一点并应用与@larsk的答案相同的配方,检查可以实现为:
---
- name: Demo play to check load and continue
  hosts: localhost
  gather_facts: false

  tasks:
    - name: Check load before next tasks
      ansible.builtin.setup:
        gather_subset:
          - '!all'
          - '!min'
          - 'loadavg'
      retries: 30
      delay: 5
      until: ansible_loadavg['1m'] < 2.00

    - name: Now do something on a cooled down system
      debug:
        msg: I'm doing something without pressure

关于setup模块的未记录子集,这里有一个快速而肮脏的解决方案,可以列出您的模块版本中所有接受的子集:

$ ansible localhost -m setup -a gather_subset='toto'
localhost | FAILED! => {
    "changed": false,
    "msg": "Bad subset 'toto' given to Ansible. gather_subset options allowed: all, all_ipv4_addresses, all_ipv6_addresses, apparmor, architecture, caps, chroot, cmdline, date_time, default_ipv4, default_ipv6, devices, distribution, distribution_major_version, distribution_release, distribution_version, dns, effective_group_ids, effective_user_id, env, facter, fibre_channel_wwn, fips, hardware, interfaces, is_chroot, iscsi, kernel, kernel_version, loadavg, local, lsb, machine, machine_id, mounts, network, nvme, ohai, os_family, pkg_mgr, platform, processor, processor_cores, processor_count, python, python_version, real_user_id, selinux, service_mgr, ssh_host_key_dsa_public, ssh_host_key_ecdsa_public, ssh_host_key_ed25519_public, ssh_host_key_rsa_public, ssh_host_pub_keys, ssh_pub_keys, system, system_capabilities, system_capabilities_enforced, user, user_dir, user_gecos, user_gid, user_id, user_shell, user_uid, virtual, virtualization_role, virtualization_tech_guest, virtualization_tech_host, virtualization_type"
}

相关问题