diff --git a/changelogs/fragments/75568-fix-templating-task-action-host-specific-vars.yml b/changelogs/fragments/75568-fix-templating-task-action-host-specific-vars.yml new file mode 100644 index 00000000000..861bd7d8efd --- /dev/null +++ b/changelogs/fragments/75568-fix-templating-task-action-host-specific-vars.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix templating task action with host-specific vars (https://github.com/ansible/ansible/issues/75568) diff --git a/lib/ansible/plugins/strategy/linear.py b/lib/ansible/plugins/strategy/linear.py index d22f03e9f00..8b434983437 100644 --- a/lib/ansible/plugins/strategy/linear.py +++ b/lib/ansible/plugins/strategy/linear.py @@ -263,16 +263,16 @@ class StrategyModule(StrategyBase): # sets BYPASS_HOST_LOOP to true, or if it has run_once enabled. If so, we # will only send this task to the first host in the list. - task.action = templar.template(task.action) + task_action = templar.template(task.action) try: - action = action_loader.get(task.action, class_only=True, collection_list=task.collections) + action = action_loader.get(task_action, class_only=True, collection_list=task.collections) except KeyError: # we don't care here, because the action may simply not have a # corresponding action plugin action = None - if task.action in C._ACTION_META: + if task_action in C._ACTION_META: # for the linear strategy, we run meta tasks just once and for # all hosts currently being iterated over rather than one host results.extend(self._execute_meta(task, play_context, iterator, host)) diff --git a/test/integration/targets/strategy_linear/inventory b/test/integration/targets/strategy_linear/inventory index 4a34c320851..698d69dc06e 100644 --- a/test/integration/targets/strategy_linear/inventory +++ b/test/integration/targets/strategy_linear/inventory @@ -1,3 +1,3 @@ [local] -testhost ansible_connection=local -testhost2 ansible_connection=local +testhost ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}" +testhost2 ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}" diff --git a/test/integration/targets/strategy_linear/runme.sh b/test/integration/targets/strategy_linear/runme.sh index 41639f3cd77..cbb6aea3a51 100755 --- a/test/integration/targets/strategy_linear/runme.sh +++ b/test/integration/targets/strategy_linear/runme.sh @@ -3,3 +3,5 @@ set -eux ansible-playbook test_include_file_noop.yml -i inventory "$@" + +ansible-playbook task_action_templating.yml -i inventory "$@" diff --git a/test/integration/targets/strategy_linear/task_action_templating.yml b/test/integration/targets/strategy_linear/task_action_templating.yml new file mode 100644 index 00000000000..5f7438feb5f --- /dev/null +++ b/test/integration/targets/strategy_linear/task_action_templating.yml @@ -0,0 +1,26 @@ +- hosts: testhost,testhost2 + gather_facts: no + tasks: + - set_fact: + module_to_run: 'debug' + when: inventory_hostname == 'testhost' + + - set_fact: + module_to_run: 'ping' + when: inventory_hostname == 'testhost2' + + - action: + module: '{{ module_to_run }}' + register: out + + - assert: + that: + - "'msg' in out" + - "'ping' not in out" + when: inventory_hostname == 'testhost' + + - assert: + that: + - "'ping' in out" + - "'msg' not in out" + when: inventory_hostname == 'testhost2'