From 40e60c947d9e52af82626d115cc9191cc12344c9 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Thu, 8 Aug 2013 18:07:57 +0200 Subject: [PATCH] Host specifiers should be sorted to ensure consistent behavior, hosts should look like this: hosts: webservers:&boston:!rack42 So when applying the host selectors, run those without the "&" first, then the &s, then the !s. Closes #3500 --- lib/ansible/inventory/__init__.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index f85add0ad73..d6384247b63 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -136,11 +136,27 @@ class Inventory(object): finds hosts that match a list of patterns. Handles negative matches as well as intersection matches. """ - try: - if patterns[0].startswith("!"): - patterns.insert(0, "all") - except IndexError: - pass + + # Host specifiers should be sorted to ensure consistent behavior + pattern_regular = [] + pattern_intersection = [] + pattern_exclude = [] + for p in patterns: + if p.startswith("!"): + pattern_exclude.append(p) + elif p.startswith("&"): + pattern_intersection.append(p) + else: + pattern_regular.append(p) + + # if no regular pattern was given, hence only exclude and/or intersection + # make that magically work + if pattern_regular == []: + pattern_regular = ['all'] + + # when applying the host selectors, run those without the "&" or "!" + # first, then the &s, then the !s. + patterns = pattern_regular + pattern_intersection + pattern_exclude hosts = set() for p in patterns: