You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.0 KiB

  1. ---
  2. - name: Set external kube-apiserver endpoint
  3. set_fact:
  4. external_apiserver_address: >-
  5. {%- if loadbalancer_apiserver is defined and loadbalancer_apiserver.address is defined -%}
  6. {{ loadbalancer_apiserver.address }}
  7. {%- else -%}
  8. {{ kube_apiserver_access_address }}
  9. {%- endif -%}
  10. external_apiserver_port: >-
  11. {%- if loadbalancer_apiserver is defined and loadbalancer_apiserver.address is defined and loadbalancer_apiserver.port is defined -%}
  12. {{ loadbalancer_apiserver.port|default(kube_apiserver_port) }}
  13. {%- else -%}
  14. {{ kube_apiserver_port }}
  15. {%- endif -%}
  16. tags:
  17. - facts
  18. - name: Create kube config dir for current/ansible become user
  19. file:
  20. path: "{{ ansible_env.HOME | default('/root') }}/.kube"
  21. mode: "0700"
  22. state: directory
  23. - name: Copy admin kubeconfig to current/ansible become user home
  24. copy:
  25. src: "{{ kube_config_dir }}/admin.conf"
  26. dest: "{{ ansible_env.HOME | default('/root') }}/.kube/config"
  27. remote_src: yes
  28. mode: "0600"
  29. backup: yes
  30. - name: Create kube artifacts dir
  31. file:
  32. path: "{{ artifacts_dir }}"
  33. mode: "0750"
  34. state: directory
  35. delegate_to: localhost
  36. connection: local
  37. become: no
  38. run_once: yes
  39. when: kubeconfig_localhost
  40. - name: Wait for k8s apiserver
  41. wait_for:
  42. host: "{{ kube_apiserver_access_address }}"
  43. port: "{{ kube_apiserver_port }}"
  44. timeout: 180
  45. - name: Create external_kubeconfig dir
  46. file:
  47. path: "{{ kube_config_dir }}/external_kubeconfig"
  48. mode: "0750"
  49. state: directory
  50. when: kubeconfig_localhost
  51. # NOTE(mattymo): Please forgive this workaround
  52. - name: Generate admin kubeconfig with external api endpoint # noqa 302
  53. shell: >-
  54. {{ bin_dir }}/kubeadm
  55. init phase
  56. kubeconfig admin
  57. --kubeconfig-dir {{ kube_config_dir }}/external_kubeconfig
  58. --cert-dir {{ kube_cert_dir }}
  59. --apiserver-advertise-address {{ external_apiserver_address }}
  60. --apiserver-bind-port {{ external_apiserver_port }}
  61. >/dev/null && cat {{ kube_config_dir }}/external_kubeconfig/admin.conf &&
  62. rm -rf {{ kube_config_dir }}/external_kubeconfig
  63. environment: "{{ proxy_env }}"
  64. run_once: yes
  65. register: raw_admin_kubeconfig
  66. when: kubeconfig_localhost
  67. - name: Convert kubeconfig to YAML
  68. set_fact:
  69. admin_kubeconfig: "{{ raw_admin_kubeconfig.stdout | from_yaml }}"
  70. when: kubeconfig_localhost
  71. - name: Override username in kubeconfig
  72. set_fact:
  73. final_admin_kubeconfig: "{{ admin_kubeconfig | combine(override_cluster_name, recursive=true) | combine(override_context, recursive=true) | combine(override_user, recursive=true) }}"
  74. vars:
  75. cluster_infos: "{{ admin_kubeconfig['clusters'][0]['cluster'] }}"
  76. user_certs: "{{ admin_kubeconfig['users'][0]['user'] }}"
  77. username: "kubernetes-admin-{{ cluster_name }}"
  78. context: "kubernetes-admin-{{ cluster_name }}@{{ cluster_name }}"
  79. override_cluster_name: "{{ { 'clusters': [ { 'cluster': cluster_infos, 'name': cluster_name } ] } }}"
  80. override_context: "{{ { 'contexts': [ { 'context': { 'user': username, 'cluster': cluster_name }, 'name': context } ], 'current-context': context } }}"
  81. override_user: "{{ { 'users': [ { 'name': username, 'user': user_certs } ] } }}"
  82. when: kubeconfig_localhost
  83. - name: Write admin kubeconfig on ansible host
  84. copy:
  85. content: "{{ final_admin_kubeconfig | to_nice_yaml(indent=2) }}"
  86. dest: "{{ artifacts_dir }}/admin.conf"
  87. mode: 0600
  88. delegate_to: localhost
  89. connection: local
  90. become: no
  91. run_once: yes
  92. when: kubeconfig_localhost
  93. - name: Copy kubectl binary to ansible host
  94. fetch:
  95. src: "{{ bin_dir }}/kubectl"
  96. dest: "{{ artifacts_dir }}/kubectl"
  97. flat: yes
  98. validate_checksum: no
  99. become: no
  100. run_once: yes
  101. when: kubectl_localhost
  102. - name: create helper script kubectl.sh on ansible host
  103. copy:
  104. content: |
  105. #!/bin/bash
  106. ${BASH_SOURCE%/*}/kubectl --kubeconfig=${BASH_SOURCE%/*}/admin.conf "$@"
  107. dest: "{{ artifacts_dir }}/kubectl.sh"
  108. mode: 0755
  109. become: no
  110. run_once: yes
  111. delegate_to: localhost
  112. connection: local
  113. when: kubectl_localhost and kubeconfig_localhost