From 48b20894b3e0d895abaae3ec98d8682acdcbf993 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 15 Mar 2023 07:28:40 +0100 Subject: [PATCH] [PR #6129/df34569f backport][stable-6] Updating yarn.list to not fail when when warnings are emitted (#6189) Updating yarn.list to not fail when when warnings are emitted (#6129) * Updating yarn.list to not fail when when warnings are emitted * Adding changelog fragment * Adding _process_yarn_error function * - Adding back changes to the changelog fragment - Fixing formatting * Fix trailing whitespace * Update plugins/modules/yarn.py Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein (cherry picked from commit df34569fadca7f3b554f612d7480b1eb50919704) Co-authored-by: John Daly --- .../fragments/6127-yarn-ignore-warnings.yml | 2 ++ plugins/modules/yarn.py | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/6127-yarn-ignore-warnings.yml diff --git a/changelogs/fragments/6127-yarn-ignore-warnings.yml b/changelogs/fragments/6127-yarn-ignore-warnings.yml new file mode 100644 index 0000000000..f094838edb --- /dev/null +++ b/changelogs/fragments/6127-yarn-ignore-warnings.yml @@ -0,0 +1,2 @@ +bugfixes: + - yarn - fixes bug where yarn module tasks would fail when warnings were emitted from Yarn. The ``yarn.list`` method was not filtering out warnings (https://github.com/ansible-collections/community.general/issues/6127). diff --git a/plugins/modules/yarn.py b/plugins/modules/yarn.py index 02cfc4f8be..c278951d5e 100644 --- a/plugins/modules/yarn.py +++ b/plugins/modules/yarn.py @@ -226,6 +226,15 @@ class Yarn(object): return None, None + def _process_yarn_error(self, err): + try: + # We need to filter for errors, since Yarn warnings are included in stderr + for line in err.splitlines(): + if json.loads(line)['type'] == 'error': + self.module.fail_json(msg=err) + except Exception: + self.module.fail_json(msg="Unexpected stderr output from Yarn: %s" % err, stderr=err) + def list(self): cmd = ['list', '--depth=0', '--json'] @@ -240,8 +249,7 @@ class Yarn(object): # because it only only lists binaries, but `yarn global add` can install libraries too. result, error = self._exec(cmd, run_in_check_mode=True, check_rc=False, unsupported_with_global=True) - if error: - self.module.fail_json(msg=error) + self._process_yarn_error(error) for json_line in result.strip().split('\n'): data = json.loads(json_line) @@ -279,9 +287,7 @@ class Yarn(object): cmd_result, err = self._exec(['outdated', '--json'], True, False, unsupported_with_global=True) # the package.json in the global dir is missing a license field, so warnings are expected on stderr - for line in err.splitlines(): - if json.loads(line)['type'] == 'error': - self.module.fail_json(msg=err) + self._process_yarn_error(err) if not cmd_result: return outdated