redhat-cop / ee_utilities

This ansible collection includes a number of roles and tools which can be useful for managing Ansible Execution Environments.
https://galaxy.ansible.com/infra/ee_utilities
GNU General Public License v3.0
57 stars 34 forks source link

Allow Specific Versions of Packages from `pip freeze` in venv #13

Open jandiorio opened 2 years ago

jandiorio commented 2 years ago

The current solution compares the package names in the venv to the base image only resulting in the derived image losing the version specified in the venv.

The assumption is that most packages with specific versions required are packages in addition to what's installed by the base Ansible requirements in support of other modules, roles, and collections.

  1. Gather pip freeze from the base image and each venv
  2. Split the output on == in each line and build a new dictionary
  3. Loop over venv keys (package name)
  4. If venv package name is not in base package keys() add to new var (packages_to_install)

Essentially, perform the comparison at a name level, but install a specific version if the package doesn't exist in the base already.

Sample Solution Code


    - name: VENV PACKAGES AND VERSIONS SPLIT
      ansible.builtin.set_fact:
        venv_package_details: "{{ venv_package_details | default({}) | combine({item.split('==')[0]: item.split('==')[1]}) }}"
      loop: "{{ venv_packages }}"

    - name: BASE PACKAGES AND VERSIONS SPLIT
      ansible.builtin.set_fact:
        base_package_details: "{{ base_package_details | default({}) | combine({item.split('==')[0]: item.split('==')[1]}) }}"
      loop: "{{ base_packages }}"

    - name: CREATE NEW WITH VERSION
      ansible.builtin.set_fact:
        packages_to_install: "{{ packages_to_install | default([]) + [item.key + '==' + item.value] }}"
      loop: "{{ venv_package_details | dict2items }}"
      when:
        - item.key not in base_package_details.keys()
        - not item.key is search('ansible')

...working on integrating similar code into the existing role and PR...

sean-m-sullivan commented 2 years ago

I've made some changes to the role, but this would still be useful, Even a partial solution WIP and we can likely figure out a solution.