From 1881aad9e3cb0cae5979bb06094faa171dad6fe5 Mon Sep 17 00:00:00 2001
From: Mohammed Naser <mnaser@vexxhost.com>
Date: Fri, 29 Apr 2022 21:59:05 -0400
Subject: [PATCH] Clean-ups for Molecule

- Add fetch-output
- Refresh inventory after
- Install into system globally

Change-Id: Ic279d40d7513003ede62633f16739f4df7946b38
---
 playbooks/molecule/post-run.yaml         | 11 ++++-
 playbooks/molecule/pre-run.yaml          | 20 ++++++++--
 playbooks/molecule/run.yaml              |  9 ++---
 roles/ensure-molecule/README.rst         | 14 -------
 roles/ensure-molecule/defaults/main.yaml |  3 +-
 roles/ensure-molecule/tasks/main.yaml    | 51 +++++-------------------
 6 files changed, 40 insertions(+), 68 deletions(-)

diff --git a/playbooks/molecule/post-run.yaml b/playbooks/molecule/post-run.yaml
index 614614f..da3eda2 100644
--- a/playbooks/molecule/post-run.yaml
+++ b/playbooks/molecule/post-run.yaml
@@ -1,7 +1,14 @@
 ---
+- hosts: molecule
+  roles:
+    - fetch-output
+
+# NOTE(mnaser): We refresh the inventory to drop all the hosts we've added using
+#               the add_host task to avoid any failures after we destroy.
+- meta: refresh_inventory
+
 - hosts: ubuntu-focal
   tasks:
-    - name: molecule destroy
-      shell: "{{ molecule_executable }} destroy"
+    - shell: molecule destroy
       args:
         chdir: "{{ zuul.project.src_dir }}"
diff --git a/playbooks/molecule/pre-run.yaml b/playbooks/molecule/pre-run.yaml
index b960986..3d0e7fd 100644
--- a/playbooks/molecule/pre-run.yaml
+++ b/playbooks/molecule/pre-run.yaml
@@ -8,13 +8,25 @@
 
 - hosts: ubuntu-focal
   tasks:
-    - name: molecule create
-      shell: "{{ molecule_executable }} create"
+    # TODO(mnaser): Get artifact from build job
+    - name: Build & Install collection
+      block:
+        - name: Install build-time dependencies
+          become: true
+          pip:
+            name: ['pbr', 'pyyaml']
+        - name: Generate meta.yml file
+          shell: python3 {{ zuul.project.src_dir }}/tools/generate-galaxy-yml.py
+          args:
+            chdir: "{{ zuul.project.src_dir }}"
+        - name: Install collection
+          shell: ansible-galaxy collection install -f {{ zuul.project.src_dir }}
+
+    - shell: molecule create
       args:
         chdir: "{{ zuul.project.src_dir }}"
 
-    - name: molecule prepare
-      shell: "{{ molecule_executable }} prepare"
+    - shell: molecule prepare
       args:
         chdir: "{{ zuul.project.src_dir }}"
 
diff --git a/playbooks/molecule/run.yaml b/playbooks/molecule/run.yaml
index 90936a9..b0f9858 100644
--- a/playbooks/molecule/run.yaml
+++ b/playbooks/molecule/run.yaml
@@ -1,17 +1,14 @@
 ---
 - hosts: ubuntu-focal
   tasks:
-    - name: molecule converge
-      shell: "{{ molecule_executable }} converge"
+    - shell: molecule converge
       args:
         chdir: "{{ zuul.project.src_dir }}"
 
-    - name: molecule idempotence
-      shell: "{{ molecule_executable }} idempotence"
+    - shell: molecule idempotence
       args:
         chdir: "{{ zuul.project.src_dir }}"
 
-    - name: molecule verify
-      shell: "{{ molecule_executable }} verify"
+    - shell: molecule verify
       args:
         chdir: "{{ zuul.project.src_dir }}"
diff --git a/roles/ensure-molecule/README.rst b/roles/ensure-molecule/README.rst
index e122e09..98a1510 100644
--- a/roles/ensure-molecule/README.rst
+++ b/roles/ensure-molecule/README.rst
@@ -9,17 +9,3 @@ virtual environment for the current user.
    :default: 3.6.1
 
    Molecule version to install on the host.
-
-**Output Variables**
-
-.. zuul:rolevar:: molecule_executable
-   :default: molecule
-
-   After running this role, ``molecule_executable`` will be set as the path
-   to a valid ``molecule``.
-
-   At role runtime, look for an existing ``molecule`` at this specific
-   path.  Note the default (``molecule``) effectively means to find tox in
-   the current ``$PATH``.  For example, if your base image
-   pre-installs molecule in an out-of-path environment, set this so the
-   role does not attempt to install the user version.
\ No newline at end of file
diff --git a/roles/ensure-molecule/defaults/main.yaml b/roles/ensure-molecule/defaults/main.yaml
index 7cc6309..86ae731 100644
--- a/roles/ensure-molecule/defaults/main.yaml
+++ b/roles/ensure-molecule/defaults/main.yaml
@@ -1,2 +1 @@
-molecule_executable: molecule
-molecule_venv_path: '{{ ansible_user_dir }}/.local/molecule'
\ No newline at end of file
+molecule_version: 3.6.1
\ No newline at end of file
diff --git a/roles/ensure-molecule/tasks/main.yaml b/roles/ensure-molecule/tasks/main.yaml
index 3004bf9..0e79d54 100644
--- a/roles/ensure-molecule/tasks/main.yaml
+++ b/roles/ensure-molecule/tasks/main.yaml
@@ -2,46 +2,17 @@
   include_role:
     name: ensure-pip
 
-- name: Check if molecule is installed
-  shell: |
-    command -v {{ molecule_executable }} {{ molecule_venv_path }}/bin/molecule || exit 1
-  args:
-    executable: /bin/bash
-  register: molecule_preinstalled
-  failed_when: false
+- name: Install Molecule
+  become: true
+  pip:
+    name:
+      - ansible-core
+      - molecule=={{ molecule_version }}
 
-- name: Export preinstalled molecule_executable
-  set_fact:
-    molecule_executable: '{{ molecule_preinstalled.stdout_lines[0] }}'
-    cacheable: true
-  when: molecule_preinstalled.rc == 0
-
-- name: Install molecule to local env
-  when: molecule_preinstalled.rc != 0
-  block:
-    - name: Create local venv
-      command: '{{ ensure_pip_virtualenv_command }} {{ molecule_venv_path }}'
-
-    - name: Install molecule to local venv
-      command: '{{ molecule_venv_path }}/bin/pip install -r {{ zuul.project.src_dir }}/molecule/default/requirements.txt ansible molecule=={{ molecule_version }}'
-
-    # NOTE(mnaser): molecule shells out to "ansible-config" and since we don't
-    #               have it in our PATH.
-    - name: Create symbolic link for ansible-config
-      become: true
-      file:
-        src: '{{ molecule_venv_path }}/bin/{{ item }}'
-        dest: '/usr/local/bin/{{ item }}'
-        state: link
-      loop:
-        - ansible
-        - ansible-config
-        - ansible-playbook
-
-    - name: Export installed molecule_executable path
-      set_fact:
-        molecule_executable: '{{ molecule_venv_path }}/bin/molecule'
-        cacheable: true
+- name: Install additional dependencies
+  become: true
+  pip:
+    requirements: "{{ zuul.project.src_dir }}/molecule/default/requirements.txt"
 
 - name: Output molecule version
-  command: "{{ molecule_executable }} --version"
+  command: molecule --version