TP-IaC-Kubernetes/initialize-kubernetes.yaml

110 lines
3.3 KiB
YAML

---
- hosts: control-plane
name: "Initialize Kubernetes"
tasks:
- name: kubeadm config
become: yes
command: kubeadm config images pull
register: kubeadmconfig
- debug: var=kubeadmconfig.stdout_lines
- name: crictl image
become: yes
command: crictl image
register: crictl
- debug: var=crictl.stdout_lines
- name: kubadm init
become: yes
command: kubeadm init --pod-network-cidr={{ pods_subnet }} --service-cidr={{ services_subnet }} --apiserver-advertise-address={{ ansible_default_ipv4.address }} --cri-socket=unix:///var/run/crio/crio.sock
register: kubeadminit
- debug: var=kubeadminit.stdout_lines
- name: Set Up Kubernetes credential
block:
- name: create ~/.kube folder
file:
path: $HOME/.kube
state: directory
mode: '0755'
- name: get current user
command: whoami
register: c_user
- name: get current group
command: id -g
register: c_group
- name: Copy admin.conf to .kube
become: yes
copy:
src: /etc/kubernetes/admin.conf
dest: /home/{{ c_user.stdout }}/.kube/config
remote_src: yes
owner: "{{ c_user.stdout }}"
group: "{{ c_group.stdout }}"
mode: '0600'
- name: kubectl cluster-info
command: kubectl cluster-info
register: kubectl
- debug: var=kubectl.stdout_lines
- name: get calico conf
template:
src: calico.yaml.j2
dest: $HOME/calico.yaml
- name: apply calico conf1
shell: kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.1/manifests/tigera-operator.yaml
register: apply1
- debug: var=apply1.stdout_lines
- name: apply calico conf2
shell: kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.1/manifests/custom-resources.yaml
register: apply2
- debug: var=apply2.stdout_lines
- name: apply calico conf3
shell: kubectl apply -f calico.yaml
register: apply3
- debug: var=apply3.stdout_lines
- name: show pods
command: kubectl get pods --all-namespaces
register: pods
- debug: var=pods.stdout_lines
- name: "Cluster token"
shell: kubeadm token list | cut -d ' ' -f1 | sed -n '2p'
register: K8S_TOKEN
- name: "CA Hash"
shell: openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
register: K8S_MASTER_CA_HASH
- name: "IP and port"
shell: kubectl cluster-info | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | cut -d ' ' -f 7 | sed -n '1p' | cut -c 9-
register: K8S_IP_PORT
- name: "Add K8S Token and Hash to dummy host"
add_host:
name: "K8S_TOKEN_HOLDER"
token: "{{ K8S_TOKEN.stdout }}"
hash: "{{ K8S_MASTER_CA_HASH.stdout }}"
ipport: "{{ K8S_IP_PORT.stdout }}"
- name:
debug:
msg: "[Master] K8S_TOKEN_HOLDER K8S token is {{ hostvars['K8S_TOKEN_HOLDER']['token'] }}"
- name:
debug:
msg: "[Master] K8S_TOKEN_HOLDER K8S Hash is {{ hostvars['K8S_TOKEN_HOLDER']['hash'] }}"
- name:
debug:
msg: "[Master] K8S_TOKEN_HOLDER K8S IP and port is {{ hostvars['K8S_TOKEN_HOLDER']['ipport'] }}"