From 9001a8794f19053f52a807cb59ced87357873e9e Mon Sep 17 00:00:00 2001 From: kiorky Date: Wed, 31 Jan 2018 14:35:06 +0100 Subject: [PATCH] FIX: multiple nested tasks include levels (#35165) * Test for include_tasks & include_role bug Related to ansible/ansible:#21890 * Fix nested include_tasks called from role This fixes the path of included files when you want to call include_task inside a role's task file and this role is itself called from multiple level of playbook include_tasks Related to ansible/ansible:#21890 This fixes ansible/ansible:#35109 --- lib/ansible/playbook/included_file.py | 11 ++++++++++- test/integration/targets/include_import/nested.yml | 6 ++++++ .../include_import/nestedtasks/nested/nested.yml | 2 ++ .../nested/nested/nested_dep_role2/defaults/main.yml | 3 +++ .../nested/nested/nested_dep_role2/meta/main.yml | 2 ++ .../nested/nested/nested_dep_role2/tasks/main.yml | 2 ++ .../nested/nested/nested_dep_role2/tasks/rund.yml | 2 ++ .../nested/nested/nested_dep_role2/vars/main.yml | 2 ++ .../nested/nested/nested_dep_role2a/defaults/main.yml | 3 +++ .../nested/nested/nested_dep_role2a/meta/main.yml | 2 ++ .../nested/nested/nested_dep_role2a/tasks/main.yml | 2 ++ .../nested/nested/nested_dep_role2a/tasks/rune.yml | 2 ++ .../nested/nested/nested_dep_role2a/vars/main.yml | 2 ++ .../nested/nested/nested_dep_role2b/defaults/main.yml | 3 +++ .../nested/nested/nested_dep_role2b/meta/main.yml | 1 + .../nested/nested/nested_dep_role2b/tasks/main.yml | 2 ++ .../nested/nested/nested_dep_role2b/tasks/runf.yml | 2 ++ .../nested/nested/nested_dep_role2b/vars/main.yml | 2 ++ .../roles/nested/nested_dep_role/defaults/main.yml | 3 +++ .../roles/nested/nested_dep_role/meta/main.yml | 2 ++ .../roles/nested/nested_dep_role/tasks/main.yml | 2 ++ .../roles/nested/nested_dep_role/tasks/runc.yml | 4 ++++ .../roles/nested/nested_dep_role/vars/main.yml | 2 ++ .../roles/nested_include_task/meta/main.yml | 2 ++ .../roles/nested_include_task/tasks/main.yml | 2 ++ .../roles/nested_include_task/tasks/runa.yml | 3 +++ test/integration/targets/include_import/runme.sh | 5 +++++ .../targets/include_import/tasks/nested/nested.yml | 2 ++ 28 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/integration/targets/include_import/nested.yml create mode 100644 test/integration/targets/include_import/nestedtasks/nested/nested.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/defaults/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/meta/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/tasks/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/tasks/rund.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/vars/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/defaults/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/meta/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/tasks/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/tasks/rune.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/vars/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/defaults/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/meta/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/tasks/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/tasks/runf.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/vars/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested_dep_role/defaults/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested_dep_role/meta/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested_dep_role/tasks/main.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested_dep_role/tasks/runc.yml create mode 100644 test/integration/targets/include_import/roles/nested/nested_dep_role/vars/main.yml create mode 100644 test/integration/targets/include_import/roles/nested_include_task/meta/main.yml create mode 100644 test/integration/targets/include_import/roles/nested_include_task/tasks/main.yml create mode 100644 test/integration/targets/include_import/roles/nested_include_task/tasks/runa.yml create mode 100644 test/integration/targets/include_import/tasks/nested/nested.yml diff --git a/lib/ansible/playbook/included_file.py b/lib/ansible/playbook/included_file.py index 6bcd631ce2..f565a4aa0c 100644 --- a/lib/ansible/playbook/included_file.py +++ b/lib/ansible/playbook/included_file.py @@ -109,7 +109,16 @@ class IncludedFile: include_target = templar.template(include_result['include']) if original_task._role: new_basedir = os.path.join(original_task._role._role_path, 'tasks', cumulative_path) - include_file = loader.path_dwim_relative(new_basedir, 'tasks', include_target) + candidates = [loader.path_dwim_relative(original_task._role._role_path, 'tasks', include_target), + loader.path_dwim_relative(new_basedir, 'tasks', include_target)] + for include_file in candidates: + try: + # may throw OSError + os.stat(include_file) + # or select the task file if it exists + break + except OSError: + pass else: include_file = loader.path_dwim_relative(loader.get_basedir(), cumulative_path, include_target) diff --git a/test/integration/targets/include_import/nested.yml b/test/integration/targets/include_import/nested.yml new file mode 100644 index 0000000000..9d1d25ebec --- /dev/null +++ b/test/integration/targets/include_import/nested.yml @@ -0,0 +1,6 @@ +- name: >- + verify that multiple level of nested statements and + include+meta doesnt mess included files mecanisms + hosts: testhost + tasks: + - include_tasks: ./tasks/nested/nested.yml diff --git a/test/integration/targets/include_import/nestedtasks/nested/nested.yml b/test/integration/targets/include_import/nestedtasks/nested/nested.yml new file mode 100644 index 0000000000..95fe2660df --- /dev/null +++ b/test/integration/targets/include_import/nestedtasks/nested/nested.yml @@ -0,0 +1,2 @@ +--- +- include_role: {name: nested_include_task} diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/defaults/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/defaults/main.yml new file mode 100644 index 0000000000..aba24bbe92 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/defaults/main.yml @@ -0,0 +1,3 @@ +--- +testnesteddep2_defvar1: foobar +testnesteddep2_varvar1: foobar diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/meta/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/meta/main.yml new file mode 100644 index 0000000000..31afcaa9a0 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- role: nested/nested/nested_dep_role2a diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/tasks/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/tasks/main.yml new file mode 100644 index 0000000000..1f2ee7f073 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/tasks/main.yml @@ -0,0 +1,2 @@ +--- +- include_tasks: ./rund.yml diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/tasks/rund.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/tasks/rund.yml new file mode 100644 index 0000000000..523e579d45 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/tasks/rund.yml @@ -0,0 +1,2 @@ +--- +- shell: echo from deprole2a diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/vars/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/vars/main.yml new file mode 100644 index 0000000000..c89b697346 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2/vars/main.yml @@ -0,0 +1,2 @@ +--- +testnesteddep2_varvar1: muche diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/defaults/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/defaults/main.yml new file mode 100644 index 0000000000..aba24bbe92 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/defaults/main.yml @@ -0,0 +1,3 @@ +--- +testnesteddep2_defvar1: foobar +testnesteddep2_varvar1: foobar diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/meta/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/meta/main.yml new file mode 100644 index 0000000000..6fc8ab0cf0 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- role: nested/nested/nested_dep_role2b diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/tasks/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/tasks/main.yml new file mode 100644 index 0000000000..729582c4f3 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/tasks/main.yml @@ -0,0 +1,2 @@ +--- +- include_tasks: ./rune.yml diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/tasks/rune.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/tasks/rune.yml new file mode 100644 index 0000000000..e77882b29d --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/tasks/rune.yml @@ -0,0 +1,2 @@ +--- +- shell: echo from deprole2 diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/vars/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/vars/main.yml new file mode 100644 index 0000000000..c89b697346 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2a/vars/main.yml @@ -0,0 +1,2 @@ +--- +testnesteddep2_varvar1: muche diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/defaults/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/defaults/main.yml new file mode 100644 index 0000000000..aba24bbe92 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/defaults/main.yml @@ -0,0 +1,3 @@ +--- +testnesteddep2_defvar1: foobar +testnesteddep2_varvar1: foobar diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/meta/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/meta/main.yml new file mode 100644 index 0000000000..32cf5dda7e --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/meta/main.yml @@ -0,0 +1 @@ +dependencies: [] diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/tasks/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/tasks/main.yml new file mode 100644 index 0000000000..5fbb04fe40 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/tasks/main.yml @@ -0,0 +1,2 @@ +--- +- include_tasks: ./runf.yml diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/tasks/runf.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/tasks/runf.yml new file mode 100644 index 0000000000..694005fd36 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/tasks/runf.yml @@ -0,0 +1,2 @@ +--- +- shell: echo from deprole2b diff --git a/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/vars/main.yml b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/vars/main.yml new file mode 100644 index 0000000000..c89b697346 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested/nested_dep_role2b/vars/main.yml @@ -0,0 +1,2 @@ +--- +testnesteddep2_varvar1: muche diff --git a/test/integration/targets/include_import/roles/nested/nested_dep_role/defaults/main.yml b/test/integration/targets/include_import/roles/nested/nested_dep_role/defaults/main.yml new file mode 100644 index 0000000000..536745eec0 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested_dep_role/defaults/main.yml @@ -0,0 +1,3 @@ +--- +testnesteddep_defvar1: foobar +testnesteddep_varvar1: foobar diff --git a/test/integration/targets/include_import/roles/nested/nested_dep_role/meta/main.yml b/test/integration/targets/include_import/roles/nested/nested_dep_role/meta/main.yml new file mode 100644 index 0000000000..23d65c7ef4 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested_dep_role/meta/main.yml @@ -0,0 +1,2 @@ +--- +dependencies: [] diff --git a/test/integration/targets/include_import/roles/nested/nested_dep_role/tasks/main.yml b/test/integration/targets/include_import/roles/nested/nested_dep_role/tasks/main.yml new file mode 100644 index 0000000000..d86604b4ed --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested_dep_role/tasks/main.yml @@ -0,0 +1,2 @@ +--- +- include_tasks: ./runc.yml diff --git a/test/integration/targets/include_import/roles/nested/nested_dep_role/tasks/runc.yml b/test/integration/targets/include_import/roles/nested/nested_dep_role/tasks/runc.yml new file mode 100644 index 0000000000..76682f5412 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested_dep_role/tasks/runc.yml @@ -0,0 +1,4 @@ +--- +- debug: + msg: from test_nested_dep_role +- include_role: {name: nested/nested/nested_dep_role2} diff --git a/test/integration/targets/include_import/roles/nested/nested_dep_role/vars/main.yml b/test/integration/targets/include_import/roles/nested/nested_dep_role/vars/main.yml new file mode 100644 index 0000000000..b80b5de38f --- /dev/null +++ b/test/integration/targets/include_import/roles/nested/nested_dep_role/vars/main.yml @@ -0,0 +1,2 @@ +--- +testnesteddep_varvar1: muche diff --git a/test/integration/targets/include_import/roles/nested_include_task/meta/main.yml b/test/integration/targets/include_import/roles/nested_include_task/meta/main.yml new file mode 100644 index 0000000000..9410b7d296 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested_include_task/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: +- role: nested/nested_dep_role diff --git a/test/integration/targets/include_import/roles/nested_include_task/tasks/main.yml b/test/integration/targets/include_import/roles/nested_include_task/tasks/main.yml new file mode 100644 index 0000000000..15a8e9faa2 --- /dev/null +++ b/test/integration/targets/include_import/roles/nested_include_task/tasks/main.yml @@ -0,0 +1,2 @@ +--- +- include_tasks: ./runa.yml diff --git a/test/integration/targets/include_import/roles/nested_include_task/tasks/runa.yml b/test/integration/targets/include_import/roles/nested_include_task/tasks/runa.yml new file mode 100644 index 0000000000..643fdd2a9d --- /dev/null +++ b/test/integration/targets/include_import/roles/nested_include_task/tasks/runa.yml @@ -0,0 +1,3 @@ +--- +- debug: + msg: from nested_include_task diff --git a/test/integration/targets/include_import/runme.sh b/test/integration/targets/include_import/runme.sh index fbe961c574..8b8ce28e23 100755 --- a/test/integration/targets/include_import/runme.sh +++ b/test/integration/targets/include_import/runme.sh @@ -36,3 +36,8 @@ ANSIBLE_STRATEGY='free' ansible-playbook role/test_include_role.yml -i ../../inv ## Recursion # https://github.com/ansible/ansible/issues/23609 ANSIBLE_STRATEGY='linear' ansible-playbook test_recursion.yml -i ../../inventory "$@" --skip-tags never + +## Nested tasks +# https://github.com/ansible/ansible/issues/34782 +ANSIBLE_STRATEGY='linear' ansible-playbook nested.yml -i ../../inventory "$@" --skip-tags never +ANSIBLE_STRATEGY='free' ansible-playbook nested.yml -i ../../inventory "$@" --skip-tags never diff --git a/test/integration/targets/include_import/tasks/nested/nested.yml b/test/integration/targets/include_import/tasks/nested/nested.yml new file mode 100644 index 0000000000..0bfcdeefaa --- /dev/null +++ b/test/integration/targets/include_import/tasks/nested/nested.yml @@ -0,0 +1,2 @@ +--- +- include_tasks: ../../nestedtasks/nested/nested.yml