From 2ebc4e1e7eda61f133d9425872e2a858bae92ec4 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Tue, 30 Jul 2019 22:32:17 +0530 Subject: [PATCH] inventory: Handle IndexError while parsing limit file (#59776) Handle IndexError exception raised while parsing the limit file. Fixes: #59695 Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/limit_file_parsing.yml | 2 ++ lib/ansible/inventory/manager.py | 5 ++++- test/integration/targets/inventory/runme.sh | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/limit_file_parsing.yml diff --git a/changelogs/fragments/limit_file_parsing.yml b/changelogs/fragments/limit_file_parsing.yml new file mode 100644 index 00000000000..eaefb089fba --- /dev/null +++ b/changelogs/fragments/limit_file_parsing.yml @@ -0,0 +1,2 @@ +bugfixes: + - Handle IndexError while parsing empty limit file (https://github.com/ansible/ansible/issues/59695). diff --git a/lib/ansible/inventory/manager.py b/lib/ansible/inventory/manager.py index 7c328668d11..14e61ba9334 100644 --- a/lib/ansible/inventory/manager.py +++ b/lib/ansible/inventory/manager.py @@ -69,11 +69,14 @@ def order_patterns(patterns): pattern_intersection = [] pattern_exclude = [] for p in patterns: + if not p: + continue + if p[0] == "!": pattern_exclude.append(p) elif p[0] == "&": pattern_intersection.append(p) - elif p: + else: pattern_regular.append(p) # if no regular pattern was given, hence only exclude and/or intersection diff --git a/test/integration/targets/inventory/runme.sh b/test/integration/targets/inventory/runme.sh index 31854c8778e..b6ac952a0c0 100755 --- a/test/integration/targets/inventory/runme.sh +++ b/test/integration/targets/inventory/runme.sh @@ -2,6 +2,17 @@ set -x +empty_limit_file="/tmp/limit_file" +touch "${empty_limit_file}" + +cleanup() { + if [[ -f "${empty_limit_file}" ]]; then + rm -rf "${empty_limit_file}" + fi +} + +trap 'cleanup' EXIT + # https://github.com/ansible/ansible/issues/52152 # Ensure that non-matching limit causes failure with rc 1 ansible-playbook -i ../../inventory --limit foo playbook.yml @@ -10,6 +21,9 @@ if [ "$?" != "1" ]; then exit 1 fi +# Ensure that non-matching limit causes failure with rc 1 +ansible-playbook -i ../../inventory --limit @"${empty_limit_file}" playbook.yml + ansible-playbook -i ../../inventory "$@" strategy.yml ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS=always ansible-playbook -i ../../inventory "$@" strategy.yml ANSIBLE_TRANSFORM_INVALID_GROUP_CHARS=never ansible-playbook -i ../../inventory "$@" strategy.yml