Skip to content

Commit

Permalink
Don't hardcode the dnf module, dynamically select one (#83183)
Browse files Browse the repository at this point in the history
  • Loading branch information
sivel committed May 2, 2024
1 parent 2b65166 commit dc6b77b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 25 deletions.
40 changes: 40 additions & 0 deletions test/integration/targets/dnf/filter_plugins/dnf_module_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import annotations

from collections import Counter


def parse_module_list(stdout):
lines = stdout.splitlines()
name_offset = 0
empty_offset = -1
modules = []
for i, line in enumerate(lines):
if line.startswith('Name '):
name_offset = i + 1
if not line.strip():
empty_offset = i
for line in lines[name_offset:empty_offset]:
cols = line.split()[:3]
modules.append({
'name': cols[0],
'version': cols[1],
'profile': cols[2].rstrip(','), # Just the first profile
})
return modules


def get_first_single_version_module(stdout):
modules = parse_module_list(stdout)
name = Counter([m['name'] for m in modules]).most_common()[-1][0]
module, = [m for m in modules if m['name'] == name]
return module


class FilterModule:
def filters(self):
return {
'get_first_single_version_module': get_first_single_version_module,
}
25 changes: 21 additions & 4 deletions test/integration/targets/dnf/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,30 @@
- ansible_distribution == 'Fedora'
- ansible_distribution_major_version is version('23', '>=')

- include_tasks: modularity.yml
when:
- when:
- (ansible_distribution == 'Fedora' and ansible_distribution_major_version is version('39', '<')) or
(ansible_distribution in ['RedHat', 'CentOS'] and ansible_distribution_major_version is version('8', '>='))
- not dnf5|default(false)
tags:
- dnf_modularity
block:
# FUTURE - look at including AppStream support in our local repo
- name: list modules
command: dnf module list -q
register: module_list

# A module that only has a single version
- name: Find a module that meets our testing needs
set_fact:
astream_name: '@{{ module.name }}:{{ module.version }}/{{ module.profile }}'
astream_name_no_stream: '@{{ module.name }}/{{ module.profile }}'
vars:
module: '{{ module_list.stdout|get_first_single_version_module }}'

- include_tasks: modularity.yml
tags:
- dnf_modularity
rescue:
# Just in case something crazy happens when listing or parsing modules
- meta: noop

- include_tasks: logging.yml
when: (ansible_distribution == 'Fedora' and ansible_distribution_major_version is version('31', '>=')) or
Expand Down
9 changes: 0 additions & 9 deletions test/integration/targets/dnf/tasks/modularity.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
# FUTURE - look at including AppStream support in our local repo
- name: Include distribution specific variables
include_vars: "{{ item }}"
with_first_found:
- files:
- "{{ ansible_facts.distribution }}-{{ ansible_facts.distribution_major_version }}.yml"
- "{{ ansible_facts.distribution }}.yml"
paths: ../vars

- name: install "{{ astream_name }}" module
dnf:
name: "{{ astream_name }}"
Expand Down
2 changes: 0 additions & 2 deletions test/integration/targets/dnf/vars/CentOS.yml

This file was deleted.

6 changes: 0 additions & 6 deletions test/integration/targets/dnf/vars/Fedora.yml

This file was deleted.

2 changes: 0 additions & 2 deletions test/integration/targets/dnf/vars/RedHat-9.yml

This file was deleted.

2 changes: 0 additions & 2 deletions test/integration/targets/dnf/vars/RedHat.yml

This file was deleted.

0 comments on commit dc6b77b

Please sign in to comment.