From 3f786542d3161fd1dc87f982ffe6d6e2382f968e Mon Sep 17 00:00:00 2001
From: Giacomo Longo <gabibbo97@gmail.com>
Date: Thu, 11 Oct 2018 08:32:10 +0200
Subject: [PATCH] Automatically infer bootstrap_os (#3498)

* Automatically infer bootstrap_os

* Rename bootstrap os to os_family
---
 Vagrantfile                                 | 19 +++++++++----------
 contrib/terraform/aws/README.md             |  2 +-
 contrib/terraform/openstack/README.md       |  5 -----
 docs/coreos.md                              |  1 -
 inventory/sample/group_vars/all/all.yml     |  6 ------
 roles/bootstrap-os/tasks/main.yml           | 21 +++++++++++++++++++++
 roles/kubespray-defaults/defaults/main.yaml |  4 ----
 tests/files/do_ubuntu-canal-ha.yml          |  1 -
 tests/files/gce_coreos-alpha-weave-ha.yml   |  1 -
 tests/files/gce_coreos-calico-aio.yml       |  1 -
 tests/files/gce_coreos-canal.yml            |  1 -
 tests/files/gce_coreos-cilium.yml           |  1 -
 tests/files/gce_coreos-vault-upgrade.yml    |  1 -
 tests/files/gce_debian8-calico-upgrade.yml  |  1 -
 tests/files/gce_opensuse-canal.yml          |  1 -
 tests/files/gce_ubuntu-canal-ha.yml         |  1 -
 tests/files/gce_ubuntu-canal-kubeadm.yml    |  1 -
 tests/files/gce_ubuntu-flannel-sep.yml      |  1 -
 tests/files/gce_ubuntu-rkt-sep.yml          |  1 -
 tests/files/gce_ubuntu-vault-sep.yml        |  1 -
 tests/files/gce_ubuntu-weave-sep.yml        |  1 -
 tests/files/gce_ubuntu18-flannel-aio.yml    |  2 --
 22 files changed, 31 insertions(+), 43 deletions(-)

diff --git a/Vagrantfile b/Vagrantfile
index 5e690cfff..1c0d6d7b9 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -13,15 +13,15 @@ COREOS_URL_TEMPLATE = "https://storage.googleapis.com/%s.release.core-os.net/amd
 DISK_UUID = Time.now.utc.to_i
 
 SUPPORTED_OS = {
-  "coreos-stable" => {box: "coreos-stable",      bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
-  "coreos-alpha"  => {box: "coreos-alpha",       bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
-  "coreos-beta"   => {box: "coreos-beta",        bootstrap_os: "coreos", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
-  "ubuntu1604"    => {box: "generic/ubuntu1604", bootstrap_os: "ubuntu", user: "vagrant"},
-  "ubuntu1804"    => {box: "generic/ubuntu1804", bootstrap_os: "ubuntu", user: "vagrant"},
-  "centos"        => {box: "centos/7",           bootstrap_os: "centos", user: "vagrant"},
-  "fedora"        => {box: "fedora/28-cloud-base", bootstrap_os: "fedora", user: "vagrant"},
-  "opensuse"      => {box: "opensuse/openSUSE-42.3-x86_64", bootstrap_os: "opensuse", use: "vagrant"},
-  "opensuse-tumbleweed" => {box: "opensuse/openSUSE-Tumbleweed-x86_64", bootstrap_os: "opensuse", use: "vagrant"},
+  "coreos-stable" => {box: "coreos-stable",      user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
+  "coreos-alpha"  => {box: "coreos-alpha",       user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
+  "coreos-beta"   => {box: "coreos-beta",        user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
+  "ubuntu1604"    => {box: "generic/ubuntu1604", user: "vagrant"},
+  "ubuntu1804"    => {box: "generic/ubuntu1804", user: "vagrant"},
+  "centos"        => {box: "centos/7",           user: "vagrant"},
+  "fedora"        => {box: "fedora/28-cloud-base", user: "vagrant"},
+  "opensuse"      => {box: "opensuse/openSUSE-42.3-x86_64", use: "vagrant"},
+  "opensuse-tumbleweed" => {box: "opensuse/openSUSE-Tumbleweed-x86_64", use: "vagrant"},
 }
 
 # Defaults for config options defined in CONFIG
@@ -138,7 +138,6 @@ Vagrant.configure("2") do |config|
       ip = "#{$subnet}.#{i+100}"
       host_vars[vm_name] = {
         "ip": ip,
-        "bootstrap_os": SUPPORTED_OS[$os][:bootstrap_os],
         "local_release_dir" => $local_release_dir,
         "download_run_once": "False",
         "kube_network_plugin": $network_plugin
diff --git a/contrib/terraform/aws/README.md b/contrib/terraform/aws/README.md
index e677869d6..bb7e42476 100644
--- a/contrib/terraform/aws/README.md
+++ b/contrib/terraform/aws/README.md
@@ -43,7 +43,7 @@ ssh -F ./ssh-bastion.conf user@$ip
 
 Example (this one assumes you are using CoreOS)
 ```commandline
-ansible-playbook -i ./inventory/hosts ./cluster.yml -e ansible_user=core -e bootstrap_os=coreos -b --become-user=root --flush-cache
+ansible-playbook -i ./inventory/hosts ./cluster.yml -e ansible_user=core -b --become-user=root --flush-cache
 ```
 ***Using other distrib than CoreOs***
 If you want to use another distribution than CoreOS, you can modify the search filters of the 'data "aws_ami" "distro"' in variables.tf.
diff --git a/contrib/terraform/openstack/README.md b/contrib/terraform/openstack/README.md
index fbfc38990..1b49603c9 100644
--- a/contrib/terraform/openstack/README.md
+++ b/contrib/terraform/openstack/README.md
@@ -341,11 +341,6 @@ If it fails try to connect manually via SSH.  It could be something as simple as
 ### Configure cluster variables
 
 Edit `inventory/$CLUSTER/group_vars/all.yml`:
-- Set variable **bootstrap_os** appropriately for your desired image:
-```
-# Valid bootstrap options (required): ubuntu, coreos, centos, none
-bootstrap_os: coreos
-```
 - **bin_dir**:
 ```
 # Directory where the binaries will be installed
diff --git a/docs/coreos.md b/docs/coreos.md
index f2884bba3..bd0c735f6 100644
--- a/docs/coreos.md
+++ b/docs/coreos.md
@@ -6,7 +6,6 @@ Example with Ansible:
 Before running the cluster playbook you must satisfy the following requirements:
 
 General CoreOS Pre-Installation Notes:
-- You should set the bootstrap_os variable to `coreos`
 - Ensure that the bin_dir is set to `/opt/bin`
 - ansible_python_interpreter should be `/opt/bin/python`. This will be laid down by the bootstrap task.
 - The default resolvconf_mode setting of `docker_dns` **does not** work for CoreOS. This is because we do not edit the systemd service file for docker on CoreOS nodes. Instead, just use the `host_resolvconf` mode. It should work out of the box.
diff --git a/inventory/sample/group_vars/all/all.yml b/inventory/sample/group_vars/all/all.yml
index 68e670ba9..042f1580a 100644
--- a/inventory/sample/group_vars/all/all.yml
+++ b/inventory/sample/group_vars/all/all.yml
@@ -1,9 +1,3 @@
-## Valid bootstrap options (required): ubuntu, coreos, centos, none
-## If the OS is not listed here, it means it doesn't require extra/bootstrap steps.
-## In example, python is not available on 'coreos' so it must be installed before
-## anything else. In the opposite, Debian has already all its dependencies fullfiled, then bootstrap_os should be set to `none`.
-bootstrap_os: none
-
 ## Directory where etcd data stored
 etcd_data_dir: /var/lib/etcd
 
diff --git a/roles/bootstrap-os/tasks/main.yml b/roles/bootstrap-os/tasks/main.yml
index af49faefe..c8e63a211 100644
--- a/roles/bootstrap-os/tasks/main.yml
+++ b/roles/bootstrap-os/tasks/main.yml
@@ -1,4 +1,25 @@
 ---
+- name: Fetch /etc/os-release
+  raw: cat /etc/os-release
+  register: os_release
+  changed_when: false
+
+- name: Set bootstrap_os
+  set_fact:
+    os_family: >-
+      {%- if 'Ubuntu' in os_release.stdout -%}
+      ubuntu
+      {%- elif 'Debian' in os_release.stdout -%}
+      debian
+      {%- elif 'CoreOS' in os_release.stdout -%}
+      coreos
+      {%- elif 'Fedora' in os_release.stdout -%}
+      fedora
+      {%- elif 'CentOS' in os_release.stdout -%}
+      centos
+      {%- elif 'OpenSUSE' in os_release.stdout -%}
+      opensuse
+      {% endif %}
 
 - include_tasks: bootstrap-ubuntu.yml
   when: os_family == "ubuntu"
diff --git a/roles/kubespray-defaults/defaults/main.yaml b/roles/kubespray-defaults/defaults/main.yaml
index 6d8f8c497..91a1e5cfa 100644
--- a/roles/kubespray-defaults/defaults/main.yaml
+++ b/roles/kubespray-defaults/defaults/main.yaml
@@ -1,8 +1,4 @@
 ---
-## Required for bootstrap-os/preinstall/download roles and setting facts
-# Valid bootstrap options (required): ubuntu, coreos, centos, none
-bootstrap_os: none
-
 # Use proxycommand if bastion host is in group all
 # This change obseletes editing ansible.cfg file depending on bastion existance
 ansible_ssh_common_args: "{% if 'bastion' in groups['all'] %} -o ProxyCommand='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -W %h:%p {{ hostvars['bastion']['ansible_user'] }}@{{ hostvars['bastion']['ansible_host'] }} {% if ansible_ssh_private_key_file is defined %}-i {{ ansible_ssh_private_key_file }}{% endif %} ' {% endif %}"
diff --git a/tests/files/do_ubuntu-canal-ha.yml b/tests/files/do_ubuntu-canal-ha.yml
index e91dfd7c1..05b5737d7 100644
--- a/tests/files/do_ubuntu-canal-ha.yml
+++ b/tests/files/do_ubuntu-canal-ha.yml
@@ -3,7 +3,6 @@ cloud_region: nyc3
 mode: ha
 
 # Deployment settings
-bootstrap_os: ubuntu
 kube_network_plugin: canal
 deploy_netchecker: true
 kubedns_min_replicas: 1
diff --git a/tests/files/gce_coreos-alpha-weave-ha.yml b/tests/files/gce_coreos-alpha-weave-ha.yml
index 883a67e2a..6754a5155 100644
--- a/tests/files/gce_coreos-alpha-weave-ha.yml
+++ b/tests/files/gce_coreos-alpha-weave-ha.yml
@@ -7,7 +7,6 @@ startup_script: 'systemctl disable locksmithd && systemctl stop locksmithd'
 
 # Deployment settings
 kube_network_plugin: weave
-bootstrap_os: coreos
 resolvconf_mode: host_resolvconf # this is required as long as the coreos stable channel uses docker < 1.12
 deploy_netchecker: true
 kubedns_min_replicas: 1
diff --git a/tests/files/gce_coreos-calico-aio.yml b/tests/files/gce_coreos-calico-aio.yml
index 7f395abf1..ca33bd982 100644
--- a/tests/files/gce_coreos-calico-aio.yml
+++ b/tests/files/gce_coreos-calico-aio.yml
@@ -8,7 +8,6 @@ startup_script: 'systemctl disable locksmithd && systemctl stop locksmithd'
 
 # Deployment settings
 no_group_vars: true
-bootstrap_os: coreos
 kube_network_plugin: calico
 resolvconf_mode: host_resolvconf # this is required as long as the coreos stable channel uses docker < 1.12
 deploy_netchecker: true
diff --git a/tests/files/gce_coreos-canal.yml b/tests/files/gce_coreos-canal.yml
index a3a750fd9..ca4cdd98d 100644
--- a/tests/files/gce_coreos-canal.yml
+++ b/tests/files/gce_coreos-canal.yml
@@ -6,7 +6,6 @@ startup_script: 'systemctl disable locksmithd && systemctl stop locksmithd'
 
 # Deployment settings
 kube_network_plugin: canal
-bootstrap_os: coreos
 resolvconf_mode: host_resolvconf # this is required as long as the coreos stable channel uses docker < 1.12
 deploy_netchecker: true
 kubedns_min_replicas: 1
diff --git a/tests/files/gce_coreos-cilium.yml b/tests/files/gce_coreos-cilium.yml
index 1778929f0..eb2b002c5 100644
--- a/tests/files/gce_coreos-cilium.yml
+++ b/tests/files/gce_coreos-cilium.yml
@@ -6,7 +6,6 @@ startup_script: 'systemctl disable locksmithd && systemctl stop locksmithd'
 
 # Deployment settings
 kube_network_plugin: cilium
-bootstrap_os: coreos
 resolvconf_mode: host_resolvconf # this is required as long as the coreos stable channel uses docker < 1.12
 deploy_netchecker: true
 enable_network_policy: true
diff --git a/tests/files/gce_coreos-vault-upgrade.yml b/tests/files/gce_coreos-vault-upgrade.yml
index 643027188..e0c2ac7d5 100644
--- a/tests/files/gce_coreos-vault-upgrade.yml
+++ b/tests/files/gce_coreos-vault-upgrade.yml
@@ -5,7 +5,6 @@ cloud_region: us-central1-b
 mode: aio
 
 # Instance settings
-bootstrap_os: coreos
 cert_management: vault
 kube_network_plugin: flannel
 deploy_netchecker: true
diff --git a/tests/files/gce_debian8-calico-upgrade.yml b/tests/files/gce_debian8-calico-upgrade.yml
index 01041007a..54c597925 100644
--- a/tests/files/gce_debian8-calico-upgrade.yml
+++ b/tests/files/gce_debian8-calico-upgrade.yml
@@ -5,7 +5,6 @@ mode: default
 
 # Deployment settings
 kube_network_plugin: calico
-bootstrap_os: debian
 deploy_netchecker: true
 kubedns_min_replicas: 1
 cloud_provider: gce
diff --git a/tests/files/gce_opensuse-canal.yml b/tests/files/gce_opensuse-canal.yml
index e5bea621c..eb30255ed 100644
--- a/tests/files/gce_opensuse-canal.yml
+++ b/tests/files/gce_opensuse-canal.yml
@@ -4,7 +4,6 @@ cloud_region: us-central1-c
 mode: default
 
 # Deployment settings
-bootstrap_os: opensuse
 kube_network_plugin: canal
 deploy_netchecker: true
 kubedns_min_replicas: 1
diff --git a/tests/files/gce_ubuntu-canal-ha.yml b/tests/files/gce_ubuntu-canal-ha.yml
index 241c7d5a2..351d64622 100644
--- a/tests/files/gce_ubuntu-canal-ha.yml
+++ b/tests/files/gce_ubuntu-canal-ha.yml
@@ -4,7 +4,6 @@ cloud_region: us-central1-c
 mode: ha
 
 # Deployment settings
-bootstrap_os: ubuntu
 kube_network_plugin: canal
 deploy_netchecker: true
 kubedns_min_replicas: 1
diff --git a/tests/files/gce_ubuntu-canal-kubeadm.yml b/tests/files/gce_ubuntu-canal-kubeadm.yml
index affb852fe..afe46938b 100644
--- a/tests/files/gce_ubuntu-canal-kubeadm.yml
+++ b/tests/files/gce_ubuntu-canal-kubeadm.yml
@@ -5,7 +5,6 @@ cloud_region: us-central1-c
 mode: ha
 
 # Deployment settings
-bootstrap_os: ubuntu
 kube_network_plugin: canal
 kubeadm_enabled: true
 dynamic_kubelet_configuration: true
diff --git a/tests/files/gce_ubuntu-flannel-sep.yml b/tests/files/gce_ubuntu-flannel-sep.yml
index df77a46b3..6a5568e1d 100644
--- a/tests/files/gce_ubuntu-flannel-sep.yml
+++ b/tests/files/gce_ubuntu-flannel-sep.yml
@@ -4,7 +4,6 @@ cloud_region: us-central1-a
 mode: separate
 
 # Deployment settings
-bootstrap_os: ubuntu
 kube_network_plugin: flannel
 deploy_netchecker: true
 kubedns_min_replicas: 1
diff --git a/tests/files/gce_ubuntu-rkt-sep.yml b/tests/files/gce_ubuntu-rkt-sep.yml
index b15989231..718a23ba1 100644
--- a/tests/files/gce_ubuntu-rkt-sep.yml
+++ b/tests/files/gce_ubuntu-rkt-sep.yml
@@ -4,7 +4,6 @@ cloud_region: us-central1-c
 mode: separate
 
 # Deployment settings
-bootstrap_os: ubuntu
 kube_network_plugin: flannel
 etcd_deployment: rkt
 kubelet_deployment: rkt
diff --git a/tests/files/gce_ubuntu-vault-sep.yml b/tests/files/gce_ubuntu-vault-sep.yml
index 60ce0c37f..c09be7c38 100644
--- a/tests/files/gce_ubuntu-vault-sep.yml
+++ b/tests/files/gce_ubuntu-vault-sep.yml
@@ -5,7 +5,6 @@ cloud_region: us-central1-b
 mode: separate
 
 # Instance settings
-bootstrap_os: ubuntu
 cert_management: vault
 kube_network_plugin: canal
 deploy_netchecker: true
diff --git a/tests/files/gce_ubuntu-weave-sep.yml b/tests/files/gce_ubuntu-weave-sep.yml
index 4598672d1..a805b24bf 100644
--- a/tests/files/gce_ubuntu-weave-sep.yml
+++ b/tests/files/gce_ubuntu-weave-sep.yml
@@ -4,7 +4,6 @@ cloud_region: us-central1-c
 mode: separate
 
 # Deployment settings
-bootstrap_os: ubuntu
 kube_network_plugin: weave
 deploy_netchecker: true
 kubedns_min_replicas: 1
diff --git a/tests/files/gce_ubuntu18-flannel-aio.yml b/tests/files/gce_ubuntu18-flannel-aio.yml
index 9df1fa7ed..3ef7aa83c 100644
--- a/tests/files/gce_ubuntu18-flannel-aio.yml
+++ b/tests/files/gce_ubuntu18-flannel-aio.yml
@@ -5,8 +5,6 @@ cloud_machine_type: "n1-standard-1"
 mode: aio
 
 # Deployment settings
-kubeadm_enabled: false
-bootstrap_os: ubuntu
 kube_network_plugin: flannel
 dynamic_kubelet_configuration: true
 deploy_netchecker: true
-- 
GitLab