From 41e19a4058bf56ad797e5c57212cd05294ff8934 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 12 Dec 2019 09:09:40 +0530 Subject: [PATCH] inventory: Fail on non-existing limit file (#59758) Ansible now fails with error message when user provides non-existing limit file. Signed-off-by: Abhijeet Kasurde --- changelogs/fragments/limit-file-exception.yml | 2 ++ lib/ansible/inventory/manager.py | 11 ++++++++--- test/integration/targets/inventory/runme.sh | 7 +++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/limit-file-exception.yml diff --git a/changelogs/fragments/limit-file-exception.yml b/changelogs/fragments/limit-file-exception.yml new file mode 100644 index 00000000000..d910b28d5b2 --- /dev/null +++ b/changelogs/fragments/limit-file-exception.yml @@ -0,0 +1,2 @@ +minor_changes: + - Ansible should fail with error when non-existing limit file is provided in command line. diff --git a/lib/ansible/inventory/manager.py b/lib/ansible/inventory/manager.py index 4bb6fe4edc5..fa37532f7fe 100644 --- a/lib/ansible/inventory/manager.py +++ b/lib/ansible/inventory/manager.py @@ -618,10 +618,15 @@ class InventoryManager(object): results = [] # allow Unix style @filename data for x in subset_patterns: + if not x: + continue + if x[0] == "@": - fd = open(x[1:]) - results.extend([to_text(l.strip()) for l in fd.read().split("\n")]) - fd.close() + b_limit_file = to_bytes(x[1:]) + if not os.path.exists(b_limit_file): + raise AnsibleError(u'Unable to find limit file %s' % b_limit_file) + with open(b_limit_file) as fd: + results.extend([to_text(l.strip()) for l in fd.read().split("\n")]) else: results.append(to_text(x)) self._subset = results diff --git a/test/integration/targets/inventory/runme.sh b/test/integration/targets/inventory/runme.sh index b6ac952a0c0..3cd533cde7b 100755 --- a/test/integration/targets/inventory/runme.sh +++ b/test/integration/targets/inventory/runme.sh @@ -21,6 +21,13 @@ if [ "$?" != "1" ]; then exit 1 fi +# Ensure that non-existing limit file causes failure with rc 1 +ansible-playbook -i ../../inventory --limit @foo playbook.yml +if [ "$?" != "1" ]; then + echo "Non-existing limit file should cause failure" + exit 1 +fi + # Ensure that non-matching limit causes failure with rc 1 ansible-playbook -i ../../inventory --limit @"${empty_limit_file}" playbook.yml