Skip to content

Commit

Permalink
fix default collection resolution in adhoc (ansible#63298)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nitzmahone authored Oct 10, 2019
1 parent b20a0f4 commit 6d52bdf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/adhoc_default_collection.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- adhoc CLI - when playbook-dir is specified and inside a collection, use default collection logic to resolve modules/actions
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion lib/ansible/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
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
Expand Down Expand Up @@ -453,6 +453,10 @@ def _play_prereqs():
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
Expand Down
2 changes: 1 addition & 1 deletion test/integration/targets/ansible/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/integration/targets/collections/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions test/lib/ansible_test/_internal/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6d52bdf

Please sign in to comment.