Clean-ups for Molecule

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

Change-Id: Ic279d40d7513003ede62633f16739f4df7946b38
This commit is contained in:
Mohammed Naser 2022-04-29 21:59:05 -04:00
parent e28e13cd4b
commit 1881aad9e3
6 changed files with 40 additions and 68 deletions

View File

@ -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 }}"

View File

@ -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 }}"

View File

@ -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 }}"

View File

@ -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.

View File

@ -1,2 +1 @@
molecule_executable: molecule
molecule_venv_path: '{{ ansible_user_dir }}/.local/molecule'
molecule_version: 3.6.1

View File

@ -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