From aea18955af45dfe24682ffa7553581cbd6c95cbe Mon Sep 17 00:00:00 2001 From: Radar231 Date: Thu, 22 Jul 2021 23:14:56 -0400 Subject: [PATCH] initial checkin --- README.md | 28 ++++++++++++ tasks/main.yml | 114 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 README.md create mode 100644 tasks/main.yml diff --git a/README.md b/README.md new file mode 100644 index 0000000..cb6ccd0 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# Ansible Role: microk8s + +## Introduction + +This role will install a microk8s cluster to three or more target hosts. + +This role requires at least three target hosts, previously deployed. Each +host should have at least 4GB of RAM and at least a 20GB disk. The hosts +should also be running Ubuntu LTS, 16.04, 18.04 or 20.04. + +It is also assumed that the user running the playbook has SSH key access +set up for the root user on the target hosts. + + +This playbook can be run using the following command line; + +ansible-playbook -i -e "username=" microk8s.yml + +ie, +ansible-playbook -i cluster_inventory.yml -e "username=joe" microk8s.yml + +NOTE: + +The following variables must be specified in the calling playbook (usually +done by doing a 'gather_facts' action); + +{{ ansible_distribution }} + diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..8568cb9 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,114 @@ +--- +# tasks file for main.yml + +########################################################### +- name: Check to make sure we have at least {{ minhosts }} target hosts (if cluster) + fail: + msg: "You need to have at least {{ minhosts }} target hosts for a cluster!" + when: (do_cluster == true) and (minhosts > numhosts) + +########################################################### +- name: Check for Ubuntu + fail: + msg: "This playbook is only set up to work on Ubuntu hosts." + when: ansible_distribution != "Ubuntu" + +########################################################### +- name: Check for Ubuntu LTS >= 16.04 + fail: + msg: "This playbook requires Ubuntu LTS, 16.04 or later." + when: not (ansible_distribution_version == "16.04" or + ansible_distribution_version == "18.04" or + ansible_distribution_version == "20.04") + +########################################################### +- name: Make sure snapd is installed + apt: + name: + - snapd + state: present + +########################################################### +- name: Install microk8s + snap: + name: microk8s + classic: true + channel: "{{ microk8s_version }}" + +########################################################### +- name: Add user "{{ username }}" to microk8s group + user: + name: "{{ username }}" + groups: "microk8s" + append: true + when: username != "root" + +########################################################### +- name: Set ownership of /home/{{ username }}/.kube + file: + path: "/home/{{ username }}/.kube" + owner: "{{ username }}" + group: "{{ username }}" + recurse: true + when: username != "root" + +########################################################### +- name: Get cluster-cidr IP range from /var/snap/microk8s/current/args/kube-proxy + shell: + cmd: grep "cluster-cidr" /var/snap//microk8s/current/args/kube-proxy | cut -d'=' -f2 + register: cluster_cidr + when: do_proxy == true + +########################################################### +- name: Get service-cluster-ip IP range from /var/snap/microk8s/current/args/kube-apiserver + shell: + cmd: grep "service-cluster-ip" /var/snap//microk8s/current/args/kube-apiserver | cut -d'=' -f2 + register: service_cluster_ip + when: do_proxy == true + +########################################################### +- name: Config proxy for microk8s + blockinfile: + path: /var/snap/microk8s/current/args/containerd-env + block: | + # proxy configuration + HTTPS_PROXY={{ proxy_host }} + NO_PROXY={{ net_cidrs }},{{ cluster_cidr.stdout }},{{ service_cluster_ip.stdout }} + when: do_proxy == true + +########################################################### +- name: Restart microk8s to apply proxy config + shell: + cmd: /usr/bin/snap restart microk8s + when: do_proxy == true + +########################################################### +- name: Wait for microk8s ready status + shell: source /etc/profile.d/apps-bin-path.sh && microk8s.status --wait-ready + args: + executable: /bin/bash + +########################################################### +- name: Run add-node command on master node "{{ hostvars[groups['master'][0]].inventory_hostname }}" + shell: source /etc/profile.d/apps-bin-path.sh && microk8s.add-node + args: + executable: /bin/bash + register: add_node_cmd + delegate_to: "{{ hostvars[groups['master'][0]].inventory_hostname }}" + when: do_cluster == true + +########################################################### +- name: Run join command on worker nodes + shell: source /etc/profile.d/apps-bin-path.sh && {{ add_node_cmd.stdout_lines[1] }} + args: + executable: /bin/bash + when: (do_cluster == true) and ('workers' in group_names) + +########################################################### +- name: Wait for microk8s ready status + shell: source /etc/profile.d/apps-bin-path.sh && microk8s.status --wait-ready + args: + executable: /bin/bash + when: (do_cluster == true) and ('workers' in group_names) + +# EOF