main.yml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # Copyright 2021 Dell Inc. or its subsidiaries. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. ---
  15. - name: Disable SWAP (1/2)
  16. command: /usr/sbin/swapoff -a
  17. changed_when: true
  18. tags: init
  19. - name: Disable SWAP in fstab (2/2)
  20. replace:
  21. path: /etc/fstab
  22. regexp: '^([^#].*?\sswap\s+.*)$'
  23. replace: '# \1'
  24. - name: Get netaddr
  25. setup:
  26. filter: ansible_default_ipv4.address
  27. - name: Get K8s nodes status
  28. command: kubectl get nodes
  29. changed_when: false
  30. failed_when: false
  31. register: k8s_nodes
  32. tags: init
  33. - name: Get K8s ready compute nodes
  34. shell: >
  35. set -o pipefail && \
  36. kubectl get node --selector='!node-role.kubernetes.io/master' | grep -v 'NotReady'
  37. changed_when: false
  38. failed_when: false
  39. register: k8s_nodes_ready
  40. tags: init
  41. - name: Get K8s not ready compute nodes
  42. shell: >
  43. set -o pipefail && \
  44. kubectl get node --selector='!node-role.kubernetes.io/master' | grep 'NotReady'
  45. changed_when: false
  46. failed_when: false
  47. register: k8s_nodes_not_ready
  48. tags: init
  49. - name: Docker login
  50. command: docker login -u {{ hostvars['127.0.0.1']['docker_username'] }} -p {{ hostvars['127.0.0.1']['docker_password'] }}
  51. changed_when: true
  52. register: docker_login_output
  53. failed_when: false
  54. when: hostvars['127.0.0.1']['docker_username'] or hostvars['127.0.0.1']['docker_password']
  55. no_log: true
  56. - name: Docker login check
  57. fail:
  58. msg: "{{ docker_login_fail_msg }}"
  59. when: docker_login_output is failed
  60. - name: Initialize kubeadm (This process may take 5-10min)
  61. block:
  62. - name: Initialize kubeadm (This process may take 5-10min)
  63. command: "/bin/kubeadm init --pod-network-cidr='{{ hostvars['127.0.0.1']['k8s_pod_network_cidr'] }}' \
  64. --apiserver-advertise-address='{{ ansible_default_ipv4.address }}'"
  65. changed_when: true
  66. register: init_output
  67. tags: init
  68. rescue:
  69. - name: Reset kubeadm
  70. command: "kubeadm reset -f"
  71. changed_when: true
  72. - name: Initialize kubeadm (This process may take 5-10min)
  73. command: "/bin/kubeadm init --pod-network-cidr='{{ hostvars['127.0.0.1']['k8s_pod_network_cidr'] }}' \
  74. --apiserver-advertise-address='{{ ansible_default_ipv4.address }}'"
  75. changed_when: true
  76. register: init_output
  77. tags: init
  78. when: "'master' not in k8s_nodes.stdout"
  79. - name: Setup directory for Kubernetes environment for root
  80. file:
  81. path: "{{ k8s_root_directory }}"
  82. state: directory
  83. mode: "{{ k8s_root_directory_mode }}"
  84. tags: init
  85. - name: Copy Kubernetes config for root
  86. copy:
  87. src: "{{ k8s_config_src }}"
  88. dest: "{{ k8s_config_dest }}"
  89. owner: root
  90. group: root
  91. mode: "{{ k8s_config_file_mode }}"
  92. remote_src: yes
  93. tags: init
  94. - name: Update the kubernetes config file permissions
  95. shell: "chown $(id -u):$(id -g) '{{ k8s_config_dest }}'"
  96. args:
  97. warn: false
  98. changed_when: true
  99. tags: init
  100. - name: Cluster token
  101. shell: >
  102. set -o pipefail && \
  103. kubeadm token list | cut -d ' ' -f1 | sed -n '2p'
  104. changed_when: false
  105. register: k8s_token
  106. tags: init
  107. - name: CA Hash
  108. shell: >
  109. set -o pipefail && \
  110. openssl x509 -pubkey -in {{ k8s_cert_path }} | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
  111. changed_when: false
  112. register: k8s_manager_ca_hash
  113. tags: init
  114. - name: Add K8S Manager IP, Token, and Hash to dummy host
  115. add_host:
  116. name: "K8S_TOKEN_HOLDER"
  117. token: "{{ k8s_token.stdout }}"
  118. hash: "{{ k8s_manager_ca_hash.stdout }}"
  119. ip: "{{ ansible_default_ipv4.address }}"
  120. k8s_nodes: "{{ k8s_nodes.stdout }}"
  121. k8s_nodes_ready: "{{ k8s_nodes_ready.stdout }}"
  122. k8s_nodes_not_ready: "{{ k8s_nodes_not_ready.stdout }}"
  123. tags: init
  124. - name: Print k8s token
  125. debug:
  126. msg: "[Manager] K8S_TOKEN_HOLDER K8S token is {{ hostvars['K8S_TOKEN_HOLDER']['token'] }}"
  127. verbosity: 2
  128. tags: init
  129. - name: Print k8s hash
  130. debug:
  131. msg: "[Manager] K8S_TOKEN_HOLDER K8S Hash is {{ hostvars['K8S_TOKEN_HOLDER']['hash'] }}"
  132. verbosity: 2
  133. tags: init
  134. - name: Print k8s ansible_default_ipv4.address
  135. debug:
  136. msg: "[Manager] K8S_MANAGER_IP is {{ ansible_default_ipv4.address }}"
  137. verbosity: 2
  138. tags: init
  139. - name: Get K8s secrets
  140. command: kubectl get secrets
  141. changed_when: false
  142. register: k8s_secret
  143. when: hostvars['127.0.0.1']['docker_username'] and hostvars['127.0.0.1']['docker_password']
  144. - name: Create docker secret
  145. command: kubectl create secret docker-registry dockerregcred --docker-username={{ hostvars['127.0.0.1']['docker_username'] }} \
  146. --docker-password={{ hostvars['127.0.0.1']['docker_password'] }}
  147. when:
  148. - hostvars['127.0.0.1']['docker_username'] and hostvars['127.0.0.1']['docker_password']
  149. - "'dockerregcred' not in k8s_secret.stdout"
  150. - name: Add docker secret to default service account
  151. shell: >
  152. kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "dockerregcred"}]}'
  153. register: patch_service_account
  154. when: hostvars['127.0.0.1']['docker_username'] and hostvars['127.0.0.1']['docker_password']
  155. until: patch_service_account is not failed
  156. retries: 10
  157. tags: install
  158. - name: Create yaml repo for setup
  159. file:
  160. path: "{{ yaml_repo_dir_path }}"
  161. state: directory
  162. mode: "{{ yaml_repo_dir_mode }}"
  163. tags: init
  164. - name: Delete Calico yaml file if exists
  165. file:
  166. path: "{{ calico_yml_file_path }}"
  167. state: absent
  168. when: hostvars['127.0.0.1']['k8s_cni'] == "calico"
  169. tags: init
  170. - name: Copy Calico yaml file
  171. copy:
  172. src: kube-calico.yaml
  173. dest: "{{ calico_yml_file_path }}"
  174. owner: root
  175. group: root
  176. mode: "{{ calico_yml_file_mode }}"
  177. when: hostvars['127.0.0.1']['k8s_cni'] == "calico"
  178. tags: init
  179. - name: Setup Calico SDN network
  180. command: "kubectl apply -f '{{ calico_yml_file_path }}'"
  181. when: hostvars['127.0.0.1']['k8s_cni'] == "calico"
  182. tags: init
  183. - name: Delete Flannel yaml file if exists
  184. file:
  185. path: "{{ flannel_yml_file_path }}"
  186. state: absent
  187. when: hostvars['127.0.0.1']['k8s_cni'] == "flannel"
  188. tags: init
  189. - name: Copy Flannel yaml file
  190. copy:
  191. src: kube-flannel.yaml
  192. dest: "{{ flannel_yml_file_path }}"
  193. owner: root
  194. group: root
  195. mode: "{{ flannel_yml_file_mode }}"
  196. when: hostvars['127.0.0.1']['k8s_cni'] == "flannel"
  197. tags: init
  198. - name: Replace flannel network cidr
  199. replace:
  200. path: "{{ flannel_yml_file_path }}"
  201. regexp: "10.244.0.0/16"
  202. replace: "{{ hostvars['127.0.0.1']['k8s_pod_network_cidr'] }}"
  203. when: hostvars['127.0.0.1']['k8s_cni'] == "flannel"
  204. tags: init
  205. - name: Setup Flannel SDN network
  206. command: "kubectl apply -f '{{ flannel_yml_file_path }}'"
  207. when: hostvars['127.0.0.1']['k8s_cni'] == "flannel"
  208. tags: init
  209. - name: Create service account (K8s dashboard) files
  210. copy:
  211. src: create_admin_user.yaml
  212. dest: "{{ k8s_service_account_file_dest }}"
  213. owner: root
  214. group: root
  215. mode: "{{ k8s_service_account_file_mode }}"
  216. tags: init
  217. - name: Check K8s service accounts status
  218. command: "kubectl get serviceaccounts"
  219. changed_when: false
  220. register: k8s_service_accounts
  221. tags: init
  222. - name: Create service account (K8s dashboard)
  223. command: "kubectl create -f '{{ k8s_service_account_file_dest }}'"
  224. changed_when: true
  225. when: "'default' not in k8s_service_accounts.stdout"
  226. tags: init
  227. - name: Create clusterRoleBinding (K8s dashboard) files
  228. copy:
  229. src: create_clusterRoleBinding.yaml
  230. dest: "{{ cluster_role_binding_file_dest }}"
  231. owner: root
  232. group: root
  233. mode: "{{ cluster_role_binding_file_mode }}"
  234. tags: init
  235. - name: Create clusterRoleBinding (K8s dashboard)
  236. command: "kubectl create -f '{{ cluster_role_binding_file_dest }}'"
  237. changed_when: true
  238. failed_when: false
  239. tags: init
  240. - name: Dump bearer token for K8s dashboard login
  241. shell: >
  242. set -o pipefail && \
  243. kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}') > /root/k8s/token
  244. changed_when: true
  245. tags: init
  246. - name: Edge / Workstation Install allows pods to scheudle on manager
  247. command: kubectl taint nodes --all node-role.kubernetes.io/master-
  248. when: groups['manager'][0] == groups['compute'][0] and groups['compute']|length == 1
  249. failed_when: false
  250. tags: init