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 16:01:56 +0000 00:38:07.95 nyctea root Ansible 2.15.2 ara 1.6.1 (client), 1.6.1 (server) Python 3.11.4 5 6 825 825 48 1

File: /root/.ansible/collections/ansible_collections/kubeinit/kubeinit/roles/kubeinit_validations/tasks/30_libvirt_check_cpu_cores.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 cores if enough for all machines
#

- name: Get hypervisor total available cores
  ansible.builtin.shell: |
    set -o pipefail
    cat /proc/cpuinfo | grep processor | grep : | wc -l
  args:
    executable: /bin/bash
  register: _result_total_cores
  loop: "{{ groups['all_hosts'] }}"
  delegate_to: "{{ item }}"
  changed_when: "_result_total_cores.rc == 0"

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

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

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

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

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

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

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

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

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

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

- name: Make sure there are enough cores
  ansible.builtin.assert:
    that:
      - kubeinit_validations_hypervisors_available_cores[item.key]|int >= item.value|float / 4
    msg:
      - "It seems there are not enough cores"
      - "Per each 4 virtual cores there must be one physical core"
      - "(Required: {{ kubeinit_validations_libvirt_summarized_cores_usage }} Available: {{ kubeinit_validations_hypervisors_available_cores }})"
  loop: "{{ kubeinit_validations_libvirt_summarized_cores_usage | dict2items }}"
  when: kubeinit_ignore_validation_checks is not defined