Allow changed/failed mgmt on strategy actions (#70919) (#70968)

* Allow changed/failed mgmt on strategy actions

(cherry picked from commit f9c3c6cba6)
This commit is contained in:
Brian Coca 2020-07-29 17:32:33 -04:00 committed by GitHub
parent 4c459f341b
commit a75b3601d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 1 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Restore the ability for changed_when/failed_when to function with group_by (#70844).

View file

@ -37,11 +37,11 @@ from ansible.errors import AnsibleError, AnsibleFileNotFound, AnsibleParserError
from ansible.executor import action_write_locks
from ansible.executor.process.worker import WorkerProcess
from ansible.executor.task_result import TaskResult
from ansible.inventory.host import Host
from ansible.module_utils.six.moves import queue as Queue
from ansible.module_utils.six import iteritems, itervalues, string_types
from ansible.module_utils._text import to_text
from ansible.module_utils.connection import Connection, ConnectionError
from ansible.playbook.conditional import Conditional
from ansible.playbook.handler import Handler
from ansible.playbook.helpers import load_list_of_blocks
from ansible.playbook.included_file import IncludedFile
@ -78,6 +78,22 @@ def SharedPluginLoaderObj():
_sentinel = StrategySentinel()
def post_process_whens(result, task, templar):
cond = None
if task.changed_when:
cond = Conditional(loader=templar._loader)
cond.when = task.changed_when
result['changed'] = cond.evaluate_conditional(templar, templar.available_variables)
if task.failed_when:
if cond is None:
cond = Conditional(loader=templar._loader)
cond.when = task.failed_when
failed_when_result = cond.evaluate_conditional(templar, templar.available_variables)
result['failed_when_result'] = result['failed'] = failed_when_result
def results_thread_main(strategy):
while True:
try:
@ -642,10 +658,12 @@ class StrategyBase:
# this task added a new host (add_host module)
new_host_info = result_item.get('add_host', dict())
self._add_host(new_host_info, result_item)
post_process_whens(result_item, original_task, handler_templar)
elif 'add_group' in result_item:
# this task added a new group (group_by module)
self._add_group(original_host, result_item)
post_process_whens(result_item, original_task, handler_templar)
if 'ansible_facts' in result_item:
# if delegated fact and we are delegating facts, we need to change target host for them

View file

@ -38,3 +38,24 @@
assert:
that:
- "not shell_result.changed"
- name: Add hosts to test group and ensure it appears as changed
group_by:
key: "cw_test1_{{ inventory_hostname }}"
register: groupby
- name: verify its changed
assert:
that:
- groupby is changed
- name: Add hosts to test group and ensure it does NOT appear as changed
group_by:
key: "cw_test2_{{ inventory_hostname }}"
changed_when: False
register: groupby
- name: verify its not changed
assert:
that:
- groupby is not changed