k8s_init.yml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright 2022 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. - name: Disable SWAP in fstab (2/2)
  19. replace:
  20. path: /etc/fstab
  21. regexp: '^([^#].*?\sswap\s+.*)$'
  22. replace: '# \1'
  23. - name: Get K8s nodes status
  24. command: kubectl get nodes
  25. changed_when: false
  26. failed_when: false
  27. register: k8s_nodes
  28. - name: Get K8s pods status
  29. command: kubectl get pods --all-namespaces
  30. changed_when: false
  31. failed_when: false
  32. register: k8s_pods
  33. - block:
  34. - name: Docker login
  35. command: docker login -u {{ docker_username }} -p {{ docker_password }}
  36. changed_when: true
  37. register: docker_login_output
  38. when: docker_username or docker_password
  39. no_log: true
  40. rescue:
  41. - name: Warning - docker login failed
  42. debug:
  43. msg: "Warning: {{ docker_login_output.stderr }}"
  44. - name: Docker login check
  45. fail:
  46. msg: "{{ docker_login_fail_msg }}"
  47. when: docker_login_output is failed
  48. - name: Initialize kubeadm (This process may take 5-10min)
  49. block:
  50. - name: Initialize kubeadm (This process may take 5-10min)
  51. command: "kubeadm init --pod-network-cidr='{{ appliance_k8s_pod_net_cidr }}' \
  52. --apiserver-advertise-address='{{ public_ip }}'"
  53. changed_when: true
  54. register: init_output
  55. rescue:
  56. - name: Reset kubeadm
  57. command: "kubeadm reset -f"
  58. changed_when: true
  59. - name: Initialize kubeadm (This process may take 5-10min)
  60. command: "kubeadm init --pod-network-cidr='{{ appliance_k8s_pod_net_cidr }}' \
  61. --apiserver-advertise-address='{{ public_ip }}'"
  62. changed_when: true
  63. register: init_output
  64. - name: Get K8s pods status
  65. command: kubectl get pods --all-namespaces
  66. changed_when: false
  67. failed_when: false
  68. register: k8s_pods
  69. when: "'master' not in k8s_nodes.stdout"
  70. - name: Setup directory for Kubernetes environment for root
  71. file:
  72. path: "{{ k8s_root_directory }}"
  73. state: directory
  74. mode: "{{ k8s_root_directory_mode }}"
  75. - name: Copy Kubernetes config for root
  76. copy:
  77. src: "{{ k8s_config_src }}"
  78. dest: "{{ k8s_config_dest }}"
  79. owner: root
  80. group: root
  81. mode: "{{ k8s_config_file_mode }}"
  82. remote_src: yes
  83. - name: Update the kubernetes config file permissions
  84. shell: "chown $(id -u):$(id -g) '{{ k8s_config_dest }}'"
  85. args:
  86. warn: false
  87. changed_when: true
  88. - name: Cluster token
  89. shell: >
  90. set -o pipefail && \
  91. kubeadm token list | cut -d ' ' -f1 | sed -n '2p'
  92. changed_when: false
  93. register: k8s_token
  94. - name: CA Hash
  95. shell: >
  96. set -o pipefail && \
  97. openssl x509 -pubkey -in {{ k8s_cert_path }} | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
  98. changed_when: false
  99. register: k8s_manager_ca_hash
  100. - name: Add K8S Manager IP, Token, and Hash to dummy host
  101. add_host:
  102. name: "K8S_TOKEN_HOLDER"
  103. token: "{{ k8s_token.stdout }}"
  104. hash: "{{ k8s_manager_ca_hash.stdout }}"
  105. ip: "{{ public_ip }}"
  106. - name: Create yaml repo for setup
  107. file:
  108. path: "{{ yaml_repo_dir_path }}"
  109. state: directory
  110. mode: "{{ yaml_repo_dir_mode }}"
  111. - name: Setup Calico SDN network - tigera-operator
  112. command: "kubectl create -f {{ tigera_operator_url }}"
  113. changed_when: true
  114. when: "'tigera-operator' not in k8s_pods.stdout"
  115. - name: Setup Calico SDN network - custom-resources
  116. command: "kubectl create -f {{ calico_yml_url }}"
  117. changed_when: true
  118. failed_when: false
  119. when: "'calico-system' not in k8s_pods.stdout"
  120. - name: Edge / Workstation Install allows pods to schedule on manager
  121. command: kubectl taint nodes --all node-role.kubernetes.io/master-
  122. changed_when: true
  123. failed_when: false