Skip to content

Commit

Permalink
Fix nested noop block padding in dynamic includes (ansible#38814)
Browse files Browse the repository at this point in the history
* Fix nested noop block padding in dynamic includes

* Address issues from the review

* Fix typo
  • Loading branch information
mkrizek authored and sivel committed Apr 26, 2018
1 parent 4b01b92 commit 5dd8977
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
43 changes: 32 additions & 11 deletions lib/ansible/plugins/strategy/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
author: Ansible Core Team
'''

from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleAssertionError
from ansible.executor.play_iterator import PlayIterator
from ansible.module_utils.six import iteritems
from ansible.module_utils._text import to_text
Expand All @@ -52,6 +52,36 @@

class StrategyModule(StrategyBase):

noop_task = None

def _replace_with_noop(self, target):
if self.noop_task is None:
raise AnsibleAssertionError('strategy.linear.StrategyModule.noop_task is None, need Task()')

result = []
for el in target:
if isinstance(el, Task):
result.append(self.noop_task)
elif isinstance(el, Block):
result.append(self._create_noop_block_from(el, el._parent))
return result

def _create_noop_block_from(self, original_block, parent):
noop_block = Block(parent_block=parent)
noop_block.block = self._replace_with_noop(original_block.block)
noop_block.always = self._replace_with_noop(original_block.always)
noop_block.rescue = self._replace_with_noop(original_block.rescue)

return noop_block

def _prepare_and_create_noop_block_from(self, original_block, parent, iterator):
self.noop_task = Task()
self.noop_task.action = 'meta'
self.noop_task.args['_raw_params'] = 'noop'
self.noop_task.set_loader(iterator._play._loader)

return self._create_noop_block_from(original_block, parent)

def _get_next_task_lockstep(self, hosts, iterator):
'''
Returns a list of (host, task) tuples, where the task may
Expand Down Expand Up @@ -309,12 +339,6 @@ def run(self, iterator, play_context):
if len(included_files) > 0:
display.debug("we have included files to process")

# A noop task for use in padding dynamic includes
noop_task = Task()
noop_task.action = 'meta'
noop_task.args['_raw_params'] = 'noop'
noop_task.set_loader(iterator._play._loader)

display.debug("generating all_blocks data")
all_blocks = dict((host, []) for host in hosts_left)
display.debug("done generating all_blocks data")
Expand Down Expand Up @@ -345,10 +369,7 @@ def run(self, iterator, play_context):
final_block = new_block.filter_tagged_tasks(play_context, task_vars)
display.debug("done filtering new block on tags")

noop_block = Block(parent_block=task._parent)
noop_block.block = [noop_task for t in new_block.block]
noop_block.always = [noop_task for t in new_block.always]
noop_block.rescue = [noop_task for t in new_block.rescue]
noop_block = self._prepare_and_create_noop_block_from(final_block, task._parent, iterator)

for host in hosts_left:
if host in included_file._hosts:
Expand Down
1 change: 1 addition & 0 deletions test/integration/targets/strategy_linear/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
posix/ci/group3
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- name: Include tasks
include_tasks: "tasks.yml"

- name: Mark role as finished
set_fact:
role1_complete: True
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- name: Call role2
include_role:
name: role2

- name: Call role2 again
include_role:
name: role2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- block:
- block:
- name: Nested task 1
debug: msg="Nested task 1"

- name: Nested task 2
debug: msg="Nested task 2"
5 changes: 5 additions & 0 deletions test/integration/targets/strategy_linear/runme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -eux

ansible-playbook test_include_file_noop.yml -i ../../inventory "$@"
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- hosts:
- testhost
- testhost2
gather_facts: no
vars:
secondhost: testhost2
tasks:
- name: Call the first role only on one host
include_role:
name: role1
when: inventory_hostname is match(secondhost)

- name: Make sure nothing else runs until role1 finishes
assert:
that:
- "'role1_complete' in hostvars[secondhost]"

0 comments on commit 5dd8977

Please sign in to comment.