From a75b3601d949b4e946848f9c12fce5357cebe6ee Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 29 Jul 2020 17:32:33 -0400 Subject: [PATCH] Allow changed/failed mgmt on strategy actions (#70919) (#70968) * Allow changed/failed mgmt on strategy actions (cherry picked from commit f9c3c6cba6f74f9c50c023389bf8f37a8534ada1) --- .../fragments/changed_when_group_by.yml | 2 ++ lib/ansible/plugins/strategy/__init__.py | 20 +++++++++++++++++- .../targets/changed_when/tasks/main.yml | 21 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/changed_when_group_by.yml diff --git a/changelogs/fragments/changed_when_group_by.yml b/changelogs/fragments/changed_when_group_by.yml new file mode 100644 index 00000000000..fd49acb5bdc --- /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 c6e03ab35ca..f66ed74374c 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 a3736943b58..7b997189666 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