From 87c75b19dd03c75659afee28856e39078c060039 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Fri, 15 Dec 2017 15:43:51 -0500 Subject: [PATCH] dont warn on not matching 'all' (#32806) * dont warn on not matching 'all' the implicit localhost warning shoudl be enough * centralized no hosts handling also extended info on implicit only --- lib/ansible/cli/__init__.py | 17 +++++++++++++++++ lib/ansible/cli/adhoc.py | 18 ++++++------------ lib/ansible/cli/console.py | 11 +---------- lib/ansible/cli/playbook.py | 10 +--------- lib/ansible/inventory/manager.py | 2 +- 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py index 923b30ebbaf..3daf65085be 100644 --- a/lib/ansible/cli/__init__.py +++ b/lib/ansible/cli/__init__.py @@ -806,3 +806,20 @@ class CLI(with_metaclass(ABCMeta, object)): variable_manager.options_vars = load_options_vars(options, CLI.version_info(gitinfo=False)) return loader, inventory, variable_manager + + @staticmethod + def get_host_list(inventory, subset, pattern='all'): + + no_hosts = False + if len(inventory.list_hosts()) == 0: + # Empty inventory + display.warning("provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'") + no_hosts = True + + inventory.subset(subset) + + hosts = inventory.list_hosts(pattern) + if len(hosts) == 0 and no_hosts is False: + raise AnsibleError("Specified hosts and/or --limit does not match any hosts") + + return hosts diff --git a/lib/ansible/cli/adhoc.py b/lib/ansible/cli/adhoc.py index 8611db458a4..2192ae98d02 100644 --- a/lib/ansible/cli/adhoc.py +++ b/lib/ansible/cli/adhoc.py @@ -112,19 +112,13 @@ class AdHocCLI(CLI): loader, inventory, variable_manager = self._play_prereqs(self.options) - no_hosts = False - if len(inventory.list_hosts()) == 0: - # Empty inventory - display.warning("provided hosts list is empty, only localhost is available") - no_hosts = True - - inventory.subset(self.options.subset) - hosts = inventory.list_hosts(pattern) - if len(hosts) == 0: - if no_hosts is False and self.options.subset: - # Invalid limit - raise AnsibleError("Specified --limit does not match any hosts") + try: + hosts = CLI.get_host_list(inventory, self.options.subset, pattern) + except AnsibleError: + if self.options.subset: + raise else: + hosts = [] display.warning("No hosts matched, nothing to do") if self.options.listhosts: diff --git a/lib/ansible/cli/console.py b/lib/ansible/cli/console.py index a40d97d84e8..c26ddea0e72 100644 --- a/lib/ansible/cli/console.py +++ b/lib/ansible/cli/console.py @@ -426,16 +426,7 @@ class ConsoleCLI(CLI, cmd.Cmd): ask_vault_pass=self.options.ask_vault_pass) self.loader.set_vault_secrets(vault_secrets) - no_hosts = False - if len(self.inventory.list_hosts()) == 0: - # Empty inventory - no_hosts = True - display.warning("provided hosts list is empty, only localhost is available") - - self.inventory.subset(self.options.subset) - hosts = self.inventory.list_hosts(self.pattern) - if len(hosts) == 0 and not no_hosts: - raise AnsibleError("Specified hosts and/or --limit does not match any hosts") + hosts = CLI.get_host_list(self.inventory, self.options.subset, self.pattern) self.groups = self.inventory.list_groups() self.hosts = [x.name for x in hosts] diff --git a/lib/ansible/cli/playbook.py b/lib/ansible/cli/playbook.py index d6482c910bb..1cbf3e80876 100644 --- a/lib/ansible/cli/playbook.py +++ b/lib/ansible/cli/playbook.py @@ -109,15 +109,7 @@ class PlaybookCLI(CLI): # limit if only implicit localhost was in inventory to start with. # # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts()) - no_hosts = False - if len(inventory.list_hosts()) == 0: - # Empty inventory - display.warning("provided hosts list is empty, only localhost is available") - no_hosts = True - inventory.subset(self.options.subset) - if len(inventory.list_hosts()) == 0 and no_hosts is False: - # Invalid limit - raise AnsibleError("Specified --limit does not match any hosts") + hosts = CLI.get_host_list(inventory, self.options.subset) # flush fact cache if requested if self.options.flush_cache: diff --git a/lib/ansible/inventory/manager.py b/lib/ansible/inventory/manager.py index cf257f09825..46ead3131c8 100644 --- a/lib/ansible/inventory/manager.py +++ b/lib/ansible/inventory/manager.py @@ -541,7 +541,7 @@ class InventoryManager(object): if implicit: results.append(implicit) - if not results: + if not results and pattern != 'all': display.warning("Could not match supplied host pattern, ignoring: %s" % pattern) return results