From c58497cde9867a0b5833d4c940b2d9d8210e478b Mon Sep 17 00:00:00 2001
From: Max Gautier <mg@max.gautier.name>
Date: Wed, 27 Mar 2024 12:58:53 +0000
Subject: [PATCH] Refactor bootstrap-os (#10983)

* Remove leftover files for Coreos

Coreos was replaced by flatcar in 058438a25 but the file was copied
instead of moved.

* Remove workarounds for resolved ansible issues

* boostrap: Use first_found to include per distro

Using directly ID and VARIANT_ID with first_found allow for less manual
includes.
Distro "families" are simply handled by symlinks.

* boostrap: don't set ansible_python_interpreter

- Allows users to override the chosen python_interpreter with group_vars
  easily (group_vars have lesser precedence than facts)
- Allows us to use vars at the task scope to use a virtual env

Ansible python discovery has improved, so those workarounds should not
be necessary anymore.
Special workaround for Flatcar, due to upstream ansible not willing to
support it.
---
 .../{bootstrap-amazon.yml => amazon.yml}      |  0
 roles/bootstrap-os/tasks/bootstrap-coreos.yml | 37 -------
 .../{bootstrap-centos.yml => centos.yml}      |  0
 ...ootstrap-clearlinux.yml => clearlinux.yml} |  0
 .../{bootstrap-debian.yml => debian.yml}      | 16 +--
 ...ap-fedora-coreos.yml => fedora-coreos.yml} |  6 --
 .../{bootstrap-fedora.yml => fedora.yml}      |  0
 .../{bootstrap-flatcar.yml => flatcar.yml}    | 17 ++--
 roles/bootstrap-os/tasks/main.yml             | 97 +++++--------------
 roles/bootstrap-os/tasks/opensuse-leap.yml    |  1 +
 .../tasks/opensuse-tumbleweed.yml             |  1 +
 .../{bootstrap-opensuse.yml => opensuse.yml}  |  0
 .../{bootstrap-redhat.yml => redhat.yml}      |  0
 roles/bootstrap-os/tasks/ubuntu.yml           |  1 +
 roles/bootstrap-os/vars/fedora-coreos.yml     |  2 +
 roles/bootstrap-os/vars/flatcar.yml           |  2 +
 16 files changed, 41 insertions(+), 139 deletions(-)
 rename roles/bootstrap-os/tasks/{bootstrap-amazon.yml => amazon.yml} (100%)
 delete mode 100644 roles/bootstrap-os/tasks/bootstrap-coreos.yml
 rename roles/bootstrap-os/tasks/{bootstrap-centos.yml => centos.yml} (100%)
 rename roles/bootstrap-os/tasks/{bootstrap-clearlinux.yml => clearlinux.yml} (100%)
 rename roles/bootstrap-os/tasks/{bootstrap-debian.yml => debian.yml} (80%)
 rename roles/bootstrap-os/tasks/{bootstrap-fedora-coreos.yml => fedora-coreos.yml} (90%)
 rename roles/bootstrap-os/tasks/{bootstrap-fedora.yml => fedora.yml} (100%)
 rename roles/bootstrap-os/tasks/{bootstrap-flatcar.yml => flatcar.yml} (54%)
 create mode 120000 roles/bootstrap-os/tasks/opensuse-leap.yml
 create mode 120000 roles/bootstrap-os/tasks/opensuse-tumbleweed.yml
 rename roles/bootstrap-os/tasks/{bootstrap-opensuse.yml => opensuse.yml} (100%)
 rename roles/bootstrap-os/tasks/{bootstrap-redhat.yml => redhat.yml} (100%)
 create mode 120000 roles/bootstrap-os/tasks/ubuntu.yml
 create mode 100644 roles/bootstrap-os/vars/fedora-coreos.yml
 create mode 100644 roles/bootstrap-os/vars/flatcar.yml

diff --git a/roles/bootstrap-os/tasks/bootstrap-amazon.yml b/roles/bootstrap-os/tasks/amazon.yml
similarity index 100%
rename from roles/bootstrap-os/tasks/bootstrap-amazon.yml
rename to roles/bootstrap-os/tasks/amazon.yml
diff --git a/roles/bootstrap-os/tasks/bootstrap-coreos.yml b/roles/bootstrap-os/tasks/bootstrap-coreos.yml
deleted file mode 100644
index 737a7ec94..000000000
--- a/roles/bootstrap-os/tasks/bootstrap-coreos.yml
+++ /dev/null
@@ -1,37 +0,0 @@
----
-# CoreOS ships without Python installed
-
-- name: Check if bootstrap is needed
-  raw: stat /opt/bin/.bootstrapped
-  register: need_bootstrap
-  failed_when: false
-  changed_when: false
-  tags:
-    - facts
-
-- name: Force binaries directory for Container Linux by CoreOS and Flatcar
-  set_fact:
-    bin_dir: "/opt/bin"
-  tags:
-    - facts
-
-- name: Run bootstrap.sh
-  script: bootstrap.sh
-  become: true
-  environment: "{{ proxy_env }}"
-  when:
-    - need_bootstrap.rc != 0
-
-- name: Set the ansible_python_interpreter fact
-  set_fact:
-    ansible_python_interpreter: "{{ bin_dir }}/python"
-  tags:
-    - facts
-
-- name: Disable auto-upgrade
-  systemd:
-    name: locksmithd.service
-    masked: true
-    state: stopped
-  when:
-    - coreos_locksmithd_disable
diff --git a/roles/bootstrap-os/tasks/bootstrap-centos.yml b/roles/bootstrap-os/tasks/centos.yml
similarity index 100%
rename from roles/bootstrap-os/tasks/bootstrap-centos.yml
rename to roles/bootstrap-os/tasks/centos.yml
diff --git a/roles/bootstrap-os/tasks/bootstrap-clearlinux.yml b/roles/bootstrap-os/tasks/clearlinux.yml
similarity index 100%
rename from roles/bootstrap-os/tasks/bootstrap-clearlinux.yml
rename to roles/bootstrap-os/tasks/clearlinux.yml
diff --git a/roles/bootstrap-os/tasks/bootstrap-debian.yml b/roles/bootstrap-os/tasks/debian.yml
similarity index 80%
rename from roles/bootstrap-os/tasks/bootstrap-debian.yml
rename to roles/bootstrap-os/tasks/debian.yml
index 47bad2047..9b18baa06 100644
--- a/roles/bootstrap-os/tasks/bootstrap-debian.yml
+++ b/roles/bootstrap-os/tasks/debian.yml
@@ -55,22 +55,10 @@
   raw: apt-get update --allow-releaseinfo-change
   become: true
   when:
-    - '''ID=debian'' in os_release.stdout_lines'
-    - '''VERSION_ID="10"'' in os_release.stdout_lines or ''VERSION_ID="11"'' in os_release.stdout_lines'
+    - os_release_dict['ID'] == 'debian'
+    - os_release_dict['VERSION_ID'] in ["10", "11"]
   register: bootstrap_update_apt_result
   changed_when:
     - '"changed its" in bootstrap_update_apt_result.stdout'
     - '"value from" in bootstrap_update_apt_result.stdout'
   ignore_errors: true
-
-- name: Set the ansible_python_interpreter fact
-  set_fact:
-    ansible_python_interpreter: "/usr/bin/python3"
-
-# Workaround for https://github.com/ansible/ansible/issues/25543
-- name: Install dbus for the hostname module
-  package:
-    name: dbus
-    state: present
-    use: apt
-  become: true
diff --git a/roles/bootstrap-os/tasks/bootstrap-fedora-coreos.yml b/roles/bootstrap-os/tasks/fedora-coreos.yml
similarity index 90%
rename from roles/bootstrap-os/tasks/bootstrap-fedora-coreos.yml
rename to roles/bootstrap-os/tasks/fedora-coreos.yml
index 91dc020c4..b8c0f3fe7 100644
--- a/roles/bootstrap-os/tasks/bootstrap-fedora-coreos.yml
+++ b/roles/bootstrap-os/tasks/fedora-coreos.yml
@@ -38,9 +38,3 @@
     delay: 5
     sleep: 5
   when: need_bootstrap.rc != 0
-
-- name: Store the fact if this is an fedora core os host
-  set_fact:
-    is_fedora_coreos: True
-  tags:
-    - facts
diff --git a/roles/bootstrap-os/tasks/bootstrap-fedora.yml b/roles/bootstrap-os/tasks/fedora.yml
similarity index 100%
rename from roles/bootstrap-os/tasks/bootstrap-fedora.yml
rename to roles/bootstrap-os/tasks/fedora.yml
diff --git a/roles/bootstrap-os/tasks/bootstrap-flatcar.yml b/roles/bootstrap-os/tasks/flatcar.yml
similarity index 54%
rename from roles/bootstrap-os/tasks/bootstrap-flatcar.yml
rename to roles/bootstrap-os/tasks/flatcar.yml
index b0f3a9eb8..d5ecda8fa 100644
--- a/roles/bootstrap-os/tasks/bootstrap-flatcar.yml
+++ b/roles/bootstrap-os/tasks/flatcar.yml
@@ -9,12 +9,6 @@
   tags:
     - facts
 
-- name: Force binaries directory for Flatcar Container Linux by Kinvolk
-  set_fact:
-    bin_dir: "/opt/bin"
-  tags:
-    - facts
-
 - name: Run bootstrap.sh
   script: bootstrap.sh
   become: true
@@ -22,11 +16,14 @@
   when:
     - need_bootstrap.rc != 0
 
-- name: Set the ansible_python_interpreter fact
+# Workaround ansible https://github.com/ansible/ansible/pull/82821
+# We set the interpreter rather than ansible_python_interpreter to allow
+# - using virtual env with task level ansible_python_interpreter later
+# - let users specify an ansible_python_interpreter in group_vars
+
+- name: Make interpreter discovery works on Flatcar
   set_fact:
-    ansible_python_interpreter: "{{ bin_dir }}/python"
-  tags:
-    - facts
+    ansible_interpreter_python_fallback: "{{ ansible_interpreter_python_fallback + [ '/opt/bin/python' ] }}"
 
 - name: Disable auto-upgrade
   systemd:
diff --git a/roles/bootstrap-os/tasks/main.yml b/roles/bootstrap-os/tasks/main.yml
index 73c9e060f..d1e5f7371 100644
--- a/roles/bootstrap-os/tasks/main.yml
+++ b/roles/bootstrap-os/tasks/main.yml
@@ -6,47 +6,29 @@
   # This command should always run, even in check mode
   check_mode: false
 
-- name: Bootstrap CentOS
-  include_tasks: bootstrap-centos.yml
-  when: '''ID="centos"'' in os_release.stdout_lines or ''ID="ol"'' in os_release.stdout_lines or ''ID="almalinux"'' in os_release.stdout_lines or ''ID="rocky"'' in os_release.stdout_lines or ''ID="kylin"'' in os_release.stdout_lines  or ''ID="uos"'' in os_release.stdout_lines or ''ID="openEuler"'' in os_release.stdout_lines'
+- name: Include distro specifics vars and tasks
+  vars:
+    os_release_dict: "{{ os_release.stdout_lines | select('regex', '^.+=.*$') | map('regex_replace', '\"', '') |
+                         map('split', '=') | community.general.dict }}"
+  block:
+  - name: Include vars
+    include_vars: "{{ item }}"
+    tags:
+    - facts
+    with_first_found:
+    - &search
+      files:
+      - "{{ os_release_dict['ID'] }}-{{ os_release_dict['VARIANT_ID'] }}.yml"
+      - "{{ os_release_dict['ID'] }}.yml"
+      paths:
+      - vars/
+      skip: True
+  - name: Include tasks
+    include_tasks: "{{ item }}"
+    with_first_found:
+    - <<: *search
+      paths: []
 
-- name: Bootstrap Amazon
-  include_tasks: bootstrap-amazon.yml
-  when: '''ID="amzn"'' in os_release.stdout_lines'
-
-- name: Bootstrap RedHat
-  include_tasks: bootstrap-redhat.yml
-  when: '''ID="rhel"'' in os_release.stdout_lines'
-
-- name: Bootstrap Clear Linux
-  include_tasks: bootstrap-clearlinux.yml
-  when: '''ID=clear-linux-os'' in os_release.stdout_lines'
-
-# Fedora CoreOS
-- name: Bootstrap Fedora CoreOS
-  include_tasks: bootstrap-fedora-coreos.yml
-  when:
-    - '''ID=fedora'' in os_release.stdout_lines'
-    - '''VARIANT_ID=coreos'' in os_release.stdout_lines'
-
-- name: Bootstrap Flatcar
-  include_tasks: bootstrap-flatcar.yml
-  when: '''ID=flatcar'' in os_release.stdout_lines'
-
-- name: Bootstrap Debian
-  include_tasks: bootstrap-debian.yml
-  when: '''ID=debian'' in os_release.stdout_lines or ''ID=ubuntu'' in os_release.stdout_lines'
-
-# Fedora "classic"
-- name: Boostrap Fedora
-  include_tasks: bootstrap-fedora.yml
-  when:
-    - '''ID=fedora'' in os_release.stdout_lines'
-    - '''VARIANT_ID=coreos'' not in os_release.stdout_lines'
-
-- name: Bootstrap OpenSUSE
-  include_tasks: bootstrap-opensuse.yml
-  when: '''ID="opensuse-leap"'' in os_release.stdout_lines or ''ID="opensuse-tumbleweed"'' in os_release.stdout_lines'
 
 - name: Create remote_tmp for it is used by another module
   file:
@@ -54,9 +36,7 @@
     state: directory
     mode: 0700
 
-# Workaround for https://github.com/ansible/ansible/issues/42726
-# (1/3)
-- name: Gather host facts to get ansible_os_family
+- name: Gather facts
   setup:
     gather_subset: '!all'
     filter: ansible_*
@@ -64,39 +44,12 @@
 - name: Assign inventory name to unconfigured hostnames (non-CoreOS, non-Flatcar, Suse and ClearLinux, non-Fedora)
   hostname:
     name: "{{ inventory_hostname }}"
-  when:
-    - override_system_hostname
-    - ansible_os_family not in ['Suse', 'Flatcar', 'Flatcar Container Linux by Kinvolk', 'ClearLinux']
-    - not ansible_distribution == "Fedora"
-    - not is_fedora_coreos
-
-# (2/3)
-- name: Assign inventory name to unconfigured hostnames (CoreOS, Flatcar, Suse, ClearLinux and Fedora only)
-  command: "hostnamectl set-hostname {{ inventory_hostname }}"
-  register: hostname_changed
-  become: true
-  changed_when: false
-  when: >
-    override_system_hostname
-    and (ansible_os_family in ['Suse', 'Flatcar', 'Flatcar Container Linux by Kinvolk', 'ClearLinux']
-    or is_fedora_coreos
-    or ansible_distribution == "Fedora")
-
-# (3/3)
-- name: Update hostname fact (CoreOS, Flatcar, Suse, ClearLinux and Fedora only)
-  setup:
-    gather_subset: '!all'
-    filter: ansible_hostname
-  when: >
-    override_system_hostname
-    and (ansible_os_family in ['Suse', 'Flatcar', 'Flatcar Container Linux by Kinvolk', 'ClearLinux']
-    or is_fedora_coreos
-    or ansible_distribution == "Fedora")
+  when: override_system_hostname
 
 - name: Install ceph-commmon package
   package:
     name:
-      - ceph-common
+    - ceph-common
     state: present
   when: rbd_provisioner_enabled | default(false)
 
diff --git a/roles/bootstrap-os/tasks/opensuse-leap.yml b/roles/bootstrap-os/tasks/opensuse-leap.yml
new file mode 120000
index 000000000..389442aed
--- /dev/null
+++ b/roles/bootstrap-os/tasks/opensuse-leap.yml
@@ -0,0 +1 @@
+opensuse.yml
\ No newline at end of file
diff --git a/roles/bootstrap-os/tasks/opensuse-tumbleweed.yml b/roles/bootstrap-os/tasks/opensuse-tumbleweed.yml
new file mode 120000
index 000000000..389442aed
--- /dev/null
+++ b/roles/bootstrap-os/tasks/opensuse-tumbleweed.yml
@@ -0,0 +1 @@
+opensuse.yml
\ No newline at end of file
diff --git a/roles/bootstrap-os/tasks/bootstrap-opensuse.yml b/roles/bootstrap-os/tasks/opensuse.yml
similarity index 100%
rename from roles/bootstrap-os/tasks/bootstrap-opensuse.yml
rename to roles/bootstrap-os/tasks/opensuse.yml
diff --git a/roles/bootstrap-os/tasks/bootstrap-redhat.yml b/roles/bootstrap-os/tasks/redhat.yml
similarity index 100%
rename from roles/bootstrap-os/tasks/bootstrap-redhat.yml
rename to roles/bootstrap-os/tasks/redhat.yml
diff --git a/roles/bootstrap-os/tasks/ubuntu.yml b/roles/bootstrap-os/tasks/ubuntu.yml
new file mode 120000
index 000000000..f1a5a89f1
--- /dev/null
+++ b/roles/bootstrap-os/tasks/ubuntu.yml
@@ -0,0 +1 @@
+debian.yml
\ No newline at end of file
diff --git a/roles/bootstrap-os/vars/fedora-coreos.yml b/roles/bootstrap-os/vars/fedora-coreos.yml
new file mode 100644
index 000000000..e0bb069f9
--- /dev/null
+++ b/roles/bootstrap-os/vars/fedora-coreos.yml
@@ -0,0 +1,2 @@
+---
+is_fedora_coreos: True
diff --git a/roles/bootstrap-os/vars/flatcar.yml b/roles/bootstrap-os/vars/flatcar.yml
new file mode 100644
index 000000000..f18bec5bb
--- /dev/null
+++ b/roles/bootstrap-os/vars/flatcar.yml
@@ -0,0 +1,2 @@
+---
+bin_dir: "/opt/bin"
-- 
GitLab