1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

finished implementing list-hosts, started adding list-tasks/list-tags

but getting just task names and have to adjust for having blocks.
This commit is contained in:
Brian Coca 2015-04-04 10:25:55 -04:00
parent 2eb2a41d05
commit e6e69c0894
3 changed files with 118 additions and 88 deletions

View file

@ -59,12 +59,18 @@ class PlaybookExecutor:
signal.signal(signal.SIGINT, self._cleanup) signal.signal(signal.SIGINT, self._cleanup)
result = 0 result = 0
entrylist = []
entry = {}
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 if self._tqm is None: # we are doing a listing
for play in pb.get_entries(): entry = {'playbook': playbook_path}
entry['plays'] = []
i = 1
for play in pb.get_plays():
self._inventory.remove_restriction() self._inventory.remove_restriction()
# Create a temporary copy of the play here, so we can run post_validate # Create a temporary copy of the play here, so we can run post_validate
@ -73,54 +79,91 @@ class PlaybookExecutor:
new_play = play.copy() new_play = play.copy()
new_play.post_validate(all_vars, fail_on_undefined=False) new_play.post_validate(all_vars, fail_on_undefined=False)
for batch in self._get_serialized_batches(new_play): if self._tqm is None:
if len(batch) == 0: # we are just doing a listing
self._tqm.send_callback('v2_playbook_on_play_start', new_play)
self._tqm.send_callback('v2_playbook_on_no_hosts_matched') pname = new_play.get_name().strip()
result = 0 if pname == 'PLAY: <no name specified>':
break pname = 'PLAY: #%d' % i
# restrict the inventory to the hosts in the serialized batch p = { 'name': pname }
self._inventory.restrict_to_hosts(batch)
# and run it... if self._options.listhosts:
result = self._tqm.run(play=play) p['pattern']=play.hosts
p['hosts']=set(self._inventory.get_hosts(new_play.hosts))
#TODO: play tasks are really blocks, need to figure out how to get task objects from them
elif self._options.listtasks:
p['tasks'] = []
for task in play.get_tasks():
p['tasks'].append(task)
#p['tasks'].append({'name': task.get_name().strip(), 'tags': task.tags})
elif self._options.listtags:
p['tags'] = set(new_play.tags)
for task in play.get_tasks():
p['tags'].update(task)
#p['tags'].update(task.tags)
entry['plays'].append(p)
else:
# we are actually running plays
for batch in self._get_serialized_batches(new_play):
if len(batch) == 0:
self._tqm.send_callback('v2_playbook_on_play_start', new_play)
self._tqm.send_callback('v2_playbook_on_no_hosts_matched')
result = 0
break
# restrict the inventory to the hosts in the serialized batch
self._inventory.restrict_to_hosts(batch)
# and run it...
result = self._tqm.run(play=play)
if result != 0:
break
if result != 0: if result != 0:
break raise AnsibleError("Play failed!: %d" % result)
i = i + 1 # per play
if entry:
entrylist.append(entry) # per playbook
if entrylist:
return entrylist
if result != 0:
raise AnsibleError("Play failed!: %d" % result)
finally: finally:
self._cleanup() if self._tqm is not None:
self._cleanup()
if result == 0: #TODO: move to callback
#TODO: move to callback # FIXME: this stat summary stuff should be cleaned up and moved
# FIXME: this stat summary stuff should be cleaned up and moved # to a new method, if it even belongs here...
# to a new method, if it even belongs here... self._display.banner("PLAY RECAP")
self._display.banner("PLAY RECAP")
hosts = sorted(self._tqm._stats.processed.keys()) hosts = sorted(self._tqm._stats.processed.keys())
for h in hosts: for h in hosts:
t = self._tqm._stats.summarize(h) t = self._tqm._stats.summarize(h)
self._display.display("%s : %s %s %s %s" % ( self._display.display("%s : %s %s %s %s" % (
hostcolor(h, t), hostcolor(h, t),
colorize('ok', t['ok'], 'green'), colorize('ok', t['ok'], 'green'),
colorize('changed', t['changed'], 'yellow'), colorize('changed', t['changed'], 'yellow'),
colorize('unreachable', t['unreachable'], 'red'), colorize('unreachable', t['unreachable'], 'red'),
colorize('failed', t['failures'], 'red')), colorize('failed', t['failures'], 'red')),
screen_only=True screen_only=True
) )
self._display.display("%s : %s %s %s %s" % ( self._display.display("%s : %s %s %s %s" % (
hostcolor(h, t, False), hostcolor(h, t, False),
colorize('ok', t['ok'], None), colorize('ok', t['ok'], None),
colorize('changed', t['changed'], None), colorize('changed', t['changed'], None),
colorize('unreachable', t['unreachable'], None), colorize('unreachable', t['unreachable'], None),
colorize('failed', t['failures'], None)), colorize('failed', t['failures'], None)),
log_only=True log_only=True
) )
self._display.display("", screen_only=True) self._display.display("", screen_only=True)
# END STATS STUFF # END STATS STUFF
return result return result
@ -161,36 +204,3 @@ class PlaybookExecutor:
serialized_batches.append(play_hosts) serialized_batches.append(play_hosts)
return serialized_batches return serialized_batches
def list_hosts_per_play(self):
playlist = []
try:
i = 1
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)
pname = play.get_name().strip()
if pname == 'PLAY: <no name specified>':
pname = 'PLAY: #%d' % i
playlist.append( {
'name': pname,
'pattern': play.hosts,
'hosts': set(self._inventory.get_hosts(new_play.hosts)),
} )
i = i + 1
except AnsibleError:
raise
except Exception, e:
#TODO: log exception
raise AnsibleParserError("Failed to process plays: %s" % str(e))
return playlist

View file

@ -27,6 +27,7 @@ from ansible.playbook.become import Become
from ansible.playbook.helpers import load_list_of_blocks, load_list_of_roles from ansible.playbook.helpers import load_list_of_blocks, load_list_of_roles
from ansible.playbook.role import Role from ansible.playbook.role import Role
from ansible.playbook.taggable import Taggable from ansible.playbook.taggable import Taggable
from ansible.playbook.block import Block
from ansible.utils.vars import combine_vars from ansible.utils.vars import combine_vars
@ -233,6 +234,15 @@ class Play(Base, Taggable, Become):
def get_roles(self): def get_roles(self):
return self.roles[:] return self.roles[:]
def get_tasks(self):
tasklist = []
for task in self.pre_tasks + self.tasks + self.post_tasks:
if isinstance(task, Block):
tasklist.append(task.block + task.rescue + task.always)
else:
tasklist.append(task)
return tasklist
def serialize(self): def serialize(self):
data = super(Play, self).serialize() data = super(Play, self).serialize()

View file

@ -134,20 +134,30 @@ def main(args):
display = Display() display = Display()
pbex = PlaybookExecutor(playbooks=args, inventory=inventory, variable_manager=variable_manager, loader=loader, display=display, options=options) pbex = PlaybookExecutor(playbooks=args, inventory=inventory, variable_manager=variable_manager, loader=loader, display=display, options=options)
if options.listhosts: results = pbex.run()
for p in pbex.list_hosts_per_play():
print("\n %s (%s): host count=%d" % (p['name'], p['pattern'], len(p['hosts']))) if isinstance(results, list):
for host in p['hosts']: for p in results:
print(" %s" % host)
sys.exit(0) print('')
elif options.listtasks: print('playbook: %s' % p['playbook'])
print('TODO: implement') print('')
sys.exit(0)
elif options.listtags: for play in p['plays']:
print('TODO: implement') if options.listhosts:
sys.exit(0) print("\n %s (%s): host count=%d" % (play['name'], play['pattern'], len(play['hosts'])))
for host in play['hosts']:
print(" %s" % host)
if options.listtasks: #TODO: do we want to display block info?
print("\n %s: task count=%d" % (play['name'], len(play['tasks'])))
for task in play['tasks']:
print(" %s" % task)
if options.listtags:
print("\n %s: tags count=%d" % (play['name'], len(play['tags'])))
for tag in play['tags']:
print(" %s" % tag)
else: else:
return pbex.run() return results
if __name__ == "__main__": if __name__ == "__main__":
#display(" ", log_only=True) #display(" ", log_only=True)