summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2020-07-29 17:32:33 -0400
committerGitHub <noreply@github.com>2020-07-29 14:32:33 -0700
commita75b3601d949b4e946848f9c12fce5357cebe6ee (patch)
tree5220136fa83267cb74558c3a308fa9b851d111d1
parent4c459f341b88a87d04dc474eacc9feb707ac5987 (diff)
downloadansible-a75b3601d949b4e946848f9c12fce5357cebe6ee.tar.gz
Allow changed/failed mgmt on strategy actions (#70919) (#70968)
* Allow changed/failed mgmt on strategy actions (cherry picked from commit f9c3c6cba6f74f9c50c023389bf8f37a8534ada1)
-rw-r--r--changelogs/fragments/changed_when_group_by.yml2
-rw-r--r--lib/ansible/plugins/strategy/__init__.py20
-rw-r--r--test/integration/targets/changed_when/tasks/main.yml21
3 files changed, 42 insertions, 1 deletions
diff --git a/changelogs/fragments/changed_when_group_by.yml b/changelogs/fragments/changed_when_group_by.yml
new file mode 100644
index 0000000000..fd49acb5bd
--- /dev/null
+++ b/changelogs/fragments/changed_when_group_by.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - Restore the ability for changed_when/failed_when to function with group_by (#70844).
diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py
index c6e03ab35c..f66ed74374 100644
--- a/lib/ansible/plugins/strategy/__init__.py
+++ b/lib/ansible/plugins/strategy/__init__.py
@@ -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
diff --git a/test/integration/targets/changed_when/tasks/main.yml b/test/integration/targets/changed_when/tasks/main.yml
index a3736943b5..7b99718966 100644
--- a/test/integration/targets/changed_when/tasks/main.yml
+++ b/test/integration/targets/changed_when/tasks/main.yml
@@ -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