Check to make sure that tags passed as parameters actually exist
This commit is contained in:
parent
dde97906f8
commit
2755602dcb
2 changed files with 40 additions and 18 deletions
|
@ -146,12 +146,34 @@ class PlayBook(object):
|
|||
|
||||
def run(self):
|
||||
''' run all patterns in the playbook '''
|
||||
plays = []
|
||||
matched_tags_all = set()
|
||||
unmatched_tags_all = set()
|
||||
|
||||
# loop through all patterns and run them
|
||||
self.callbacks.on_start()
|
||||
for play_ds in self.playbook:
|
||||
play = Play(self,play_ds)
|
||||
matched_tags, unmatched_tags = play.compare_tags(self.only_tags)
|
||||
matched_tags_all = matched_tags_all | matched_tags
|
||||
unmatched_tags_all = unmatched_tags_all | unmatched_tags
|
||||
|
||||
# if we have matched_tags, the play must be run.
|
||||
# if the play contains no tasks, assume we just want to gather facts
|
||||
if (len(matched_tags) > 0 or len(play.tasks()) == 0):
|
||||
plays.append(play)
|
||||
|
||||
# if the playbook is invoked with --tags that don't exist at all in the playbooks
|
||||
# then we need to raise an error so that the user can correct the arguments.
|
||||
unknown_tags = set(self.only_tags) - (matched_tags_all | unmatched_tags_all)
|
||||
if len(unknown_tags) > 0:
|
||||
unmatched_tags_all.discard('all')
|
||||
msg = 'tags "%s" given as argument but not found in playbooks, did you mean one of "%s"?'
|
||||
raise errors.AnsibleError(msg % (', '.join(unknown_tags),', '.join(unmatched_tags_all)))
|
||||
|
||||
for play in plays:
|
||||
self._run_play(play)
|
||||
|
||||
# summarize the results
|
||||
results = {}
|
||||
for host in self.stats.processed.keys():
|
||||
|
@ -292,9 +314,6 @@ class PlayBook(object):
|
|||
def _run_play(self, play):
|
||||
''' run a list of tasks for a given pattern, in order '''
|
||||
|
||||
if not play.should_run(self.only_tags):
|
||||
return
|
||||
|
||||
self.callbacks.on_play_start(play.name)
|
||||
|
||||
# get facts from system
|
||||
|
|
|
@ -197,22 +197,25 @@ class Play(object):
|
|||
|
||||
# *************************************************
|
||||
|
||||
def should_run(self, tags):
|
||||
''' does the play match any of the tags? '''
|
||||
def compare_tags(self, tags):
|
||||
''' given a list of tags that the user has specified, return two lists:
|
||||
matched_tags: tags were found within the current play and match those given
|
||||
by the user
|
||||
unmatched_tags: tags that were found within the current play but do not match
|
||||
any provided by the user '''
|
||||
|
||||
tags_counted = 0
|
||||
# gather all the tags in all the tasks into one list
|
||||
all_tags = []
|
||||
for task in self._tasks:
|
||||
for task_tag in task.tags:
|
||||
tags_counted = tags_counted + 1
|
||||
if task_tag in tags:
|
||||
return True
|
||||
all_tags.extend(task.tags)
|
||||
|
||||
if tags_counted > 0:
|
||||
return False
|
||||
# compare the lists of tags using sets and return the matched and unmatched
|
||||
all_tags_set = set(all_tags)
|
||||
tags_set = set(tags)
|
||||
matched_tags = all_tags_set & tags_set
|
||||
unmatched_tags = all_tags_set - tags_set
|
||||
|
||||
# didn't tag the play, and the play contains no steps
|
||||
# so assume we just want to gather facts
|
||||
return True
|
||||
return matched_tags, unmatched_tags
|
||||
|
||||
# *************************************************
|
||||
|
||||
|
|
Loading…
Reference in a new issue