summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Krizek <martin.krizek@gmail.com>2021-09-01 18:29:50 +0200
committerGitHub <noreply@github.com>2021-09-01 11:29:50 -0500
commita6f24c407c7e01c5eb29e223c3a8789e889fe08e (patch)
tree9b93d7ca2179deefbeae7fe7d1b8d4ae5bac8e71
parent999c2e603df0e482cc32e1465502872d3308087b (diff)
downloadansible-a6f24c407c7e01c5eb29e223c3a8789e889fe08e.tar.gz
Fix templating task action with host-specific vars (#75600) (#75613)
Fixes #75568 (cherry picked from commit 9c2f44b8848a317a2304254eba0b9b348d5034ad)
-rw-r--r--changelogs/fragments/75568-fix-templating-task-action-host-specific-vars.yml2
-rw-r--r--lib/ansible/plugins/strategy/linear.py6
-rw-r--r--test/integration/targets/strategy_linear/inventory4
-rwxr-xr-xtest/integration/targets/strategy_linear/runme.sh2
-rw-r--r--test/integration/targets/strategy_linear/task_action_templating.yml26
5 files changed, 35 insertions, 5 deletions
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 0000000000..861bd7d8ef
--- /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 d22f03e9f0..8b43498343 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 4a34c32085..698d69dc06 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 41639f3cd7..cbb6aea3a5 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 0000000000..5f7438feb5
--- /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'