2019-11-04 17:41:14 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eux
|
|
|
|
|
2020-10-30 22:13:12 +01:00
|
|
|
# Collections vars plugins must be enabled using the FQCN in the 'enabled' list, because PluginLoader.all() does not search collections
|
2019-11-04 17:41:14 +01:00
|
|
|
|
|
|
|
# Let vars plugins run for inventory by using the global setting
|
|
|
|
export ANSIBLE_RUN_VARS_PLUGINS=start
|
|
|
|
|
|
|
|
# Test vars plugin in a playbook-adjacent collection
|
|
|
|
export ANSIBLE_VARS_ENABLED=testns.content_adj.custom_adj_vars
|
|
|
|
|
|
|
|
ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt
|
|
|
|
|
|
|
|
grep '"collection": "adjacent"' out.txt
|
|
|
|
grep '"adj_var": "value"' out.txt
|
|
|
|
|
|
|
|
# Test vars plugin in a collection path
|
|
|
|
export ANSIBLE_VARS_ENABLED=testns.testcoll.custom_vars
|
2020-06-11 20:40:13 +02:00
|
|
|
export ANSIBLE_COLLECTIONS_PATH=$PWD/collection_root_user:$PWD/collection_root_sys
|
2019-11-04 17:41:14 +01:00
|
|
|
|
|
|
|
ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt
|
|
|
|
|
|
|
|
grep '"collection": "collection_root_user"' out.txt
|
|
|
|
grep -v '"adj_var": "value"' out.txt
|
|
|
|
|
|
|
|
# Test enabled vars plugins order reflects the order in which variables are merged
|
|
|
|
export ANSIBLE_VARS_ENABLED=testns.content_adj.custom_adj_vars,testns.testcoll.custom_vars
|
|
|
|
|
|
|
|
ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt
|
|
|
|
|
|
|
|
grep '"collection": "collection_root_user"' out.txt
|
|
|
|
grep '"adj_var": "value"' out.txt
|
|
|
|
grep -v '"collection": "adjacent"' out.txt
|
|
|
|
|
2020-10-30 22:13:12 +01:00
|
|
|
# Test that 3rd party plugins in plugin_path do not need to require enabling by default
|
2019-11-04 17:41:14 +01:00
|
|
|
# Plugins shipped with Ansible and in the custom plugin dir should be used first
|
|
|
|
export ANSIBLE_VARS_PLUGINS=./custom_vars_plugins
|
|
|
|
|
|
|
|
ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt
|
|
|
|
|
|
|
|
grep '"name": "v2_vars_plugin"' out.txt
|
|
|
|
grep '"collection": "collection_root_user"' out.txt
|
|
|
|
grep '"adj_var": "value"' out.txt
|
|
|
|
|
2020-10-30 22:13:12 +01:00
|
|
|
# Test plugins in plugin paths that opt-in to require enabling
|
2019-11-04 17:41:14 +01:00
|
|
|
unset ANSIBLE_VARS_ENABLED
|
2020-06-11 20:40:13 +02:00
|
|
|
unset ANSIBLE_COLLECTIONS_PATH
|
2019-11-04 17:41:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Test vars plugins that support the stage setting don't run for inventory when stage is set to 'task'
|
|
|
|
# and that the vars plugins that don't support the stage setting don't run for inventory when the global setting is 'demand'
|
|
|
|
ANSIBLE_VARS_PLUGIN_STAGE=task ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt
|
|
|
|
|
|
|
|
grep -v '"v1_vars_plugin": true' out.txt
|
|
|
|
grep -v '"v2_vars_plugin": true' out.txt
|
|
|
|
grep -v '"collection": "adjacent"' out.txt
|
|
|
|
grep -v '"collection": "collection_root_user"' out.txt
|
|
|
|
grep -v '"adj_var": "value"' out.txt
|
|
|
|
|
|
|
|
# Test that the global setting allows v1 and v2 plugins to run after importing inventory
|
|
|
|
ANSIBLE_RUN_VARS_PLUGINS=start ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt
|
|
|
|
|
|
|
|
grep '"v1_vars_plugin": true' out.txt
|
|
|
|
grep '"v2_vars_plugin": true' out.txt
|
|
|
|
grep '"name": "v2_vars_plugin"' out.txt
|
|
|
|
|
|
|
|
# Test that vars plugins in collections and in the vars plugin path are available for tasks
|
|
|
|
cat << EOF > "test_task_vars.yml"
|
|
|
|
---
|
|
|
|
- hosts: localhost
|
|
|
|
connection: local
|
|
|
|
gather_facts: no
|
|
|
|
tasks:
|
|
|
|
- debug: msg="{{ name }}"
|
|
|
|
- debug: msg="{{ collection }}"
|
|
|
|
- debug: msg="{{ adj_var }}"
|
|
|
|
EOF
|
|
|
|
|
|
|
|
export ANSIBLE_VARS_ENABLED=testns.content_adj.custom_adj_vars
|
|
|
|
|
|
|
|
ANSIBLE_VARS_PLUGIN_STAGE=task ANSIBLE_VARS_PLUGINS=./custom_vars_plugins ansible-playbook test_task_vars.yml | grep "ok=3"
|
|
|
|
ANSIBLE_RUN_VARS_PLUGINS=start ANSIBLE_VARS_PLUGIN_STAGE=inventory ANSIBLE_VARS_PLUGINS=./custom_vars_plugins ansible-playbook test_task_vars.yml | grep "ok=3"
|
|
|
|
ANSIBLE_RUN_VARS_PLUGINS=demand ANSIBLE_VARS_PLUGIN_STAGE=inventory ANSIBLE_VARS_PLUGINS=./custom_vars_plugins ansible-playbook test_task_vars.yml | grep "ok=3"
|
|
|
|
ANSIBLE_VARS_PLUGINS=./custom_vars_plugins ansible-playbook test_task_vars.yml | grep "ok=3"
|