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,6 +79,34 @@ 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)
if self._tqm is None:
# we are just doing a listing
pname = new_play.get_name().strip()
if pname == 'PLAY: <no name specified>':
pname = 'PLAY: #%d' % i
p = { 'name': pname }
if self._options.listhosts:
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): for batch in self._get_serialized_batches(new_play):
if len(batch) == 0: if len(batch) == 0:
self._tqm.send_callback('v2_playbook_on_play_start', new_play) self._tqm.send_callback('v2_playbook_on_play_start', new_play)
@ -88,10 +122,19 @@ class PlaybookExecutor:
if result != 0: if result != 0:
raise AnsibleError("Play failed!: %d" % result) raise AnsibleError("Play failed!: %d" % result)
i = i + 1 # per play
if entry:
entrylist.append(entry) # per playbook
if entrylist:
return entrylist
finally: finally:
if self._tqm is not None:
self._cleanup() 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...
@ -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)
results = pbex.run()
if isinstance(results, list):
for p in results:
print('')
print('playbook: %s' % p['playbook'])
print('')
for play in p['plays']:
if options.listhosts: if options.listhosts:
for p in pbex.list_hosts_per_play(): print("\n %s (%s): host count=%d" % (play['name'], play['pattern'], len(play['hosts'])))
print("\n %s (%s): host count=%d" % (p['name'], p['pattern'], len(p['hosts']))) for host in play['hosts']:
for host in p['hosts']:
print(" %s" % host) print(" %s" % host)
sys.exit(0) if options.listtasks: #TODO: do we want to display block info?
elif options.listtasks: print("\n %s: task count=%d" % (play['name'], len(play['tasks'])))
print('TODO: implement') for task in play['tasks']:
sys.exit(0) print(" %s" % task)
elif options.listtags: if options.listtags:
print('TODO: implement') print("\n %s: tags count=%d" % (play['name'], len(play['tags'])))
sys.exit(0) 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)