---
# Copyright kubeinit contributors
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
- name: Deploy the cluster nodes
ansible.builtin.include_role:
name: kubeinit.kubeinit.kubeinit_libvirt
tasks_from: deploy_ubuntu_guest.yml
public: yes
loop: "{{ groups['all_cluster_nodes'] + groups['all_extra_nodes'] }}"
loop_control:
loop_var: kubeinit_deployment_node_name
vars:
kubeinit_deployment_delegate: "{{ hostvars[kubeinit_deployment_node_name].target }}"
when: kubeinit_cluster_nodes_deployed is not defined or not kubeinit_cluster_nodes_deployed
- name: Render the cluster template
ansible.builtin.template:
src: "cloud.yml.j2"
dest: "~/cloud.yml"
mode: '0644'
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Render the bundle template
ansible.builtin.template:
src: "bundle.yml.j2"
dest: "~/bundle.yml"
mode: '0644'
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Add host keys to known_hosts
ansible.builtin.known_hosts:
name: "{{ hostvars[item].ansible_host }}"
key: "{{ hostvars[item].ansible_host }} {{ hostvars[item].ssh_host_key_ecdsa }}"
state: present
loop: "{{ groups['all_cluster_nodes'] + groups['all_extra_nodes'] }}"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Install the CDK juju installer
ansible.builtin.shell: |
set -eo pipefail
wget {{ kubeinit_cdk_juju_uri }}
xzcat {{ kubeinit_cdk_juju_file }} | (cd /usr/bin; tar xvf - ./juju)
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Add the CDK cluster
ansible.builtin.shell: |
juju add-cloud --client {{ kubeinit_cluster_name }} -f cloud.yml
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Copy juju client key to cluster nodes
ansible.builtin.shell: |
ssh-copy-id -i ~/.local/share/juju/ssh/juju_id_rsa root@{{ hostvars[item].ansible_host }}
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
loop: "{{ groups['all_cluster_nodes'] + groups['all_extra_nodes'] }}"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Bootstrap the CDK controller
ansible.builtin.shell: |
juju bootstrap \
--debug \
--bootstrap-series={{ kubeinit_cdk_series }} \
manual/root@{{ hostvars[groups['all_extra_nodes'][0]].ansible_host }} > juju-bootstrap.log 2>&1
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Show juju info
ansible.builtin.shell: |
juju controllers --format=yaml
# juju status --format=yaml
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Add model
ansible.builtin.shell: |
juju add-model {{ kubeinit_cluster_name }}
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Add machines
ansible.builtin.shell: |
juju add-machine --debug \
ssh:root@{{ hostvars[item].ansible_host }} > juju-add-machine-{{ item }}.log 2>&1
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
loop: "{{ groups['all_cluster_nodes'] }}"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Verify that all cluster nodes are started
ansible.builtin.shell: |
set -o pipefail
juju machines | grep started
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
retries: 60
delay: 60
delegate_to: "{{ kubeinit_provision_service_node }}"
until: _result.stdout_lines | list | length == kubeinit_cluster_node_count|int
- name: Deploy CDK
ansible.builtin.shell: |
juju deploy \
./bundle.yml \
--force \
--map-machines=existing{% for item in groups['all_cluster_nodes'] -%},{{ loop.index0 }}={{ loop.index0 }}{%- endfor %}
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
retries: 20
delegate_to: "{{ kubeinit_provision_service_node }}"
until: _result.rc == 0
- name: Verify that the etcd cluster is up and healthy
ansible.builtin.shell: |
set -o pipefail
juju status --format=json | \
sed 's/application-status/application_status/g' | \
jq -r .applications.etcd.application_status.message
args:
executable: /bin/bash
vars:
all_controllers_healthy_text: "Healthy with {{ kubeinit_controller_count }} known peer{{ 's' if (kubeinit_controller_count|int > 1) }}"
register: _result
changed_when: "_result.rc == 0"
retries: 100
delay: 60
delegate_to: "{{ kubeinit_provision_service_node }}"
until: all_controllers_healthy_text == _result.stdout
- name: Verify that the kubernetes_master application is up and healthy
ansible.builtin.shell: |
set -o pipefail
juju status --format=json | \
sed 's/application-status/application_status/g' | \
sed 's/kubernetes-control-plane/kubernetes_control_plane/g' | \
jq -r .applications.kubernetes_control_plane.application_status.message
args:
executable: /bin/bash
vars:
healthy_text: "Kubernetes control-plane running."
register: _result
changed_when: "_result.rc == 0"
retries: 100
delay: 60
delegate_to: "{{ kubeinit_provision_service_node }}"
until: healthy_text == _result.stdout
- name: Create kube directory
ansible.builtin.file:
path: ~/.kube
state: directory
mode: '0755'
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Get the kubectl binary and kubeconfig from the first controller node
ansible.builtin.shell: |
juju scp kubernetes-control-plane/0:/usr/bin/kubectl /usr/bin/kubectl
juju scp kubernetes-control-plane/0:config ~/.kube/config
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Install kustomize
ansible.builtin.shell: |
curl -sL https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv5.1.0/kustomize_v5.1.0_linux_amd64.tar.gz > kustomize.tar.gz
tar xzf ./kustomize.tar.gz
mv ./kustomize /bin/
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Autoload kubeconfig
ansible.builtin.shell: |
echo "export KUBECONFIG=~/.kube/config" >> ~/.bashrc
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"
delegate_to: "{{ kubeinit_provision_service_node }}"
- name: Finished deploying cluster
ansible.builtin.shell: |
echo "Finished deploying CDK cluster"
args:
executable: /bin/bash
register: _result
changed_when: "_result.rc == 0"