main.yml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # Copyright 2020 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
  16. command: /usr/sbin/swapoff -a
  17. changed_when: true
  18. tags: init
  19. - name: Get netaddr
  20. setup:
  21. filter: ansible_default_ipv4.address
  22. - name: Get K8s nodes status
  23. command: kubectl get nodes
  24. changed_when: false
  25. ignore_errors: True
  26. register: k8s_nodes
  27. tags: init
  28. - name: Get K8s ready compute nodes
  29. shell: kubectl get node --selector='!node-role.kubernetes.io/master' | grep -v 'NotReady'
  30. changed_when: false
  31. ignore_errors: True
  32. register: k8s_nodes_ready
  33. tags: init
  34. - name: Get K8s not ready compute nodes
  35. shell: kubectl get node --selector='!node-role.kubernetes.io/master' | grep 'NotReady'
  36. changed_when: false
  37. ignore_errors: True
  38. register: k8s_nodes_not_ready
  39. tags: init
  40. - name: Initialize kubeadm
  41. command: "/bin/kubeadm init --pod-network-cidr='{{ hostvars['127.0.0.1']['k8s_pod_network_cidr'] }}' \
  42. --apiserver-advertise-address='{{ ansible_default_ipv4.address }}'"
  43. changed_when: true
  44. when: "'master' not in k8s_nodes.stdout"
  45. register: init_output
  46. tags: init
  47. - name: Setup directory for Kubernetes environment for root
  48. file:
  49. path: "{{ k8s_root_directory }}"
  50. state: directory
  51. mode: "{{ k8s_root_directory_mode }}"
  52. tags: init
  53. - name: Copy Kubernetes config for root
  54. copy:
  55. src: "{{ k8s_config_src }}"
  56. dest: "{{ k8s_config_dest }}"
  57. owner: root
  58. group: root
  59. mode: "{{ k8s_config_file_mode }}"
  60. remote_src: yes
  61. tags: init
  62. - name: Update the kubernetes config file permissions
  63. shell: "chown $(id -u):$(id -g) '{{ k8s_config_dest }}'"
  64. args:
  65. warn: false
  66. changed_when: true
  67. tags: init
  68. - name: Cluster token
  69. shell: >
  70. set -o pipefail && \
  71. kubeadm token list | cut -d ' ' -f1 | sed -n '2p'
  72. changed_when: false
  73. register: K8S_TOKEN
  74. tags: init
  75. - name: CA Hash
  76. shell: >
  77. set -o pipefail && \
  78. openssl x509 -pubkey -in {{ k8s_cert_path }} | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
  79. changed_when: false
  80. register: K8S_MANAGER_CA_HASH
  81. tags: init
  82. - name: Add K8S Manager IP, Token, and Hash to dummy host
  83. add_host:
  84. name: "K8S_TOKEN_HOLDER"
  85. token: "{{ K8S_TOKEN.stdout }}"
  86. hash: "{{ K8S_MANAGER_CA_HASH.stdout }}"
  87. ip: "{{ ansible_default_ipv4.address }}"
  88. k8s_nodes: "{{ k8s_nodes.stdout }}"
  89. k8s_nodes_ready: "{{ k8s_nodes_ready.stdout }}"
  90. k8s_nodes_not_ready: "{{ k8s_nodes_not_ready.stdout }}"
  91. tags: init
  92. - name: Print k8s token
  93. debug:
  94. msg: "[Manager] K8S_TOKEN_HOLDER K8S token is {{ hostvars['K8S_TOKEN_HOLDER']['token'] }}"
  95. verbosity: 2
  96. tags: init
  97. - name: Print k8s hash
  98. debug:
  99. msg: "[Manager] K8S_TOKEN_HOLDER K8S Hash is {{ hostvars['K8S_TOKEN_HOLDER']['hash'] }}"
  100. verbosity: 2
  101. tags: init
  102. - name: Print k8s ansible_default_ipv4.address
  103. debug:
  104. msg: "[Manager] K8S_MANAGER_IP is {{ ansible_default_ipv4.address }}"
  105. verbosity: 2
  106. tags: init
  107. - name: Setup Calico SDN network
  108. command: "kubectl apply -f '{{ calico_yml_url }}'"
  109. when: hostvars['127.0.0.1']['k8s_cni'] == "calico"
  110. tags: init
  111. - name: Setup Flannel SDN network
  112. command: "kubectl apply -f '{{ flannel_yml_url }}'"
  113. when: hostvars['127.0.0.1']['k8s_cni'] == "flannel"
  114. tags: init
  115. - name: Create yaml repo for setup
  116. file:
  117. path: "{{ yaml_repo_dir_path }}"
  118. state: directory
  119. mode: "{{ yaml_repo_dir_mode }}"
  120. tags: init
  121. - name: Create service account (K8s dashboard) files
  122. copy:
  123. src: create_admin_user.yaml
  124. dest: "{{ k8s_service_account_file_dest }}"
  125. owner: root
  126. group: root
  127. mode: "{{ k8s_service_account_file_mode }}"
  128. tags: init
  129. - name: Check K8s service accounts status
  130. command: "kubectl get serviceaccounts"
  131. changed_when: false
  132. register: k8s_service_accounts
  133. tags: init
  134. - name: Create service account (K8s dashboard)
  135. command: "kubectl create -f '{{ k8s_service_account_file_dest }}'"
  136. changed_when: true
  137. when: "'default' not in k8s_service_accounts.stdout"
  138. tags: init
  139. - name: Create clusterRoleBinding (K8s dashboard) files
  140. copy:
  141. src: create_clusterRoleBinding.yaml
  142. dest: "{{ k8s_clusterRoleBinding_file_dest }}"
  143. owner: root
  144. group: root
  145. mode: "{{ k8s_clusterRoleBinding_file_mode }}"
  146. tags: init
  147. - name: Create clusterRoleBinding (K8s dashboard)
  148. command: "kubectl create -f '{{ k8s_clusterRoleBinding_file_dest }}'"
  149. changed_when: true
  150. ignore_errors: True
  151. tags: init
  152. - name: Dump bearer token for K8s dashboard login
  153. shell: >
  154. set -o pipefail && \
  155. kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}') > /root/k8s/token
  156. changed_when: true
  157. tags: init
  158. - name: Edge / Workstation Install allows pods to scheudle on manager
  159. command: kubectl taint nodes --all node-role.kubernetes.io/master-
  160. when: groups['manager'][0] == groups['compute'][0] and groups['compute']|length == 1
  161. tags: init