From 9a1c291d0193da9954500687f0759221a52569dd Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 25 Jan 2019 16:00:28 -0500 Subject: [PATCH] add toggle for host pattern mismatch behaviour (#51199) * add toggle for host pattern mismatch behaviour fixes #40030 * fix --- lib/ansible/config/base.yml | 9 +++++++++ lib/ansible/inventory/manager.py | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/ansible/config/base.yml b/lib/ansible/config/base.yml index 60d59ef90d3..93288b5ea9e 100644 --- a/lib/ansible/config/base.yml +++ b/lib/ansible/config/base.yml @@ -1391,6 +1391,15 @@ HOST_KEY_CHECKING: ini: - {key: host_key_checking, section: defaults} type: boolean +HOST_PATTERN_MISMATCH: + name: Control host pattern mismatch behaviour + default: 'warning' + description: This setting changes the behaviour of mismatched host patterns, it allows you to force a fatal error, a warning or just ignore it + env: [{name: ANSIBLE_HOST_PATTERN_MISMATCH}] + ini: + - {key: host_pattern_mismatch, section: inventory} + choices: ['warning', 'error', 'ignore'] + version_added: "2.8" INVALID_TASK_ATTRIBUTE_FAILED: name: Controls whether invalid attributes for a task result in errors instead of warnings default: True diff --git a/lib/ansible/inventory/manager.py b/lib/ansible/inventory/manager.py index 66885da8478..9d96bf76f90 100644 --- a/lib/ansible/inventory/manager.py +++ b/lib/ansible/inventory/manager.py @@ -553,7 +553,13 @@ class InventoryManager(object): # Display warning if specified host pattern did not match any groups or hosts if not results and not matching_groups and pattern != 'all': - display.warning("Could not match supplied host pattern, ignoring: %s" % pattern) + msg = "Could not match supplied host pattern, ignoring: %s" % pattern + display.debug(msg) + if C.HOST_PATTERN_MISMATCH == 'warning': + display.warning(msg) + elif C.HOST_PATTERN_MISMATCH == 'error': + raise AnsibleOptionsError(msg) + # no need to write 'ignore' state return results