k8s_init.yml 4.2 KB

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