diff --git a/changelogs/fragments/83070-add-support-for-apt-patterns.yml b/changelogs/fragments/83070-add-support-for-apt-patterns.yml new file mode 100644 index 00000000000000..42aae64b9feed3 --- /dev/null +++ b/changelogs/fragments/83070-add-support-for-apt-patterns.yml @@ -0,0 +1,2 @@ +minor_changes: + - apt - add support for apt-patterns (https://github.com/ansible/ansible/pull/83070) diff --git a/lib/ansible/modules/apt.py b/lib/ansible/modules/apt.py index cc5edd0f6d958f..3153c08fa81b89 100644 --- a/lib/ansible/modules/apt.py +++ b/lib/ansible/modules/apt.py @@ -634,8 +634,11 @@ def expand_pkgspec_from_fnmatches(m, pkgspec, cache): pkgname_pattern, version_cmp, version = package_split(pkgspec_pattern) + if pkgname_pattern.startswith(('?', '~', '(?', '(~')): + # this is an apt-pattern (https://manpages.debian.org/bookworm/apt/apt-patterns.7.en.html) + new_pkgspec.append(pkgspec_pattern) # note that none of these chars is allowed in a (debian) pkgname - if frozenset('*?[]!').intersection(pkgname_pattern): + elif frozenset('*?[]!').intersection(pkgname_pattern): # handle multiarch pkgnames, the idea is that "apt*" should # only select native packages. But "apt*:i386" should still work if ":" not in pkgname_pattern: @@ -952,7 +955,7 @@ def remove(m, pkgspec, cache, purge=False, force=False, for package in pkgspec: name, version_cmp, version = package_split(package) installed, installed_version, upgradable, has_files = package_status(m, name, version_cmp, version, None, cache, state='remove') - if installed_version or (has_files and purge): + if installed_version or (has_files and purge) or package.startswith(('?', '~', '(?', '(~')): pkg_list.append("'%s'" % package) packages = ' '.join(pkg_list) diff --git a/test/units/modules/test_apt.py b/test/units/modules/test_apt.py index d207320c82ad64..18b16c80d39f14 100644 --- a/test/units/modules/test_apt.py +++ b/test/units/modules/test_apt.py @@ -38,6 +38,11 @@ ["apt", "apt-utils"], id="pkgname-expands", ), + pytest.param( + ["?config-files"], + ["?config-files"], + id="apt-pattern", + ), ], ) def test_expand_pkgspec_from_fnmatches(test_input, expected):