From cdb53ff1a43cebe937dea87e579e548de9c5b144 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Wed, 13 Feb 2019 11:52:30 -0600 Subject: [PATCH] Restore behavior for detecting only localhost, and no limit match. Fixes #52152 (#52172) * Restore behavior for detecting only localhost, and no limit match. Fixes #52152 * Add test for non-matching limit --- lib/ansible/cli/__init__.py | 7 +++---- lib/ansible/cli/playbook.py | 8 ++++++++ test/integration/targets/inventory/aliases | 1 + test/integration/targets/inventory/playbook.yml | 4 ++++ test/integration/targets/inventory/runme.sh | 11 +++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 test/integration/targets/inventory/aliases create mode 100644 test/integration/targets/inventory/playbook.yml create mode 100755 test/integration/targets/inventory/runme.sh diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py index f9e2f7438fb..0543bc1b33b 100644 --- a/lib/ansible/cli/__init__.py +++ b/lib/ansible/cli/__init__.py @@ -477,9 +477,6 @@ class CLI(with_metaclass(ABCMeta, object)): # create the inventory, and filter it based on the subset specified (if any) inventory = InventoryManager(loader=loader, sources=options['inventory']) - subset = options.get('subset', False) - if subset: - inventory.subset(subset) # create the variable manager, which will be shared throughout # the code, ensuring a consistent view of global variables @@ -497,8 +494,10 @@ class CLI(with_metaclass(ABCMeta, object)): 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: + if not hosts 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/playbook.py b/lib/ansible/cli/playbook.py index 8b10c95e6a8..cb3de31eb6b 100644 --- a/lib/ansible/cli/playbook.py +++ b/lib/ansible/cli/playbook.py @@ -99,6 +99,14 @@ class PlaybookCLI(CLI): # create base objects loader, inventory, variable_manager = self._play_prereqs() + # (which is not returned in list_hosts()) is taken into account for + # warning if inventory is empty. But it can't be taken into account for + # checking if limit doesn't match any hosts. Instead we don't worry about + # 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()) + CLI.get_host_list(inventory, context.CLIARGS['subset']) + # flush fact cache if requested if context.CLIARGS['flush_cache']: self._flush_cache(inventory, variable_manager) diff --git a/test/integration/targets/inventory/aliases b/test/integration/targets/inventory/aliases new file mode 100644 index 00000000000..b59832142f2 --- /dev/null +++ b/test/integration/targets/inventory/aliases @@ -0,0 +1 @@ +shippable/posix/group3 diff --git a/test/integration/targets/inventory/playbook.yml b/test/integration/targets/inventory/playbook.yml new file mode 100644 index 00000000000..5e073614089 --- /dev/null +++ b/test/integration/targets/inventory/playbook.yml @@ -0,0 +1,4 @@ +- hosts: all + gather_facts: false + tasks: + - ping: diff --git a/test/integration/targets/inventory/runme.sh b/test/integration/targets/inventory/runme.sh new file mode 100755 index 00000000000..8fb90ea65b4 --- /dev/null +++ b/test/integration/targets/inventory/runme.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -x + +# 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 +if [ "$?" != "1" ]; then + echo "Non-matching limit should cause failure" + exit 1 +fi