| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -eux |
| 4 |
|
| 5 |
empty_limit_file="$(mktemp)" |
| 6 |
touch "${empty_limit_file}" |
| 7 |
|
| 8 |
tmpdir="$(mktemp -d)" |
| 9 |
|
| 10 |
cleanup() { |
| 11 |
if [[ -f "${empty_limit_file}" ]]; then |
| 12 |
rm -rf "${empty_limit_file}" |
| 13 |
fi |
| 14 |
rm -rf "$tmpdir" |
| 15 |
} |
| 16 |
|
| 17 |
trap 'cleanup' EXIT |
| 18 |
|
| 19 |
# https://github.com/ansible/ansible/issues/52152 |
| 20 |
# Ensure that non-matching limit causes failure with rc 1 |
| 21 |
if ansible-playbook -i ../../inventory --limit foo playbook.yml; then |
| 22 |
echo "Non-matching limit should cause failure" |
| 23 |
exit 1 |
| 24 |
fi |
| 25 |
|
| 26 |
# Ensure that non-existing limit file causes failure with rc 1 |
| 27 |
if ansible-playbook -i ../../inventory --limit @foo playbook.yml; then |
| 28 |
echo "Non-existing limit file should cause failure" |
| 29 |
exit 1 |
| 30 |
fi |
| 31 |
|
| 32 |
if ! ansible-playbook -i ../../inventory --limit @"$tmpdir" playbook.yml 2>&1 | grep 'must be a file'; then |
| 33 |
echo "Using a directory as a limit file should throw proper AnsibleError" |
| 34 |
exit 1 |
| 35 |
fi |
| 36 |
|
| 37 |
# Ensure that empty limit file does not cause IndexError #59695 |
| 38 |
ansible-playbook -i ../../inventory --limit @"${empty_limit_file}" playbook.yml |
| 39 |
|
| 40 |
ansible-playbook -i ../../inventory "$@" strategy.yml |
| 41 |
ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS=always ansible-playbook -i ../../inventory "$@" strategy.yml |
| 42 |
ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS=never ansible-playbook -i ../../inventory "$@" strategy.yml |
| 43 |
|
| 44 |
# test extra vars |
| 45 |
ansible-inventory -i testhost, -i ./extra_vars_constructed.yml --list -e 'from_extras=hey ' "$@"|grep ': "hellohey"' |
| 46 |
|
| 47 |
# test host vars from previous inventory sources |
| 48 |
ansible-inventory -i ./inv_with_host_vars.yml -i ./host_vars_constructed.yml --graph "$@" | tee out.txt |
| 49 |
if [[ "$(grep out.txt -ce '.*host_var[1|2]_defined')" != 2 ]]; then |
| 50 |
cat out.txt |
| 51 |
echo "Expected groups host_var1_defined and host_var2_defined to both exist" |
| 52 |
exit 1 |
| 53 |
fi |
| 54 |
|
| 55 |
# Do not fail when all inventories fail to parse. |
| 56 |
# Do not fail when any inventory fails to parse. |
| 57 |
ANSIBLE_INVENTORY_UNPARSED_FAILED=False ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=False ansible -m ping localhost -i /idontexist "$@" |
| 58 |
|
| 59 |
# Fail when all inventories fail to parse. |
| 60 |
# Do not fail when just one inventory fails to parse. |
| 61 |
if ANSIBLE_INVENTORY_UNPARSED_FAILED=True ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=False ansible -m ping localhost -i /idontexist; then |
| 62 |
echo "All inventories failed/did not exist, should cause failure" |
| 63 |
echo "ran with: ANSIBLE_INVENTORY_UNPARSED_FAILED=True ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=False" |
| 64 |
exit 1 |
| 65 |
fi |
| 66 |
|
| 67 |
# Same as above but ensuring no failure we *only* fail when all inventories fail to parse. |
| 68 |
# Fail when all inventories fail to parse. |
| 69 |
# Do not fail when just one inventory fails to parse. |
| 70 |
ANSIBLE_INVENTORY_UNPARSED_FAILED=True ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=False ansible -m ping localhost -i /idontexist -i ../../inventory "$@" |
| 71 |
# Fail when all inventories fail to parse. |
| 72 |
# Do not fail when just one inventory fails to parse. |
| 73 |
|
| 74 |
# Fail when any inventories fail to parse. |
| 75 |
if ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=True ansible -m ping localhost -i /idontexist -i ../../inventory; then |
| 76 |
echo "One inventory failed/did not exist, should NOT cause failure" |
| 77 |
echo "ran with: ANSIBLE_INVENTORY_UNPARSED_FAILED=True ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=False" |
| 78 |
exit 1 |
| 79 |
fi |
| 80 |
|
| 81 |
# Test parsing an empty config |
| 82 |
set +e |
| 83 |
ANSIBLE_INVENTORY_UNPARSED_FAILED=True ANSIBLE_INVENTORY_ENABLED=constructed ansible-inventory -i ./test_empty.yml --list "$@" |
| 84 |
rc_failed_inventory="$?" |
| 85 |
set -e |
| 86 |
if [[ "$rc_failed_inventory" != 1 ]]; then |
| 87 |
echo "Config was empty so inventory was not parsed, should cause failure" |
| 88 |
exit 1 |
| 89 |
fi |
| 90 |
|
| 91 |
# Ensure we don't throw when an empty directory is used as inventory |
| 92 |
ansible-playbook -i "$tmpdir" playbook.yml |
| 93 |
|
| 94 |
# Ensure we can use a directory of inventories |
| 95 |
cp ../../inventory "$tmpdir" |
| 96 |
ansible-playbook -i "$tmpdir" playbook.yml |
| 97 |
|
| 98 |
# ... even if it contains another empty directory |
| 99 |
mkdir "$tmpdir/empty" |
| 100 |
ansible-playbook -i "$tmpdir" playbook.yml |
| 101 |
|
| 102 |
if ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=True ansible -m ping localhost -i "$tmpdir"; then |
| 103 |
echo "Empty directory should cause failure when ANSIBLE_INVENTORY_ANY_UNPARSED_IS_FAILED=True" |
| 104 |
exit 1 |
| 105 |
fi |
| 106 |
|
| 107 |
# ensure we don't traceback on inventory due to variables with int as key |
| 108 |
ansible-inventory -i inv_with_int.yml --list "$@" |
| 109 |
|
| 110 |
|
| 111 |
# test in subshell relative paths work mid play for extra vars in inventory refresh |
| 112 |
{ |
| 113 |
cd 1/2 |
| 114 |
ansible-playbook -e @../vars.yml -i 'web_host.example.com,' -i inventory.yml 3/extra_vars_relative.yml "$@" |
| 115 |
} |