From 6d52bdf4db39947327ff9584e879ed7baaa8b4b6 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 10 Oct 2019 12:42:03 -0700 Subject: [PATCH] fix default collection resolution in adhoc (#63298) * fix default collection resolution in adhoc * if an adhoc command is run with a playbook-dir under a configured collection, default collection resolution is used to resolve unqualified module/action names * Set ANSIBLE_PLAYBOOK_DIR in integration tests. * Fix config conflict in ansible integration test. * add adhoc default collection test * text-ify warning string --- changelogs/fragments/adhoc_default_collection.yml | 2 ++ .../ansible-test-collections-ansible-adhoc.yml | 2 ++ lib/ansible/cli/__init__.py | 6 +++++- test/integration/targets/ansible/runme.sh | 2 +- test/integration/targets/collections/runme.sh | 4 ++++ test/lib/ansible_test/_internal/executor.py | 10 ++++++++++ 6 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/adhoc_default_collection.yml create mode 100644 changelogs/fragments/ansible-test-collections-ansible-adhoc.yml diff --git a/changelogs/fragments/adhoc_default_collection.yml b/changelogs/fragments/adhoc_default_collection.yml new file mode 100644 index 00000000000..764e2edc0a0 --- /dev/null +++ b/changelogs/fragments/adhoc_default_collection.yml @@ -0,0 +1,2 @@ +bugfixes: + - adhoc CLI - when playbook-dir is specified and inside a collection, use default collection logic to resolve modules/actions diff --git a/changelogs/fragments/ansible-test-collections-ansible-adhoc.yml b/changelogs/fragments/ansible-test-collections-ansible-adhoc.yml new file mode 100644 index 00000000000..878feb10bb5 --- /dev/null +++ b/changelogs/fragments/ansible-test-collections-ansible-adhoc.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-test now properly sets ``ANSIBLE_PLAYBOOK_DIR`` for integration tests so unqualified collection references work for adhoc ``ansible`` usage diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py index 66c2bc9bff0..c725db0d003 100644 --- a/lib/ansible/cli/__init__.py +++ b/lib/ansible/cli/__init__.py @@ -26,7 +26,7 @@ from ansible.parsing.dataloader import DataLoader from ansible.parsing.vault import PromptVaultSecret, get_file_vault_secret from ansible.plugins.loader import add_all_plugin_dirs from ansible.release import __version__ -from ansible.utils.collection_loader import set_collection_playbook_paths +from ansible.utils.collection_loader import AnsibleCollectionLoader, get_collection_name_from_path, set_collection_playbook_paths from ansible.utils.display import Display from ansible.utils.path import unfrackpath from ansible.utils.unsafe_proxy import AnsibleUnsafeBytes @@ -453,6 +453,10 @@ class CLI(with_metaclass(ABCMeta, object)): loader.set_basedir(basedir) add_all_plugin_dirs(basedir) set_collection_playbook_paths(basedir) + default_collection = get_collection_name_from_path(basedir) + if default_collection: + display.warning(u'running with default collection {0}'.format(default_collection)) + AnsibleCollectionLoader().set_default_collection(default_collection) vault_ids = list(options['vault_ids']) default_vault_ids = C.DEFAULT_VAULT_IDENTITY_LIST diff --git a/test/integration/targets/ansible/runme.sh b/test/integration/targets/ansible/runme.sh index d790e481bf9..7d264cfe9d5 100755 --- a/test/integration/targets/ansible/runme.sh +++ b/test/integration/targets/ansible/runme.sh @@ -25,7 +25,7 @@ ANSIBLE_PLAYBOOK_DIR=/tmp ansible localhost -m debug -a var=playbook_dir | grep ansible localhost -m debug -a var=playbook_dir --playbook-dir=/tmp | grep '"playbook_dir": "/tmp"' # test setting playbook dir via ansible.cfg -ANSIBLE_CONFIG=./playbookdir_cfg.ini ansible localhost -m debug -a var=playbook_dir | grep '"playbook_dir": "/tmp"' +env -u ANSIBLE_PLAYBOOK_DIR ANSIBLE_CONFIG=./playbookdir_cfg.ini ansible localhost -m debug -a var=playbook_dir | grep '"playbook_dir": "/tmp"' # Test that no tmp dirs are left behind when running ansible-config TMP_DIR=~/.ansible/tmptest diff --git a/test/integration/targets/collections/runme.sh b/test/integration/targets/collections/runme.sh index c5faf154d31..d2932945042 100755 --- a/test/integration/targets/collections/runme.sh +++ b/test/integration/targets/collections/runme.sh @@ -18,6 +18,10 @@ ANSIBLE_CALLBACK_WHITELIST=testns.testcoll.usercallback ansible localhost -m pin # test documentation ansible-doc testns.testcoll.testmodule -vvv | grep -- "- normal_doc_frag" +# test adhoc default collection resolution (use unqualified collection module with playbook dir under its collection) +echo "testing adhoc default collection support with explicit playbook dir" +ANSIBLE_PLAYBOOK_DIR=./collection_root_user/ansible_collections/testns/testcoll ansible localhost -m testmodule + echo "testing bad doc_fragments (expected ERROR message follows)" # test documentation failure ansible-doc testns.testcoll.testmodule_bad_docfrags -vvv 2>&1 | grep -- "unknown doc_fragment" diff --git a/test/lib/ansible_test/_internal/executor.py b/test/lib/ansible_test/_internal/executor.py index 4938b462f84..f9bc00e9a51 100644 --- a/test/lib/ansible_test/_internal/executor.py +++ b/test/lib/ansible_test/_internal/executor.py @@ -1342,6 +1342,11 @@ def command_integration_script(args, target, test_dir, inventory_path, temp_path env = integration_environment(args, target, test_dir, test_env.inventory_path, test_env.ansible_config, env_config) cwd = os.path.join(test_env.targets_dir, target.relative_path) + env.update(dict( + # support use of adhoc ansible commands in collections without specifying the fully qualified collection name + ANSIBLE_PLAYBOOK_DIR=cwd, + )) + if env_config and env_config.env_vars: env.update(env_config.env_vars) @@ -1445,6 +1450,11 @@ def command_integration_role(args, target, start_at_task, test_dir, inventory_pa env = integration_environment(args, target, test_dir, test_env.inventory_path, test_env.ansible_config, env_config) cwd = test_env.integration_dir + env.update(dict( + # support use of adhoc ansible commands in collections without specifying the fully qualified collection name + ANSIBLE_PLAYBOOK_DIR=cwd, + )) + env['ANSIBLE_ROLES_PATH'] = test_env.targets_dir module_coverage = 'non_local/' not in target.aliases