2023-01-02 00:41:41 +01:00
|
|
|
---
|
|
|
|
- name: Setup Kubernetes control plane
|
|
|
|
block:
|
|
|
|
- name: Set cluster endpoint record
|
|
|
|
become: true
|
|
|
|
ansible.builtin.lineinfile:
|
|
|
|
path: /etc/hosts
|
|
|
|
state: present
|
|
|
|
regexp: '^\d+\.\d+\.\d+\.\d+\ .*-cluster-endpoint$'
|
|
|
|
line: "127.0.0.1 {{ cluster_name }}-cluster-endpoint"
|
|
|
|
|
|
|
|
- name: Check if kubeadm has already run
|
|
|
|
become: true
|
|
|
|
ansible.builtin.stat:
|
|
|
|
path: "/etc/kubernetes/pki/ca.key"
|
|
|
|
register: kubeadm_ca
|
|
|
|
|
|
|
|
- name: Set up control plane
|
|
|
|
when: not kubeadm_ca.stat.exists
|
|
|
|
become: true
|
|
|
|
ansible.builtin.command: |
|
|
|
|
kubeadm init \
|
|
|
|
--service-cidr {{ service_cidr }} \
|
|
|
|
--pod-network-cidr {{ pod_network_cidr }} \
|
|
|
|
--control-plane-endpoint {{ cluster_name }}-cluster-endpoint \
|
|
|
|
|
|
|
|
- name: Create Kubernetes config directory
|
|
|
|
ansible.builtin.file:
|
|
|
|
path: ".kube/"
|
|
|
|
state: directory
|
|
|
|
owner: "{{ ansible_user | default(ansible_user_id) }}"
|
|
|
|
group: "{{ ansible_user | default(ansible_user_id) }}"
|
|
|
|
|
|
|
|
- name: Copy admin.conf to home
|
|
|
|
become: true
|
|
|
|
ansible.builtin.copy:
|
|
|
|
src: "{{ kubeadmin_config }}"
|
|
|
|
dest: "{{ ansible_env.HOME }}/.kube/config"
|
|
|
|
owner: "{{ ansible_user | default(ansible_user_id) }}"
|
|
|
|
group: "{{ ansible_user | default(ansible_user_id) }}"
|
|
|
|
mode: 0600
|
|
|
|
remote_src: true
|
|
|
|
|
|
|
|
# Kubernetes module dependency installation
|
|
|
|
- name: Install Kubernetes module dependencies
|
|
|
|
block:
|
|
|
|
- name: Install Pip3
|
|
|
|
become: true
|
|
|
|
vars:
|
|
|
|
packages:
|
|
|
|
- python3-pip
|
|
|
|
ansible.builtin.apt:
|
|
|
|
name: "{{ packages }}"
|
|
|
|
state: present
|
|
|
|
update_cache: yes
|
2023-01-11 21:04:00 +01:00
|
|
|
cache_valid_time: 3600
|
2023-01-02 00:41:41 +01:00
|
|
|
|
|
|
|
- name: Install Kubernetes module dependencies
|
|
|
|
ansible.builtin.pip:
|
|
|
|
name:
|
|
|
|
- kubernetes>12.0.0
|
|
|
|
- pyyaml>3.11
|
|
|
|
- jsonpatch
|
|
|
|
|
|
|
|
# CNI plugin installation
|
|
|
|
- name: Install CNI plugin
|
|
|
|
vars:
|
|
|
|
manifests:
|
|
|
|
- tigera-operator.yaml
|
|
|
|
- custom-resources.yaml
|
|
|
|
block:
|
|
|
|
- name: Copy Calico manifests
|
|
|
|
loop: "{{ manifests }}"
|
|
|
|
ansible.builtin.copy:
|
|
|
|
src: "files/calico/v3.24.5/{{ item }}"
|
|
|
|
dest: "/tmp/calico-{{ item }}"
|
|
|
|
|
|
|
|
- name: Update spec.calicoNetwork.ipPools.cidr
|
|
|
|
ansible.builtin.replace:
|
|
|
|
path: /tmp/calico-custom-resources.yaml
|
|
|
|
regexp: '192\.168\.0\.0\/16'
|
|
|
|
replace: "{{ pod_network_cidr }}"
|
|
|
|
|
|
|
|
- name: Apply Calico manifests
|
|
|
|
loop: "{{ manifests }}"
|
|
|
|
kubernetes.core.k8s:
|
|
|
|
src: /tmp/calico-{{ item }}
|
|
|
|
state: present
|