diff --git a/changelogs/fragments/6783-6837-rhsm_repository-internal-refactor.yml b/changelogs/fragments/6783-6837-rhsm_repository-internal-refactor.yml new file mode 100644 index 0000000000..fe7b9510c6 --- /dev/null +++ b/changelogs/fragments/6783-6837-rhsm_repository-internal-refactor.yml @@ -0,0 +1,8 @@ +minor_changes: + - | + rhsm_repository - the interaction with ``subscription-manager`` was + refactored by grouping things together, removing unused bits, and hardening + the way it is run; also, the parsing of ``subscription-manager repos --list`` + was improved and made slightly faster; no behaviour change is expected + (https://github.com/ansible-collections/community.general/pull/6783, + https://github.com/ansible-collections/community.general/pull/6837). diff --git a/changelogs/fragments/6783-rhsm_repository-internal-refactor.yml b/changelogs/fragments/6783-rhsm_repository-internal-refactor.yml deleted file mode 100644 index 7b76118d68..0000000000 --- a/changelogs/fragments/6783-rhsm_repository-internal-refactor.yml +++ /dev/null @@ -1,6 +0,0 @@ -minor_changes: - - | - rhsm_repository - the interaction with ``subscription-manager`` was - refactored by grouping things together, removing unused bits, and hardening - the way it is run; no behaviour change is expected - (https://github.com/ansible-collections/community.general/pull/6783). diff --git a/plugins/modules/rhsm_repository.py b/plugins/modules/rhsm_repository.py index 2a7d8acf2a..e58389102e 100644 --- a/plugins/modules/rhsm_repository.py +++ b/plugins/modules/rhsm_repository.py @@ -90,7 +90,6 @@ repositories: type: list ''' -import re import os from fnmatch import fnmatch from copy import deepcopy @@ -129,15 +128,6 @@ class Rhsm(object): """ rc, out, err = self.run_repos(['--list']) - skip_lines = [ - '+----------------------------------------------------------+', - ' Available Repositories in /etc/yum.repos.d/redhat.repo' - ] - repo_id_re = re.compile(r'Repo ID:\s+(.*)') - repo_name_re = re.compile(r'Repo Name:\s+(.*)') - repo_url_re = re.compile(r'Repo URL:\s+(.*)') - repo_enabled_re = re.compile(r'Enabled:\s+(.*)') - repo_id = '' repo_name = '' repo_url = '' @@ -145,27 +135,27 @@ class Rhsm(object): repo_result = [] for line in out.splitlines(): - if line == '' or line in skip_lines: + # ignore lines that are: + # - empty + # - "+---------[...]" -- i.e. header + # - " Available Repositories [...]" -- i.e. header + if line == '' or line[0] == '+' or line[0] == ' ': continue - repo_id_match = repo_id_re.match(line) - if repo_id_match: - repo_id = repo_id_match.group(1) + if line.startswith('Repo ID: '): + repo_id = line[9:].lstrip() continue - repo_name_match = repo_name_re.match(line) - if repo_name_match: - repo_name = repo_name_match.group(1) + if line.startswith('Repo Name: '): + repo_name = line[11:].lstrip() continue - repo_url_match = repo_url_re.match(line) - if repo_url_match: - repo_url = repo_url_match.group(1) + if line.startswith('Repo URL: '): + repo_url = line[10:].lstrip() continue - repo_enabled_match = repo_enabled_re.match(line) - if repo_enabled_match: - repo_enabled = repo_enabled_match.group(1) + if line.startswith('Enabled: '): + repo_enabled = line[9:].lstrip() repo = { "id": repo_id,