Playbook #1

/root/kubeinit/ci/builds/6mbKNrxD/0/kubeinit/kubeinit/kubeinit-aux/kubeinit/playbook.yml

Report Status CLI Date Duration Controller User Versions Hosts Plays Tasks Results Files Records
29 Oct 2023 09:29:53 +0000 00:12:50.32 nyctea root Ansible 2.15.2 ara 1.6.1 (client), 1.6.1 (server) Python 3.11.4 6 8 849 849 49 1

File: /root/.ansible/collections/ansible_collections/kubeinit/kubeinit/roles/kubeinit_validations/tasks/20_libvirt_available_ram.yml

---
# Copyright kubeinit contributors
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

#
# Make sure the mount of RAM if enough for all machines
#
- name: Get hypervisor total RAM
  ansible.builtin.shell: |
    set -o pipefail
    free --kilo  | grep ^Mem | tr -s ' ' | cut -d ' ' -f 2
  args:
    executable: /bin/bash
  register: _result_total_ram
  loop: "{{ groups['all_hosts'] }}"
  delegate_to: "{{ item }}"
  changed_when: "_result_total_ram.rc == 0"

- name: Debug
  ansible.builtin.debug:
    var: _result_total_ram

- name: Define the hypervisors ram available dictionary
  ansible.builtin.set_fact:
    kubeinit_validations_hypervisors_free_ram: "{{ kubeinit_validations_hypervisors_free_ram|default([]) | combine( {item.item: item.stdout|int} ) }}"
  loop: "{{ _result_total_ram.results }}"

- name: Debug
  ansible.builtin.debug:
    var: kubeinit_validations_hypervisors_free_ram

- name: Get the total inventory ram usage
  ansible.builtin.set_fact:
    kubeinit_validations_libvirt_ram_usage: "{{ kubeinit_validations_libvirt_ram_usage|default({})| combine( {item: {'id': item, 'ram': hostvars[item].ram|int, 'target': hostvars[item].target} } ) }}"
  loop: "{{ groups['all_guest_vms'] }}"

- name: Debug
  ansible.builtin.debug:
    var: kubeinit_validations_libvirt_ram_usage

- name: Set combined ram requirement per hypervisor
  ansible.builtin.set_fact:
    kubeinit_validations_libvirt_combined_ram_usage: >-
      {{ kubeinit_validations_libvirt_combined_ram_usage | default({})
        | combine({item.value.target: []
        + [{
            'ram': item.value.ram,
            'id': item.value.id
          }]
        + kubeinit_validations_libvirt_combined_ram_usage[item.value.target] | default([]) })
      }}
  loop: "{{ kubeinit_validations_libvirt_ram_usage | dict2items }}"

- name: Debug
  ansible.builtin.debug:
    var: kubeinit_validations_libvirt_combined_ram_usage

- name: Set the summarized ram usage per hypervisor
  ansible.builtin.set_fact:
    kubeinit_validations_libvirt_summarized_ram_usage: "{{ kubeinit_validations_libvirt_summarized_ram_usage|default([]) | combine( {item: my_attribute} ) }}"
  vars:
    my_attribute: "{{ kubeinit_validations_libvirt_combined_ram_usage[item] | map(attribute='ram') | list | sum }}"
  loop: "{{ kubeinit_validations_libvirt_combined_ram_usage.keys() }}"

- name: Debug
  ansible.builtin.debug:
    var: kubeinit_validations_libvirt_summarized_ram_usage

- name: Debug
  ansible.builtin.debug:
    var: item
  loop: "{{ kubeinit_validations_hypervisors_free_ram | dict2items }}"

- name: Make sure there is enough available ram
  ansible.builtin.assert:
    that:
      - kubeinit_validations_hypervisors_free_ram[item.key]|int > item.value|float * 1.1
    msg: "It seems there is not enough ram space (Required: {{ kubeinit_validations_libvirt_summarized_ram_usage }} Available: {{ kubeinit_validations_hypervisors_free_ram }})"
  loop: "{{ kubeinit_validations_libvirt_summarized_ram_usage | dict2items }}"
  when: kubeinit_ignore_validation_checks is not defined