minikube_create.yml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ---
  2. - name: "Check if {{ minikube_profile_name }} already exists"
  3. ansible.builtin.command:
  4. argv:
  5. - minikube
  6. - profile
  7. - list
  8. - --light=true
  9. - --output=json
  10. register: profile_check_result
  11. changed_when: False
  12. ignore_errors: true
  13. - name: "Set minikube profile JSON Facts"
  14. set_fact:
  15. minikube_profiles_json: "{{ profile_check_result.stdout | from_json }}"
  16. - name: Set if no profiles exist
  17. set_fact:
  18. no_profiles: "{{ minikube_profiles_json.valid | length == 0 }}"
  19. - name: "Existing minikube profiles"
  20. debug:
  21. msg: "{{ minikube_profiles_json | json_query(profile_name_query) }}"
  22. - name: "Set profiles to be created"
  23. set_fact:
  24. minikube_profiles: "{{ minikube_profiles | combine( { item.key:{'create': ((not item.value.destroy) and ( item.key not in profile_names or no_profiles )) }}, recursive=True ) }}"
  25. loop: "{{ minikube_profiles | dict2items }}"
  26. loop_control:
  27. label: "{{ item.key }}"
  28. vars:
  29. profile_names: "{{ minikube_profiles_json | json_query(profile_name_query) }}"
  30. - name: "Debug::Minikube Clusters Config"
  31. debug:
  32. var: minikube_profiles
  33. - name: "Create Kubeconfig dir"
  34. file:
  35. state: directory
  36. path: "{{ kubeconfig_dir }}"
  37. - name: "Create minikube Kubernetes Cluster"
  38. command:
  39. argv:
  40. - "{{ minikube_binary }}"
  41. - -p
  42. - "{{ item.key }}"
  43. - start
  44. # TODO #2 load the start up parameters from CLI
  45. - "--memory={{ item.value.memory | default(minikube_default_memory) }}"
  46. - "--cpus={{ item.value.cpus | default(minikube_default_cpus) }}"
  47. - "--disk-size={{ item.value.disk_size | default(minikube_default_disk_size) }}"
  48. - "--delete-on-failure=true"
  49. - "--driver={{ minikube_driver }}"
  50. - "--insecure-registry=10.0.0.0/24"
  51. - --kubernetes-version={{ kubernetes_version | default(minikube_kubernetes_version) }}
  52. - "--service-cluster-ip-range={{ item.value.service_cluster_ip_range | default(minikube_service_cluster_ip_range) }}"
  53. register: minikube_start_result
  54. loop: "{{ minikube_profiles | dict2items }}"
  55. loop_control:
  56. label: "{{ item.key }}"
  57. environment:
  58. KUBECONFIG: "{{ kubeconfig_dir }}/{{ item.key }}.kubeconfig"
  59. when: (item.value.create | bool) and not ( item.value.destroy | bool )
  60. - name: "Fail when cluster not started"
  61. fail:
  62. msg: "Unable to start minikube"
  63. when: minikube_start_result.changed and item.rc != 0
  64. loop: "{{ minikube_start_result.results }}"
  65. loop_control:
  66. label: "{{ item.item.key }}"
  67. - name: "Check if local .kube directory exists"
  68. stat:
  69. path: "{{ kubeconfig_dir }}"
  70. register: kubeconfig_dir_result
  71. - debug:
  72. var: kubeconfig_dir_result
  73. - name: "Merge Kubconfig"
  74. set_fact:
  75. kubeconfig: "{{ lookup('fileglob', kubeconfig_dir +'/*.kubeconfig') | regex_replace(',',':') }}"
  76. when: kubeconfig_dir_result.stat.exists
  77. - name: "Merge and Flatten Kubeconfig"
  78. local_action:
  79. module: ansible.builtin.command
  80. argv:
  81. - kubectl
  82. - config
  83. - view
  84. - --flatten
  85. register: merged_kubeconfig
  86. environment:
  87. KUBECONFIG: "{{ kubeconfig }}"
  88. when: kubeconfig_dir_result.stat.exists
  89. - name: "Save work Kubeconfig"
  90. local_action:
  91. module: ansible.builtin.copy
  92. dest: "{{ kubeconfig_dir }}/config"
  93. content: "{{ merged_kubeconfig.stdout | from_yaml | to_nice_yaml(indent=2) }}"
  94. when: kubeconfig_dir_result.stat.exists
  95. - name: "Enabling addon"
  96. ansible.builtin.command:
  97. argv:
  98. - "{{ minikube_binary }}"
  99. - -p
  100. - "{{ item.0.key }}"
  101. - addons
  102. - enable
  103. - "{{ item.1 }}"
  104. loop: "{{ minikube_profiles | dict2items | subelements('value.addons',skip_missing=True)}}"
  105. register: addons_enable_result
  106. changed_when: False
  107. loop_control:
  108. label: "Enabling addon '{{ item.1}}' on '{{ item.0.key }}'"
  109. when: ( ( item.0.value.create | bool ) and not ( item.0.value.destroy | bool ) ) or force_enable_addon
  110. environment:
  111. KUBECONFIG: "{{ playbook_dir }}/.kube/{{ item.0.key }}.kubeconfig"