mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Filter tasks based on tags during iterator setup in v2
This commit is contained in:
parent
96a7d85b61
commit
02e7385002
5 changed files with 52 additions and 7 deletions
|
@ -87,10 +87,15 @@ class PlayIterator:
|
|||
FAILED_RESCUE = 4
|
||||
FAILED_ALWAYS = 8
|
||||
|
||||
def __init__(self, inventory, play):
|
||||
def __init__(self, inventory, play, connection_info, all_vars):
|
||||
self._play = play
|
||||
|
||||
self._blocks = self._play.compile()
|
||||
self._blocks = []
|
||||
for block in self._play.compile():
|
||||
new_block = block.filter_tagged_tasks(connection_info, all_vars)
|
||||
if new_block.has_tasks():
|
||||
self._blocks.append(new_block)
|
||||
|
||||
self._host_states = {}
|
||||
for host in inventory.get_hosts(self._play.hosts):
|
||||
self._host_states[host.name] = HostState(blocks=self._blocks)
|
||||
|
|
|
@ -161,7 +161,7 @@ class TaskQueueManager:
|
|||
raise AnsibleError("Invalid play strategy specified: %s" % new_play.strategy, obj=play._ds)
|
||||
|
||||
# build the iterator
|
||||
iterator = PlayIterator(inventory=self._inventory, play=new_play)
|
||||
iterator = PlayIterator(inventory=self._inventory, play=new_play, connection_info=connection_info, all_vars=all_vars)
|
||||
|
||||
# and run the play using the strategy
|
||||
return strategy.run(iterator, connection_info)
|
||||
|
|
|
@ -281,3 +281,25 @@ class Block(Base, Become, Conditional, Taggable):
|
|||
|
||||
return value
|
||||
|
||||
def filter_tagged_tasks(self, connection_info, all_vars):
|
||||
'''
|
||||
Creates a new block, with task lists filtered based on the tags contained
|
||||
within the connection_info object.
|
||||
'''
|
||||
|
||||
def evaluate_and_append_task(target):
|
||||
tmp_list = []
|
||||
for task in target:
|
||||
if task.evaluate_tags(connection_info.only_tags, connection_info.skip_tags, all_vars=all_vars):
|
||||
tmp_list.append(task)
|
||||
return tmp_list
|
||||
|
||||
new_block = self.copy()
|
||||
new_block.block = evaluate_and_append_task(self.block)
|
||||
new_block.rescue = evaluate_and_append_task(self.rescue)
|
||||
new_block.always = evaluate_and_append_task(self.always)
|
||||
|
||||
return new_block
|
||||
|
||||
def has_tasks(self):
|
||||
return len(self.block) > 0 or len(self.rescue) > 0 or len(self.always) > 0
|
||||
|
|
|
@ -178,10 +178,6 @@ class StrategyModule(StrategyBase):
|
|||
debug("'%s' skipped because role has already run" % task)
|
||||
continue
|
||||
|
||||
if not task.evaluate_tags(connection_info.only_tags, connection_info.skip_tags, task_vars) and task.action != 'setup':
|
||||
debug("'%s' failed tag evaluation" % task)
|
||||
continue
|
||||
|
||||
if task.action == 'meta':
|
||||
# meta tasks store their args in the _raw_params field of args,
|
||||
# since they do not use k=v pairs, so get that
|
||||
|
|
22
v2/samples/test_tags.yml
Normal file
22
v2/samples/test_tags.yml
Normal file
|
@ -0,0 +1,22 @@
|
|||
- hosts: localhost
|
||||
gather_facts: no
|
||||
tasks:
|
||||
- block:
|
||||
- debug: msg="this is the tagged block"
|
||||
tags:
|
||||
- block
|
||||
- block:
|
||||
- debug: msg="tagged debug from second block"
|
||||
tags:
|
||||
- tag1
|
||||
- fail:
|
||||
tags:
|
||||
- tag1
|
||||
rescue:
|
||||
- debug: msg="tagged rescue from second block"
|
||||
tags:
|
||||
- rescue_tag
|
||||
always:
|
||||
- debug: msg="tagged always from second block"
|
||||
tags:
|
||||
- always_tag
|
Loading…
Reference in a new issue