| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -eux |
| 4 |
|
| 5 |
# Collections vars plugins must be enabled using the FQCN in the 'enabled' list, because PluginLoader.all() does not search collections |
| 6 |
|
| 7 |
# Let vars plugins run for inventory by using the global setting |
| 8 |
export ANSIBLE_RUN_VARS_PLUGINS=start |
| 9 |
|
| 10 |
# Test vars plugin in a playbook-adjacent collection |
| 11 |
export ANSIBLE_VARS_ENABLED=testns.content_adj.custom_adj_vars |
| 12 |
|
| 13 |
ansible-inventory -i a.statichost.yml --list --playbook-dir=./ 2>&1 | tee out.txt |
| 14 |
|
| 15 |
# DTFIX-FUTURE: a number of the `grep -v` tests are broken- untrusted values are split across multiple lines and will not match the pattern. |
| 16 |
# These tests should probably be moved to a playbook and/or recorded diff outputs to more accurately inspect behavior. |
| 17 |
|
| 18 |
grep '"collection":' -A1 out.txt | grep '"adjacent"' |
| 19 |
grep '"adj_var":' -A1 out.txt | grep '"value"' |
| 20 |
grep -v "REQUIRES_ENABLED is not supported" out.txt |
| 21 |
|
| 22 |
# Test vars plugin in a collection path |
| 23 |
export ANSIBLE_VARS_ENABLED=testns.testcoll.custom_vars |
| 24 |
export ANSIBLE_COLLECTIONS_PATH=$PWD/collection_root_user:$PWD/collection_root_sys |
| 25 |
|
| 26 |
ansible-inventory -i a.statichost.yml --list --playbook-dir=./ 2>&1 | tee out.txt |
| 27 |
|
| 28 |
grep '"collection":' -A1 out.txt | grep '"collection_root_user"' |
| 29 |
|
| 30 |
# DTFIX-FUTURE: -v test is broken |
| 31 |
grep -v '"adj_var": "value"' out.txt |
| 32 |
grep "REQUIRES_ENABLED is not supported" out.txt |
| 33 |
|
| 34 |
# Test enabled vars plugins order reflects the order in which variables are merged |
| 35 |
export ANSIBLE_VARS_ENABLED=testns.content_adj.custom_adj_vars,testns.testcoll.custom_vars |
| 36 |
|
| 37 |
ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt |
| 38 |
|
| 39 |
grep '"collection":' -A1 out.txt | grep '"collection_root_user"' |
| 40 |
grep '"adj_var":' -A1 out.txt | grep '"value"' |
| 41 |
|
| 42 |
# DTFIX-FUTURE: -v test is broken |
| 43 |
grep -v '"collection": "adjacent"' out.txt |
| 44 |
|
| 45 |
# Test that 3rd party plugins in plugin_path do not need to require enabling by default |
| 46 |
# Plugins shipped with Ansible and in the custom plugin dir should be used first |
| 47 |
export ANSIBLE_VARS_PLUGINS=./custom_vars_plugins |
| 48 |
|
| 49 |
ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt |
| 50 |
|
| 51 |
grep '"name":' -A1 out.txt | grep '"v2_vars_plugin"' |
| 52 |
grep '"collection":' -A1 out.txt | grep '"collection_root_user"' |
| 53 |
grep '"adj_var":' -A1 out.txt | grep '"value"' |
| 54 |
|
| 55 |
# Test plugins in plugin paths that opt-in to require enabling |
| 56 |
unset ANSIBLE_VARS_ENABLED |
| 57 |
unset ANSIBLE_COLLECTIONS_PATH |
| 58 |
|
| 59 |
|
| 60 |
# Test vars plugins that support the stage setting don't run for inventory when stage is set to 'task' |
| 61 |
# and that the vars plugins that don't support the stage setting don't run for inventory when the global setting is 'demand' |
| 62 |
ANSIBLE_VARS_PLUGIN_STAGE=task ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt |
| 63 |
|
| 64 |
grep -v '"v1_vars_plugin": true' out.txt |
| 65 |
grep -v '"v2_vars_plugin": true' out.txt |
| 66 |
grep -v '"collection": "adjacent"' out.txt |
| 67 |
grep -v '"collection": "collection_root_user"' out.txt |
| 68 |
grep -v '"adj_var": "value"' out.txt |
| 69 |
|
| 70 |
# Test that the global setting allows v1 and v2 plugins to run after importing inventory |
| 71 |
ANSIBLE_RUN_VARS_PLUGINS=start ansible-inventory -i a.statichost.yml --list --playbook-dir=./ | tee out.txt |
| 72 |
|
| 73 |
grep '"v1_vars_plugin": true' out.txt |
| 74 |
grep '"v2_vars_plugin": true' out.txt |
| 75 |
grep '"name":' -A1 out.txt | grep '"v2_vars_plugin"' |
| 76 |
|
| 77 |
# Test that vars plugins in collections and in the vars plugin path are available for tasks |
| 78 |
cat << EOF > "test_task_vars.yml" |
| 79 |
--- |
| 80 |
- hosts: localhost |
| 81 |
connection: local |
| 82 |
gather_facts: no |
| 83 |
tasks: |
| 84 |
- debug: msg="{{ name }}" |
| 85 |
- debug: msg="{{ collection }}" |
| 86 |
- debug: msg="{{ adj_var }}" |
| 87 |
EOF |
| 88 |
|
| 89 |
export ANSIBLE_VARS_ENABLED=testns.content_adj.custom_adj_vars |
| 90 |
|
| 91 |
ANSIBLE_VARS_PLUGIN_STAGE=task ANSIBLE_VARS_PLUGINS=./custom_vars_plugins ansible-playbook test_task_vars.yml | grep "ok=3" |
| 92 |
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" |
| 93 |
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" |
| 94 |
ANSIBLE_VARS_PLUGINS=./custom_vars_plugins ansible-playbook test_task_vars.yml | grep "ok=3" |