---
# 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.
#
# Gather kubeinit facts
#
- name: Load kubeinit_defaults group
ansible.builtin.add_host:
name: 'kubeinit-defaults'
groups: 'kubeinit_defaults'
- name: Gather kubeinit secrets
ansible.builtin.include_role:
name: kubeinit.kubeinit.kubeinit_prepare
tasks_from: gather_kubeinit_secrets.yml
public: true
vars:
_param_secret_names:
- dockerhub-username
- dockerhub-password
- openshift-pullsecret
- name: Parse kubeinit_network_spec into a dictionary when defined
ansible.builtin.set_fact:
kubeinit_network_map: "{{ kubeinit_network_spec | from_yaml }}"
when: kubeinit_network_spec is defined
- name: Define kubeinit_network_name
ansible.builtin.set_fact:
kubeinit_network_name: "{{ kubeinit_network_map['network_name'] | default(hostvars['kubeinit-defaults'].default_network_name) }}"
- name: Use kubeinit_network_name as host for kubeinit_network group and load default properties
ansible.builtin.add_host:
name: "{{ kubeinit_network_name }}"
groups: 'kubeinit_network'
- name: Add kubeinit_network_map entries to kubeint_network group
ansible.builtin.add_host:
name: "{{ kubeinit_network_name }}"
groups: 'kubeinit_network'
network: "{{ kubeinit_network_map['network'] | default(omit) }}"
gateway_offset: "{{ kubeinit_network_map['gateway_offset'] | default(omit) }}"
dhcp_start_offset: "{{ kubeinit_network_map['dhcp_start_offset'] | default(omit) }}"
dhcp_end_offset: "{{ kubeinit_network_map['dhcp_end_offset'] | default(omit) }}"
when: kubeinit_network_spec is defined
- name: Define kubeinit_network_hostvars and kubeinit_fact_natwork
ansible.builtin.set_fact:
kubeinit_network_hostvars: "{{ hostvars[kubeinit_network_name] }}"
kubeinit_fact_network: "{{ hostvars[kubeinit_network_name].network }}"
- name: Set facts from kubeinit_network group
ansible.builtin.set_fact:
kubeinit_cluster_network_name: "{{ kubeinit_network_name }}"
kubeinit_cluster_network: "{{ kubeinit_fact_network | ansible.utils.ipv4('net') }}"
kubeinit_cluster_prefix: "{{ kubeinit_fact_network | ansible.utils.ipv4('prefix') }}"
kubeinit_cluster_netmask: "{{ kubeinit_fact_network | ansible.utils.ipv4('netmask') | ansible.utils.ipv4('address') }}"
kubeinit_cluster_gateway: "{{ kubeinit_fact_network | ansible.utils.ipv4(kubeinit_network_hostvars.gateway_offset|int) | ansible.utils.ipv4('address') }}"
kubeinit_cluster_next_available: "{{ kubeinit_fact_network | ansible.utils.ipv4(kubeinit_network_hostvars.dhcp_start_offset|int) | ansible.utils.ipv4('address') }}"
kubeinit_cluster_last_available: "{{ kubeinit_fact_network | ansible.utils.ipv4(kubeinit_network_hostvars.dhcp_end_offset|int) | ansible.utils.ipv4('address') }}"
- name: Assert additional constraints
ansible.builtin.assert:
that:
- kubeinit_cluster_prefix|int < 30
- name: Add cluster network facts to cluster facts
ansible.builtin.add_host:
name: "{{ kubeinit_cluster_name }}"
network_name: "{{ kubeinit_cluster_network_name }}"
network: "{{ kubeinit_cluster_network }}"
prefix: "{{ kubeinit_cluster_prefix }}"
netmask: "{{ kubeinit_cluster_netmask }}"
gateway: "{{ kubeinit_cluster_gateway }}"
- name: Assign controller_nodes addresses
ansible.builtin.add_host:
name: "{{ controller_node }}"
group: ['controller_nodes', 'cluster_nodes']
ansible_connection: 'ssh'
ansible_host: "{{ kubeinit_cluster_next_available | ansible.utils.ipmath(ansible_loop.index0) | ansible.utils.ipv4('address') }}"
type: 'virtual'
loop: "{{ range(kubeinit_spec_controller_count|int) | list }}"
loop_control:
extended: true
vars:
controller_node: "{{ hostvars[kubeinit_cluster_name].controller_name_pattern | format(ansible_loop.index) }}"
- name: Update next available address for allocation
ansible.builtin.set_fact:
kubeinit_cluster_next_available: "{{ kubeinit_cluster_next_available | ansible.utils.ipmath(kubeinit_spec_controller_count|int) }}"
- name: Assign compute_nodes addresses
ansible.builtin.add_host:
name: "{{ compute_node }}"
group: ['compute_nodes', 'cluster_nodes']
ansible_connection: 'ssh'
ansible_host: "{{ kubeinit_cluster_next_available | ansible.utils.ipmath(ansible_loop.index0) | ansible.utils.ipv4('address') }}"
type: 'virtual'
loop: "{{ range(kubeinit_spec_compute_count|int) | list }}"
loop_control:
extended: true
vars:
compute_node: "{{ hostvars[kubeinit_cluster_name].compute_name_pattern | format(ansible_loop.index) }}"
- name: Update next available address for allocation
ansible.builtin.set_fact:
kubeinit_cluster_next_available: "{{ kubeinit_cluster_next_available | ansible.utils.ipmath(kubeinit_spec_compute_count|int) }}"
- name: Set extra_nodes_map_list fact to defaults
ansible.builtin.set_fact:
extra_nodes_map_list: "{{ hostvars['kubeinit-defaults'].default_extra_nodes_map_list }}"
- name: Add entries from extra_nodes_spec to extra_nodes_map_list when defined
ansible.builtin.set_fact:
extra_nodes_map_list: "{{ extra_nodes_map_list + (extra_nodes_spec | from_yaml) }}"
when: extra_nodes_spec is defined
- name: Add extra_nodes entries for the current distro only
ansible.builtin.add_host:
name: "{{ item['name'] }}"
group: 'extra_nodes'
when_distro: "{{ item['when_distro'] | default(omit) }}"
loop: "{{ extra_nodes_map_list }}"
when: (item['when_distro'] is not defined or kubeinit_cluster_distro in item['when_distro']) and item['name'] is defined
- name: Set fact for the number of extra nodes in this cluster
ansible.builtin.set_fact:
kubeinit_spec_extra_count: "{{ groups['extra_nodes'] | default([]) | length | int }}"
- name: Assign extra_nodes addresses
ansible.builtin.add_host:
name: "{{ extra_node }}"
group: 'extra_nodes'
ansible_connection: 'ssh'
ansible_host: "{{ kubeinit_cluster_next_available | ansible.utils.ipmath(ansible_loop.index0) | ansible.utils.ipv4('address') }}"
type: 'virtual'
loop: "{{ groups['extra_nodes'] | default([]) }}"
loop_control:
loop_var: extra_node
extended: true
- name: Update next available address for allocation
ansible.builtin.set_fact:
kubeinit_cluster_next_available: "{{ kubeinit_cluster_next_available | ansible.utils.ipmath(kubeinit_spec_extra_count|int) }}"
when: kubeinit_spec_extra_count|int > 0
- name: Set service_nodes_map_list fact to defaults
ansible.builtin.set_fact:
service_nodes_map_list: "{{ hostvars['kubeinit-defaults'].default_service_nodes_map_list }}"
- name: Add entries from service_nodes_spec to service_nodes_map_list when defined
ansible.builtin.set_fact:
service_nodes_map_list: "{{ service_nodes_map_list + (service_nodes_spec | from_yaml) }}"
when: service_nodes_spec is defined
- name: Add service_nodes entries for the current distro only
ansible.builtin.add_host:
name: "{{ item['name'] }}"
group: 'service_nodes'
when_distro: "{{ item['when_distro'] | default(omit) }}"
loop: "{{ service_nodes_map_list }}"
when: (item['when_distro'] is not defined or kubeinit_cluster_distro in item['when_distro']) and item['name'] is defined
- name: Set fact for the number of service nodes in this cluster
ansible.builtin.set_fact:
kubeinit_spec_service_count: "{{ groups['service_nodes'] | length | int }}"
- name: Assign service_nodes addresses
ansible.builtin.add_host:
name: "{{ service_node }}"
group: 'service_nodes'
ansible_connection: 'ssh'
ansible_host: "{{ kubeinit_cluster_last_available | ansible.utils.ipmath(0 - ansible_loop.index0) | ansible.utils.ipv4('address') }}"
type: 'container'
loop: "{{ groups['service_nodes'] }}"
loop_control:
loop_var: service_node
extended: true
- name: Update last available address for allocation
ansible.builtin.set_fact:
kubeinit_cluster_last_available: "{{ kubeinit_cluster_last_available | ansible.utils.ipmath(0 - (kubeinit_spec_service_count|int)) }}"
- name: Set cluster dhcp facts
ansible.builtin.set_fact:
kubeinit_cluster_dhcp_start: "{{ kubeinit_cluster_next_available | ansible.utils.ipv4('address') }}"
kubeinit_cluster_dhcp_end: "{{ kubeinit_cluster_last_available | ansible.utils.ipv4('address') }}"
- name: Set cluster dhcp range size
ansible.builtin.set_fact:
kubeinit_cluster_dhcp_count: "{{ (kubeinit_cluster_dhcp_end | ansible.utils.ipv4('int') | int) - (kubeinit_cluster_dhcp_start | ansible.utils.ipv4('int') | int) + 1 }}"
- name: Assert sufficient dhcp range
ansible.builtin.assert:
that:
- kubeinit_cluster_dhcp_count|int > 0
- name: Assign dhcp facts
ansible.builtin.add_host:
name: "{{ kubeinit_cluster_name }}"
dhcp_start: "{{ kubeinit_cluster_dhcp_start }}"
dhcp_end: "{{ kubeinit_cluster_dhcp_end }}"
- name: Clear kubeinit_network_hostvars
ansible.builtin.set_fact:
kubeinit_network_hostvars: null
- name: Set cluster_nodes_map_list fact to defaults
ansible.builtin.set_fact:
cluster_nodes_map_list: "{{ hostvars['kubeinit-defaults'].default_cluster_nodes_map_list }}"
- name: Add entries from cluster_nodes_spec to cluster_nodes_map_list when defined
ansible.builtin.set_fact:
cluster_nodes_map_list: "{{ cluster_nodes_map_list + (cluster_nodes_spec | from_yaml) }}"
when: cluster_nodes_spec is defined
- name: Add all items in cluster_nodes_map_list to cluster_nodes_defaults for this distro
ansible.builtin.set_fact:
cluster_nodes_defaults: "{{ cluster_nodes_defaults | default([]) + (groups['cluster_nodes'] | product([item])) }}"
loop: "{{ cluster_nodes_map_list }}"
when: item['when_distro'] is not defined or kubeinit_cluster_distro in item['when_distro']
- name: Apply defaults to all cluster nodes
ansible.builtin.add_host:
name: "{{ _host }}"
os: "{{ _defaults['os'] if (_defaults['os'] is defined and _group_matches|bool and _type_matches|bool) else omit }}"
vcpus: "{{ _defaults['vcpus'] if (_defaults['vcpus'] is defined and _group_matches|bool and _type_matches|bool) else omit }}"
maxvcpus: "{{ _defaults['maxvcpus'] if (_defaults['maxvcpus'] is defined and _group_matches|bool and _type_matches|bool) else omit }}"
disk: "{{ _defaults['disk'] if (_defaults['disk'] is defined and _group_matches|bool and _type_matches|bool) else omit }}"
ram: "{{ _defaults['ram'] if (_defaults['ram'] is defined and _group_matches|bool and _type_matches|bool) else omit }}"
target_order: "{{ _defaults['target_order'] if (_defaults['target_order'] is defined and _group_matches|bool and _type_matches|bool) else omit }}"
loop: "{{ cluster_nodes_defaults }}"
vars:
_host: "{{ item[0] }}"
_defaults: "{{ item[1] }}"
_group_matches: "{{ item[1]['when_group'] | default('cluster_nodes') in hostvars[item[0]].group_names }}"
_type_matches: "{{ item[1]['when_type'] | default(hostvars[item[0]].type) | default('') == hostvars[item[0]].type | default('') }}"
- name: Clear cluster_nodes_defaults
ansible.builtin.set_fact:
cluster_nodes_defaults: null
- name: Add extra_nodes_map_list to config for current distro only
ansible.builtin.set_fact:
extra_nodes_config: "{{ extra_nodes_config | default([]) + (groups['extra_nodes'] | default([]) | product([item])) }}"
loop: "{{ extra_nodes_map_list }}"
when: item['when_distro'] is not defined or kubeinit_cluster_distro in item['when_distro']
- name: Apply config to all extra nodes
ansible.builtin.add_host:
name: "{{ _host }}"
os: "{{ _config['os'] if (_config['os'] is defined and _type_matches|bool) else omit }}"
vcpus: "{{ _config['vcpus'] if (_config['vcpus'] is defined and _type_matches|bool) else omit }}"
maxvcpus: "{{ _config['maxvcpus'] if (_config['maxvcpus'] is defined and _type_matches|bool) else omit }}"
disk: "{{ _config['disk'] if (_config['disk'] is defined and _type_matches|bool) else omit }}"
ram: "{{ _config['ram'] if (_config['ram'] is defined and _type_matches|bool) else omit }}"
target_order: "{{ _config['target_order'] if (_config['target_order'] is defined and _type_matches|bool) else omit }}"
loop: "{{ extra_nodes_config }}"
vars:
_host: "{{ item[0] }}"
_config: "{{ item[1] }}"
_type_matches: "{{ item[1]['when_type'] | default(hostvars[item[0]].type) | default('') == hostvars[item[0]].type | default('') }}"
when: _config['name'] | default(_host) == _host
- name: Add service_nodes_map_list to config for current distro only
ansible.builtin.set_fact:
service_nodes_config: "{{ service_nodes_config | default([]) + (groups['service_nodes'] | product([item])) }}"
loop: "{{ service_nodes_map_list }}"
when: item['when_distro'] is not defined or kubeinit_cluster_distro in item['when_distro']
- name: Apply config to all service nodes
ansible.builtin.add_host:
name: "{{ _host }}"
os: "{{ _config['os'] if (_config['os'] is defined) else omit }}"
services: "{{ _config['services'] if (_config['services'] is defined) else omit }}"
target_order: "{{ _config['target_order'] if (_config['target_order'] is defined) else omit }}"
loop: "{{ service_nodes_config }}"
vars:
_host: "{{ item[0] }}"
_config: "{{ item[1] }}"
when: _config['name'] | default(_host) == _host
- name: Define kubeinit_facts_hostvars
ansible.builtin.set_fact:
kubeinit_facts_hostvars: "{{ hostvars['kubeinit-facts'] }}"
- name: Define kubeinit_ssh_keytype
ansible.builtin.set_fact:
kubeinit_ssh_keytype: "{{ hostvars['kubeinit-env'].ssh_keytype }}"
- name: Use cluster name as random number seed
ansible.builtin.set_fact:
_random_mac: "{{ '52:54:00' | community.general.random_mac(seed=kubeinit_cluster_name) }}"
_random_uuid: "{{ 999999 | random(seed=kubeinit_cluster_name) | to_uuid }}"
- name: Assign remaining controller_nodes facts
ansible.builtin.add_host:
name: "{{ controller_node }}"
group: ['controller_nodes', 'cluster_nodes']
mac: "{{ hostvars[controller_node].mac | default('52:54:00' | community.general.random_mac) }}"
interfaceid: "{{ hostvars[controller_node].interfaceid | default(999999 | random | to_uuid) }}"
loop: "{{ range(kubeinit_spec_controller_count|int) | list }}"
loop_control:
extended: true
vars:
controller_node: "{{ hostvars[kubeinit_cluster_name].controller_name_pattern | format(ansible_loop.index) }}"
- name: Assign remaining compute_nodes facts
ansible.builtin.add_host:
name: "{{ compute_node }}"
group: ['compute_nodes', 'cluster_nodes']
mac: "{{ hostvars[compute_node].mac | default('52:54:00' | community.general.random_mac) }}"
interfaceid: "{{ hostvars[compute_node].interfaceid | default(999999 | random | to_uuid) }}"
loop: "{{ range(kubeinit_spec_compute_count|int) | list }}"
loop_control:
extended: true
vars:
compute_node: "{{ hostvars[kubeinit_cluster_name].compute_name_pattern | format(ansible_loop.index) }}"
- name: Assign remaining extra_nodes facts
ansible.builtin.add_host:
name: "{{ hostvars[extra_node].inventory_hostname }}"
group: 'extra_nodes'
mac: "{{ hostvars[extra_node].mac | default('52:54:00' | community.general.random_mac) }}"
interfaceid: "{{ hostvars[extra_node].interfaceid | default(999999 | random | to_uuid) }}"
loop: "{{ groups['extra_nodes'] | default([]) }}"
loop_control:
loop_var: extra_node
extended: true
- name: Assign remaining service_nodes facts
ansible.builtin.add_host:
name: "{{ hostvars[service_node].inventory_hostname }}"
group: 'service_nodes'
mac: "{{ hostvars[service_node].mac | default('52:54:00' | community.general.random_mac) }}"
interfaceid: "{{ hostvars[service_node].interfaceid | default(999999 | random | to_uuid) }}"
loop: "{{ groups['service_nodes'] | default([]) }}"
loop_control:
loop_var: service_node
extended: true
- name: Set additional kubeinit-env facts
ansible.builtin.set_fact:
kubeinit_dns_public: "{{ hostvars['kubeinit-env'].dns_public }}"
- name: Set libvirt release facts
ansible.builtin.set_fact:
kubeinit_libvirt_debian_release: "11"
# It is possible to configure different versions of the OS depending on the distribution
# kubeinit_libvirt_ubuntu_release: "{{ 'focal' if (kubeinit_cluster_distro == 'cdk') else 'jammy' }}"
kubeinit_libvirt_ubuntu_release: "jammy"
- name: Set registry authentication facts
ansible.builtin.set_fact:
kubeinit_registry_user: registryusername
kubeinit_registry_password: registrypassword
- name: Determine the hypervisor target for all inventory nodes
ansible.builtin.set_fact:
node_targets: "{{ (node_targets | default([])) | union([inventory_node] | product(hostvars[inventory_node].target_order | reverse)) }}"
loop: "{{ groups['cluster_nodes'] + (groups['extra_nodes'] | default([])) + groups['service_nodes'] }}"
loop_control:
loop_var: inventory_node
- name: Assign the first requested hypervisor available for each node
ansible.builtin.add_host:
name: "{{ inventory_node }}"
target: "{{ hostvars[inventory_node].target | default(candidate) }}"
loop: "{{ node_targets }}"
vars:
inventory_node: "{{ item[0] }}"
candidate: "{{ item[1] }}"
when: candidate in groups['kubeinit_hypervisors']
- name: Update hosts and stop the deployment if required
block:
- name: Add tasks-gather-facts to tasks completed
ansible.builtin.add_host:
name: "kubeinit-facts"
tasks_completed: "{{ ['task-gather-facts'] }}"
- name: Update kubeinit_facts_hostvars
ansible.builtin.set_fact:
kubeinit_facts_hostvars: "{{ hostvars['kubeinit-facts'] }}"
- name: Stop the deployment if required
block:
- name: Stop after 'task-gather-facts' when requested
ansible.builtin.add_host:
name: "kubeinit-facts"
playbook_terminated: true
- name: End play
ansible.builtin.meta: end_play
when: kubeinit_stop_after_task is defined and kubeinit_stop_after_task in hostvars['kubeinit-facts'].tasks_completed
tags: omit_from_grapher