From a2b5af86dd67920ce350e2a6f292572a9f3762a4 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Mon, 10 Feb 2020 17:13:22 -0500 Subject: [PATCH] [stable-2.9] include_vars - fix stack trace when run ad-hoc with dirs parameter (#66581) (#66910) Add integration test There are a number of other parameters that result in stack traces as well when this module is used ad-hoc. I'm not sure if we're interested in fixing them all since this module isn't meant to be run ad-hoc. (cherry picked from commit cc2376b782) Co-authored-by: Sam Doran --- .../fragments/include_vars-ad-hoc-stack-trace-fix.yaml | 2 ++ lib/ansible/plugins/action/include_vars.py | 9 +++++---- test/integration/targets/include_vars-ad-hoc/aliases | 1 + test/integration/targets/include_vars-ad-hoc/dir/inc.yml | 1 + test/integration/targets/include_vars-ad-hoc/runme.sh | 6 ++++++ 5 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/include_vars-ad-hoc-stack-trace-fix.yaml create mode 100644 test/integration/targets/include_vars-ad-hoc/aliases create mode 100644 test/integration/targets/include_vars-ad-hoc/dir/inc.yml create mode 100755 test/integration/targets/include_vars-ad-hoc/runme.sh diff --git a/changelogs/fragments/include_vars-ad-hoc-stack-trace-fix.yaml b/changelogs/fragments/include_vars-ad-hoc-stack-trace-fix.yaml new file mode 100644 index 00000000000..b77e2c11250 --- /dev/null +++ b/changelogs/fragments/include_vars-ad-hoc-stack-trace-fix.yaml @@ -0,0 +1,2 @@ +bugfixes: + - include_vars - fix stack trace when passing ``dirs`` in an ad-hoc command (https://github.com/ansible/ansible/issues/62633) diff --git a/lib/ansible/plugins/action/include_vars.py b/lib/ansible/plugins/action/include_vars.py index 25cf9dccdbf..07234537ac8 100644 --- a/lib/ansible/plugins/action/include_vars.py +++ b/lib/ansible/plugins/action/include_vars.py @@ -158,10 +158,11 @@ class ActionModule(ActionBase): ) self.source_dir = path_to_use else: - current_dir = ( - "/".join(self._task._ds._data_source.split('/')[:-1]) - ) - self.source_dir = path.join(current_dir, self.source_dir) + if hasattr(self._task._ds, '_data_source'): + current_dir = ( + "/".join(self._task._ds._data_source.split('/')[:-1]) + ) + self.source_dir = path.join(current_dir, self.source_dir) def _traverse_dir_depth(self): """ Recursively iterate over a directory and sort the files in diff --git a/test/integration/targets/include_vars-ad-hoc/aliases b/test/integration/targets/include_vars-ad-hoc/aliases new file mode 100644 index 00000000000..765b70da796 --- /dev/null +++ b/test/integration/targets/include_vars-ad-hoc/aliases @@ -0,0 +1 @@ +shippable/posix/group2 diff --git a/test/integration/targets/include_vars-ad-hoc/dir/inc.yml b/test/integration/targets/include_vars-ad-hoc/dir/inc.yml new file mode 100644 index 00000000000..c1d24c84a0d --- /dev/null +++ b/test/integration/targets/include_vars-ad-hoc/dir/inc.yml @@ -0,0 +1 @@ +porter: cable diff --git a/test/integration/targets/include_vars-ad-hoc/runme.sh b/test/integration/targets/include_vars-ad-hoc/runme.sh new file mode 100755 index 00000000000..51b68d21341 --- /dev/null +++ b/test/integration/targets/include_vars-ad-hoc/runme.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +set -eux + +ansible testhost -i ../../inventory -m include_vars -a 'dir/inc.yml' "$@" +ansible testhost -i ../../inventory -m include_vars -a 'dir=dir' "$@"