From 6418f368e3d13c404cc2d53a6d5dab68f2809996 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 21 Apr 2021 17:27:41 -0400 Subject: [PATCH] Fix pb subs collapse (#74301) * fix subdir parsing for plays fixes #74283 Co-authored-by: Nikolaos Kakouros --- changelogs/fragments/coll_pb_subdir_fixes.yml | 2 ++ .../utils/collection_loader/_collection_finder.py | 6 ++++++ .../testns/testcoll/playbooks/play.yml | 4 ++++ .../testns/testcoll/playbooks/type/play.yml | 4 ++++ .../testcoll/playbooks/type/subtype/play.yml | 4 ++++ .../targets/collections/import_collection_pb.yml | 15 +++++++++++++++ .../_collection_finder.py | 6 ++++++ 7 files changed, 41 insertions(+) create mode 100644 changelogs/fragments/coll_pb_subdir_fixes.yml create mode 100644 test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/play.yml create mode 100644 test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/type/play.yml create mode 100644 test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/type/subtype/play.yml diff --git a/changelogs/fragments/coll_pb_subdir_fixes.yml b/changelogs/fragments/coll_pb_subdir_fixes.yml new file mode 100644 index 00000000000..507c6f10e89 --- /dev/null +++ b/changelogs/fragments/coll_pb_subdir_fixes.yml @@ -0,0 +1,2 @@ +bugfixes: + - playbook loaded from collection subdir now does not ignore subdirs. diff --git a/lib/ansible/utils/collection_loader/_collection_finder.py b/lib/ansible/utils/collection_loader/_collection_finder.py index f684da2ef0e..e0c3c109b9e 100644 --- a/lib/ansible/utils/collection_loader/_collection_finder.py +++ b/lib/ansible/utils/collection_loader/_collection_finder.py @@ -891,6 +891,12 @@ def _get_collection_playbook_path(playbook): if pkg: cpath = os.path.join(sys.modules[acr.n_python_collection_package_name].__file__.replace('__synthetic__', 'playbooks')) + + if acr.subdirs: + paths = [to_native(x) for x in acr.subdirs.split(u'.')] + paths.insert(0, cpath) + cpath = os.path.join(*paths) + path = os.path.join(cpath, to_native(acr.resource)) if os.path.exists(to_bytes(path)): return acr.resource, path, acr.collection diff --git a/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/play.yml b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/play.yml new file mode 100644 index 00000000000..6be246cc514 --- /dev/null +++ b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/play.yml @@ -0,0 +1,4 @@ +- hosts: localhost + gather_facts: false + tasks: + - set_fact: play='tldr' diff --git a/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/type/play.yml b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/type/play.yml new file mode 100644 index 00000000000..dd6d563fec2 --- /dev/null +++ b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/type/play.yml @@ -0,0 +1,4 @@ +- hosts: localhost + gather_facts: false + tasks: + - set_fact: play_type='in type subdir' diff --git a/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/type/subtype/play.yml b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/type/subtype/play.yml new file mode 100644 index 00000000000..0e33a761a54 --- /dev/null +++ b/test/integration/targets/collections/collection_root_user/ansible_collections/testns/testcoll/playbooks/type/subtype/play.yml @@ -0,0 +1,4 @@ +- hosts: localhost + gather_facts: false + tasks: + - set_fact: play_type_subtype='in subtype subdir' diff --git a/test/integration/targets/collections/import_collection_pb.yml b/test/integration/targets/collections/import_collection_pb.yml index 47bfef13d4f..511d9486647 100644 --- a/test/integration/targets/collections/import_collection_pb.yml +++ b/test/integration/targets/collections/import_collection_pb.yml @@ -1,2 +1,17 @@ - import_playbook: testns.testcoll.default_collection_playbook.yml - import_playbook: testns.testcoll.default_collection_playbook + +# test subdirs +- import_playbook: "testns.testcoll.play" +- import_playbook: "testns.testcoll.type.play" +- import_playbook: "testns.testcoll.type.subtype.play" + +- hosts: localhost + gather_facts: false + tasks: + - name: check values from imports + assert: + that: + - play is defined + - play_type is defined + - play_type_subtype is defined diff --git a/test/lib/ansible_test/_data/legacy_collection_loader/_collection_finder.py b/test/lib/ansible_test/_data/legacy_collection_loader/_collection_finder.py index f684da2ef0e..e0c3c109b9e 100644 --- a/test/lib/ansible_test/_data/legacy_collection_loader/_collection_finder.py +++ b/test/lib/ansible_test/_data/legacy_collection_loader/_collection_finder.py @@ -891,6 +891,12 @@ def _get_collection_playbook_path(playbook): if pkg: cpath = os.path.join(sys.modules[acr.n_python_collection_package_name].__file__.replace('__synthetic__', 'playbooks')) + + if acr.subdirs: + paths = [to_native(x) for x in acr.subdirs.split(u'.')] + paths.insert(0, cpath) + cpath = os.path.join(*paths) + path = os.path.join(cpath, to_native(acr.resource)) if os.path.exists(to_bytes(path)): return acr.resource, path, acr.collection