Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abort a play at the start when no hosts matches, or no hosts are remaining #1202

Merged
merged 1 commit into from
Oct 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/ansible/callback_plugins/noop.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def playbook_on_start(self):
def playbook_on_notify(self, host, handler):
pass

def on_no_hosts_matched(self):
pass

def on_no_hosts_remaining(self):
pass

def playbook_on_task_start(self, name, is_conditional):
pass

Expand Down
8 changes: 8 additions & 0 deletions lib/ansible/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ def on_start(self):
def on_notify(self, host, handler):
call_callback_module('playbook_on_notify', host, handler)

def on_no_hosts_matched(self):
print stringc("no hosts matched", 'red')
call_callback_module('playbook_on_no_hosts_matched')

def on_no_hosts_remaining(self):
print stringc("\nFATAL: all hosts have already failed -- aborting", 'red')
call_callback_module('playbook_on_no_hosts_remaining')

def on_task_start(self, name, is_conditional):
msg = "TASK: [%s]" % name
if is_conditional:
Expand Down
21 changes: 15 additions & 6 deletions lib/ansible/playbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ def __init__(self,
(self.playbook, self.play_basedirs) = self._load_playbook_from_file(playbook)
self.module_path = self.module_path + os.pathsep + os.path.join(self.basedir, "library")

# which task we are currently executing
self.task_counter = 0

# *****************************************************

def _load_playbook_from_file(self, path):
Expand Down Expand Up @@ -239,8 +236,6 @@ def _run_task_internal(self, task):
# if not polling, playbook requested fire and forget, so don't poll
results = self._async_poll(poller, task.async_seconds, task.async_poll_interval)

self.task_counter = self.task_counter + 1

contacted = results.get('contacted',{})
dark = results.get('dark', {})

Expand Down Expand Up @@ -346,6 +341,11 @@ def _run_play(self, play):

self.callbacks.on_play_start(play.name)

# if no hosts matches this play, drop out
if not self.inventory.list_hosts(play.hosts):
self.callbacks.on_no_hosts_matched()
return True

# get facts from system
self._do_setup_step(play)

Expand Down Expand Up @@ -384,7 +384,15 @@ def _run_play(self, play):
# whether no hosts matched is fatal or not depends if it was on the initial step.
# if we got exactly no hosts on the first step (setup!) then the host group
# just didn't match anything and that's ok
return (self.task_counter <= 1)
return False

host_list = [ h for h in self.inventory.list_hosts(play.hosts)
if not (h in self.stats.failures or h in self.stats.dark) ]

# if no hosts remain, drop out
if not host_list:
self.callbacks.on_no_hosts_remaining()
return False

# run notify actions
for handler in play.handlers():
Expand All @@ -394,5 +402,6 @@ def _run_play(self, play):
self.inventory.lift_restriction()

self.inventory.lift_also_restriction()

return True