Skip to content

Commit cdb53ff

Browse files
authored
Restore behavior for detecting only localhost, and no limit match. Fixes ansible#52152 (ansible#52172)
* Restore behavior for detecting only localhost, and no limit match. Fixes ansible#52152 * Add test for non-matching limit
1 parent 847d089 commit cdb53ff

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

lib/ansible/cli/__init__.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,6 @@ def _play_prereqs():
477477

478478
# create the inventory, and filter it based on the subset specified (if any)
479479
inventory = InventoryManager(loader=loader, sources=options['inventory'])
480-
subset = options.get('subset', False)
481-
if subset:
482-
inventory.subset(subset)
483480

484481
# create the variable manager, which will be shared throughout
485482
# the code, ensuring a consistent view of global variables
@@ -497,8 +494,10 @@ def get_host_list(inventory, subset, pattern='all'):
497494
display.warning("provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'")
498495
no_hosts = True
499496

497+
inventory.subset(subset)
498+
500499
hosts = inventory.list_hosts(pattern)
501-
if len(hosts) == 0 and no_hosts is False:
500+
if not hosts and no_hosts is False:
502501
raise AnsibleError("Specified hosts and/or --limit does not match any hosts")
503502

504503
return hosts

lib/ansible/cli/playbook.py

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def run(self):
9999
# create base objects
100100
loader, inventory, variable_manager = self._play_prereqs()
101101

102+
# (which is not returned in list_hosts()) is taken into account for
103+
# warning if inventory is empty. But it can't be taken into account for
104+
# checking if limit doesn't match any hosts. Instead we don't worry about
105+
# limit if only implicit localhost was in inventory to start with.
106+
#
107+
# Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts())
108+
CLI.get_host_list(inventory, context.CLIARGS['subset'])
109+
102110
# flush fact cache if requested
103111
if context.CLIARGS['flush_cache']:
104112
self._flush_cache(inventory, variable_manager)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shippable/posix/group3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- hosts: all
2+
gather_facts: false
3+
tasks:
4+
- ping:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
# https://github.com/ansible/ansible/issues/52152
6+
# Ensure that non-matching limit causes failure with rc 1
7+
ansible-playbook -i ../../inventory --limit foo playbook.yml
8+
if [ "$?" != "1" ]; then
9+
echo "Non-matching limit should cause failure"
10+
exit 1
11+
fi

0 commit comments

Comments
 (0)