From 83507bf0274779e7370d7c660331afb8b791dc3b Mon Sep 17 00:00:00 2001 From: Ruakij Date: Wed, 2 Nov 2022 08:54:14 +0100 Subject: [PATCH] Grab local-IP from specified interface instead --- kubernetes/defaults/main.yml | 3 +- kubernetes/tasks/prerequisites.yml | 44 +++++-------------- .../templates/rke2/config.yaml.template | 2 +- 3 files changed, 15 insertions(+), 34 deletions(-) diff --git a/kubernetes/defaults/main.yml b/kubernetes/defaults/main.yml index b5c01e6..cf088ae 100644 --- a/kubernetes/defaults/main.yml +++ b/kubernetes/defaults/main.yml @@ -8,7 +8,8 @@ kubernetes: ipv6: cluster_cidr: fd42::/56 service_cidr: fd43::/112 - nodeip_cidr: fd41::/64 + + nodeIp_interface: control_plane: dns_name: diff --git a/kubernetes/tasks/prerequisites.yml b/kubernetes/tasks/prerequisites.yml index dcb4f1a..9d74829 100644 --- a/kubernetes/tasks/prerequisites.yml +++ b/kubernetes/tasks/prerequisites.yml @@ -49,41 +49,21 @@ mode: u=rw,g=r,o=r notify: restart_containerd -- name: Set control-plane-dns-endpoint towards local-ip - lineinfile: - dest: /etc/hosts - line: "{{ ansible_facts.default_ipv6.address }} k8s-control-plane.system.ruekov.eu" +# todo: Move to netmaker-role as handler? +- name: Gather facts to get changes + ansible.builtin.gather_facts: -- name: Setting network facts.. - set_fact: - inventory_group_index: "{{ groups['kubernetes'].index(inventory_hostname) }}" - ipPool_ipv6_nodeip_cidr_only_prefix: '{{ kubernetes.ipPool.ipv6.nodeip_cidr | regex_replace ("(?<=:)[0-9a-f]{0,4}(\/[0-9]+)?$","") }}' - ipPool_ipv6_nodeip_cidr_cidr: '{{ kubernetes.ipPool.ipv6.nodeip_cidr | regex_replace ("^.*?\/","") }}' - ipPool_ipv4_nodeip_cidr_only_prefix: '{{ kubernetes.ipPool.ipv4.nodeip_cidr | regex_replace ("(?<=.)[0-9]{0,3}(\/[0-9]+)?$","") }}' - ipPool_ipv4_nodeip_cidr_cidr: '{{ kubernetes.ipPool.ipv4.nodeip_cidr | regex_replace ("^.*?\/","") }}' - -- name: Setting more network_facts.. +- name: Getting nodeIp-data from interface set_fact: - ipPool_ipv6_nodeip: "{{ ipPool_ipv6_nodeip_cidr_only_prefix }}{{ inventory_group_index|int +1 }}" - ipPool_ipv4_nodeip: "{{ ipPool_ipv4_nodeip_cidr_only_prefix }}{{ inventory_group_index|int +1 }}" + nodeip_ipv4: "{{ ansible_facts[ kubernetes.ipPool.nodeIp_interface ].ipv4.address }}" + nodeip_ipv6: "{{ ansible_facts[ kubernetes.ipPool.nodeIp_interface ].ipv6[0].address }}" -- name: Setup IPv4-Network - lineinfile: - insertafter: "iface {{ ansible_facts.default_ipv4.interface }} inet .+" - dest: "/etc/network/interfaces" - line: " up /usr/sbin/ip addr add {{ ipPool_ipv4_nodeip }} dev {{ ansible_facts.default_ipv4.interface }}" - register: setup_network_ipv4 - -- name: Setup IPv6-Network - lineinfile: - insertafter: "iface {{ ansible_facts.default_ipv4.interface }} inet6 .+" - dest: "/etc/network/interfaces" - line: " up /usr/sbin/ip -6 addr add {{ ipPool_ipv6_nodeip }} dev {{ ansible_facts.default_ipv4.interface }}" - register: setup_network_ipv6 - -- name: Force-Reload network-interface - command: "ifup --force {{ ansible_facts.default_ipv4.interface }}" - when: setup_network_ipv4.changed or setup_network_ipv6.changed +- name: Set control-plane-dns-endpoint towards local-ip + blockinfile: + path: /etc/hosts + marker: "# {mark} ANSIBLE MANAGED BLOCK | k8s" + block: | + {{ nodeip_ipv4 }} {{ kubernetes.control_plane.dns_name }} - name: Run handlers to reload configurations meta: flush_handlers diff --git a/kubernetes/templates/rke2/config.yaml.template b/kubernetes/templates/rke2/config.yaml.template index 75c2f14..cb2a4f7 100644 --- a/kubernetes/templates/rke2/config.yaml.template +++ b/kubernetes/templates/rke2/config.yaml.template @@ -13,4 +13,4 @@ tls-san: cluster-cidr: {{ kubernetes.ipPool.ipv4.cluster_cidr }},{{ kubernetes.ipPool.ipv6.cluster_cidr }} service-cidr: {{ kubernetes.ipPool.ipv4.service_cidr }},{{ kubernetes.ipPool.ipv6.service_cidr }} -node-ip: {{ ipPool_ipv4_nodeip }},{{ ipPool_ipv6_nodeip }} +node-ip: {{ nodeip_ipv4 }},{{ nodeip_ipv6 }}