Skip to content

Instantly share code, notes, and snippets.

@ivandeex
Created August 15, 2020 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivandeex/dfb69718baad3e2ac3feec6dd3296045 to your computer and use it in GitHub Desktop.
Save ivandeex/dfb69718baad3e2ac3feec6dd3296045 to your computer and use it in GitHub Desktop.
group_by in ansible 2.9.11 reports as changed breaking idempotence
Commit: f9c3c6cba6f74f9c50c023389bf8f37a8534ada1
Date: Wed Jul 29 10:44:46 2020 -0400
Message: Allow changed/failed mgmt on strategy actions (#70919)
--- a/ansible/plugins/strategy/__init__.py
+++ b/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 @@ class StrategySentinel:
_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:
@@ -643,10 +659,12 @@ class StrategyBase:
new_host_info = result_item.get('add_host', dict())
self._add_host(new_host_info, iterator)
+ 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
if original_task.delegate_to is not None and original_task.delegate_facts:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment