added listhosts draft

fixed assert from list to new yaml ansible object
taskqueue is now None when just listing
This commit is contained in:
Brian Coca 2015-04-03 13:02:42 -04:00
parent e719bf765d
commit ada86dafaf
3 changed files with 64 additions and 38 deletions

View file

@ -43,7 +43,10 @@ class PlaybookExecutor:
self._loader = loader self._loader = loader
self._options = options self._options = options
self._tqm = TaskQueueManager(inventory=inventory, callback='default', variable_manager=variable_manager, loader=loader, options=options) if options.listhosts or options.listtasks or options.listtags:
self._tqm = None
else:
self._tqm = TaskQueueManager(inventory=inventory, callback='default', variable_manager=variable_manager, loader=loader, options=options)
def run(self): def run(self):
@ -58,7 +61,7 @@ class PlaybookExecutor:
try: try:
for playbook_path in self._playbooks: for playbook_path in self._playbooks:
pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader) pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
# FIXME: playbook entries are just plays, so we should rename them # FIXME: playbook entries are just plays, so we should rename them
for play in pb.get_entries(): for play in pb.get_entries():
self._inventory.remove_restriction() self._inventory.remove_restriction()
@ -83,43 +86,40 @@ class PlaybookExecutor:
break break
if result != 0: if result != 0:
# FIXME: do something here, to signify the playbook execution failed raise AnsibleError("Play failed!: %d" % result)
self._cleanup() finally:
return result
except:
self._cleanup() self._cleanup()
raise
self._cleanup() if result == 0:
#TODO: move to callback
# FIXME: this stat summary stuff should be cleaned up and moved
# to a new method, if it even belongs here...
self._tqm._display.banner("PLAY RECAP")
# FIXME: this stat summary stuff should be cleaned up and moved hosts = sorted(self._tqm._stats.processed.keys())
# to a new method, if it even belongs here... for h in hosts:
self._tqm._display.banner("PLAY RECAP") t = self._tqm._stats.summarize(h)
hosts = sorted(self._tqm._stats.processed.keys()) self._tqm._display.display("%s : %s %s %s %s" % (
for h in hosts: hostcolor(h, t),
t = self._tqm._stats.summarize(h) colorize('ok', t['ok'], 'green'),
colorize('changed', t['changed'], 'yellow'),
colorize('unreachable', t['unreachable'], 'red'),
colorize('failed', t['failures'], 'red')),
screen_only=True
)
self._tqm._display.display("%s : %s %s %s %s" % ( self._tqm._display.display("%s : %s %s %s %s" % (
hostcolor(h, t), hostcolor(h, t, False),
colorize('ok', t['ok'], 'green'), colorize('ok', t['ok'], None),
colorize('changed', t['changed'], 'yellow'), colorize('changed', t['changed'], None),
colorize('unreachable', t['unreachable'], 'red'), colorize('unreachable', t['unreachable'], None),
colorize('failed', t['failures'], 'red')), colorize('failed', t['failures'], None)),
screen_only=True log_only=True
) )
self._tqm._display.display("%s : %s %s %s %s" % ( self._tqm._display.display("", screen_only=True)
hostcolor(h, t, False), # END STATS STUFF
colorize('ok', t['ok'], None),
colorize('changed', t['changed'], None),
colorize('unreachable', t['unreachable'], None),
colorize('failed', t['failures'], None)),
log_only=True
)
self._tqm._display.display("", screen_only=True)
# END STATS STUFF
return result return result
@ -160,3 +160,24 @@ class PlaybookExecutor:
serialized_batches.append(play_hosts) serialized_batches.append(play_hosts)
return serialized_batches return serialized_batches
def listhosts(self):
playlist = []
try:
for playbook_path in self._playbooks:
pb = Playbook.load(playbook_path, variable_manager=self._variable_manager, loader=self._loader)
for play in pb.get_entries():
# Use templated copies in case hosts: depends on variables
all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
new_play = play.copy()
new_play.post_validate(all_vars, fail_on_undefined=False)
playlist.append(set(self._inventory.get_hosts(new_play.hosts)))
except AnsibleError:
raise
except Exception, e:
raise AnsibleParserError("Failed to process plays: %s" % str(e))
return playlist

View file

@ -21,7 +21,7 @@ import os
from types import NoneType from types import NoneType
from ansible.errors import AnsibleParserError from ansible.errors import AnsibleParserError
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject, AnsibleSequence
def load_list_of_blocks(ds, parent_block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None): def load_list_of_blocks(ds, parent_block=None, role=None, task_include=None, use_handlers=False, variable_manager=None, loader=None):
@ -34,7 +34,7 @@ def load_list_of_blocks(ds, parent_block=None, role=None, task_include=None, use
# we import here to prevent a circular dependency with imports # we import here to prevent a circular dependency with imports
from ansible.playbook.block import Block from ansible.playbook.block import Block
assert type(ds) in (list, NoneType) assert ds is None or isinstance(ds, AnsibleSequence), 'block has bad type: %s' % type(ds)
block_list = [] block_list = []
if ds: if ds:
@ -64,7 +64,7 @@ def load_list_of_tasks(ds, block=None, role=None, task_include=None, use_handler
from ansible.playbook.handler import Handler from ansible.playbook.handler import Handler
from ansible.playbook.task import Task from ansible.playbook.task import Task
assert type(ds) == list assert isinstance(ds, list), 'task has bad type: %s' % type(ds)
task_list = [] task_list = []
for task in ds: for task in ds:
@ -101,7 +101,7 @@ def load_list_of_roles(ds, current_role_path=None, variable_manager=None, loader
# we import here to prevent a circular dependency with imports # we import here to prevent a circular dependency with imports
from ansible.playbook.role.include import RoleInclude from ansible.playbook.role.include import RoleInclude
assert isinstance(ds, list) assert isinstance(ds, list), 'roles has bad type: %s' % type(ds)
roles = [] roles = []
for role_def in ds: for role_def in ds:

View file

@ -133,7 +133,12 @@ def main(args):
pbex = PlaybookExecutor(playbooks=args, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options) pbex = PlaybookExecutor(playbooks=args, inventory=inventory, variable_manager=variable_manager, loader=loader, options=options)
if options.listhosts: if options.listhosts:
print('TODO: implement') i = 1
for play in pbex.listhosts():
print("\nplay #%d" % i)
for host in sorted(play):
print(" %s" % host)
i = i + 1
sys.exit(0) sys.exit(0)
elif options.listtasks: elif options.listtasks:
print('TODO: implement') print('TODO: implement')