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
This commit is contained in:
Brian Coca 2017-12-15 15:43:51 -05:00 committed by GitHub
parent 2f2f118665
commit 87c75b19dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 32 deletions

View file

@ -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

View file

@ -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:

View file

@ -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]

View file

@ -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:

View file

@ -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