diff --git a/changelogs/fragments/12064-cargo-latest-version-regex.yml b/changelogs/fragments/12064-cargo-latest-version-regex.yml new file mode 100644 index 0000000000..cfc548224f --- /dev/null +++ b/changelogs/fragments/12064-cargo-latest-version-regex.yml @@ -0,0 +1,4 @@ +bugfixes: + - "cargo - fix ``state=latest`` always reporting ``changed`` due to greedy regex capturing description text instead of version string + (https://github.com/ansible-collections/community.general/issues/8949, + https://github.com/ansible-collections/community.general/pull/12064)." diff --git a/plugins/modules/cargo.py b/plugins/modules/cargo.py index 68c8ad4f55..98c885908c 100644 --- a/plugins/modules/cargo.py +++ b/plugins/modules/cargo.py @@ -196,7 +196,7 @@ class Cargo: cmd = ["search", name, "--limit", "1"] data, dummy = self._exec(cmd, True, False, False) - match = re.search(r'"(.+)"', data) + match = re.search(r"^" + re.escape(name) + r'\s*=\s*"([^"]+)"', data, re.MULTILINE) if not match: self.module.fail_json(msg=f"No published version for package {name} found") return match.group(1)